diff --git a/sessions/sessions.go b/sessions/sessions.go index d941a331f..3f19ebcd6 100644 --- a/sessions/sessions.go +++ b/sessions/sessions.go @@ -921,11 +921,7 @@ func (sS *SessionS) asActiveSessions(fltrs map[string]string, delete(fltrs, fltrFldName) } } - var remainingSessions []*Session // Survived index matching - ss := sS.getSessions(fltrs[utils.CGRID], psv) - for _, s := range ss { - remainingSessions = append(remainingSessions, s) - } + remainingSessions := sS.getSessions(fltrs[utils.CGRID], psv) if len(fltrs) != 0 { // Still have some filters to match for _, s := range remainingSessions { for _, sr := range s.SRuns { diff --git a/sessions/sessions_bench_test.go b/sessions/sessions_bench_test.go new file mode 100644 index 000000000..c4e4a72e1 --- /dev/null +++ b/sessions/sessions_bench_test.go @@ -0,0 +1,154 @@ +/* +Real-time Online/Offline Charging System (OCS) for Telecom & ISP environments +Copyright (C) ITsysCOM GmbH + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see +*/ + +package sessions + +import ( + "fmt" + "log" + "net/rpc" + "net/rpc/jsonrpc" + "path" + "sync" + "testing" + "time" + + "github.com/cgrates/cgrates/config" + "github.com/cgrates/cgrates/utils" +) + +var ( + SessionBenchmarkCfg *config.CGRConfig + SessionBenchmarkRPC *rpc.Client + ConnectOnce sync.Once + NoSessions int +) + +func startRPC() { + var err error + SessionBenchmarkCfg, err = config.NewCGRConfigFromPath(path.Join(config.CgrConfig().DataFolderPath, "conf", "samples", "tutmysql")) + if err != nil { + log.Fatal(err) + } + config.SetCgrConfig(SessionBenchmarkCfg) + if SessionBenchmarkRPC, err = jsonrpc.Dial("tcp", SessionBenchmarkCfg.ListenCfg().RPCJSONListen); err != nil { + log.Fatalf("Error at dialing rcp client:%v\n", err) + } +} + +func addBalance(SessionBenchmarkRPC *rpc.Client, sraccount string) { + attrSetBalance := utils.AttrSetBalance{ + Tenant: "cgrates.org", + Account: sraccount, + BalanceType: utils.VOICE, + BalanceID: utils.StringPointer("TestDynamicDebitBalance"), + Value: utils.Float64Pointer(5 * float64(time.Hour)), + RatingSubject: utils.StringPointer("*zero5ms"), + } + var reply string + if err := SessionBenchmarkRPC.Call("ApierV2.SetBalance", attrSetBalance, &reply); err != nil { + log.Fatal(err) + // } else if reply != utils.OK { + // log.Fatalf("Received: %s", reply) + } +} + +func addAccouns() { + var wg sync.WaitGroup + for i := 0; i < 23000; i++ { + wg.Add(1) + go func(i int, SessionBenchmarkRPC *rpc.Client) { + addBalance(SessionBenchmarkRPC, fmt.Sprintf("1001%v1002", i)) + addBalance(SessionBenchmarkRPC, fmt.Sprintf("1001%v1001", i)) + wg.Done() + }(i, SessionBenchmarkRPC) + } + wg.Wait() +} + +func sendInit() { + initArgs := &V1InitSessionArgs{ + InitSession: true, + CGREvent: utils.CGREvent{ + Tenant: "cgrates.org", + ID: "", + Event: map[string]interface{}{ + utils.EVENT_NAME: "TEST_EVENT", + utils.ToR: utils.VOICE, + utils.OriginID: "123491", + utils.Account: "1001", + utils.Subject: "1001", + utils.Destination: "1002", + utils.Category: "call", + utils.Tenant: "cgrates.org", + utils.RequestType: utils.META_PREPAID, + utils.SetupTime: time.Date(2016, time.January, 5, 18, 30, 59, 0, time.UTC), + utils.AnswerTime: time.Date(2016, time.January, 5, 18, 31, 05, 0, time.UTC), + utils.Usage: "10", // 5MB + }, + }, + } + // var wg sync.WaitGroup + for i := 0; i < 23000; i++ { + // wg.Add(1) + // go func(i int, SessionBenchmarkRPC *rpc.Client) { + initArgs.ID = utils.UUIDSha1Prefix() + initArgs.Event[utils.OriginID] = utils.UUIDSha1Prefix() + initArgs.Event[utils.Account] = fmt.Sprintf("1001%v1002", i) + initArgs.Event[utils.Subject] = initArgs.Event[utils.Account] + initArgs.Event[utils.Destination] = fmt.Sprintf("1001%v1001", i) + + var initRpl *V1InitSessionReply + if err := SessionBenchmarkRPC.Call(utils.SessionSv1InitiateSession, + initArgs, &initRpl); err != nil { + log.Fatal(err) + } + // _ = getCount(SessionBenchmarkRPC) + // if c := getCount(SessionBenchmarkRPC); i+1 != c { + // log.Fatalf("Not Enough sessions %v!=%v", i+1, c) + // } + // wg.Done() + // }(i, SessionBenchmarkRPC) + } + // wg.Wait() +} + +func getCount(SessionBenchmarkRPC *rpc.Client) int { + var count int + if err := SessionBenchmarkRPC.Call(utils.SessionSv1GetActiveSessionsCount, + map[string]string{}, &count); err != nil { + log.Fatal(err) + } + return count +} + +func BenchmarkSendInitSession(b *testing.B) { + ConnectOnce.Do(func() { + startRPC() + // addAccouns() + sendInit() + // time.Sleep(3 * time.Minute) + }) + b.ResetTimer() + for i := 0; i < b.N; i++ { + _ = getCount(SessionBenchmarkRPC) + // if count < 2000 { + // b.Fatal("Not Enough sessions") + // } + } +}