From c5ad27a9262466fc24ae1b96778fb27a5b6a331c Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Thu, 9 Feb 2012 15:21:26 +0200 Subject: [PATCH] code cleanups --- cmd/inquirer/inquirer.go | 47 +-------------------- cmd/stress/inquirerstress/inquirerstress.go | 34 ++++++++++----- cmd/stress/raterstress/raterstress.go | 30 +++++++++++++ cmd/stress/spansstress/spansstress.go | 14 ++++-- 4 files changed, 65 insertions(+), 60 deletions(-) diff --git a/cmd/inquirer/inquirer.go b/cmd/inquirer/inquirer.go index 9ab64aed6..677c7a374 100644 --- a/cmd/inquirer/inquirer.go +++ b/cmd/inquirer/inquirer.go @@ -10,19 +10,10 @@ import ( "net/rpc" "net/rpc/jsonrpc" "runtime" - "sync" "time" ) -var ( - nCPU = runtime.NumCPU() - raterList *RaterList - inChannels []chan *timespans.CallDescriptor - outChannels []chan *timespans.CallCost - multiplexerIndex int - mu sync.Mutex - sem = make(chan int, nCPU) -) +var raterList *RaterList /* Handler for the statistics web client @@ -36,40 +27,6 @@ func handler(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, "") } -/* -Creates a gorutine for every cpu core and the multiplexses the calls to each of them. -*/ -func initThreadedCallRater() { - multiplexerIndex = 0 - runtime.GOMAXPROCS(nCPU) - inChannels = make([]chan *timespans.CallDescriptor, nCPU) - outChannels = make([]chan *timespans.CallCost, nCPU) - for i := 0; i < nCPU; i++ { - inChannels[i] = make(chan *timespans.CallDescriptor) - outChannels[i] = make(chan *timespans.CallCost) - go func(in chan *timespans.CallDescriptor, out chan *timespans.CallCost) { - for { - key := <-in - out <- CallRater(key) - } - }(inChannels[i], outChannels[i]) - } -} - -/* - */ -func ThreadedCallRater(key *timespans.CallDescriptor) (reply *timespans.CallCost) { - mu.Lock() - defer mu.Unlock() - if multiplexerIndex >= nCPU { - multiplexerIndex = 0 - } - inChannels[multiplexerIndex] <- key - reply = <-outChannels[multiplexerIndex] - multiplexerIndex++ - return -} - /* The function that gets the information from the raters using balancer. */ @@ -124,7 +81,7 @@ func main() { go StopSingnalHandler() go listenToTheWorld() - //initThreadedCallRater() + runtime.GOMAXPROCS(runtime.NumCPU() - 1) http.HandleFunc("/", handler) log.Print("The server is listening...") http.ListenAndServe(":2000", nil) diff --git a/cmd/stress/inquirerstress/inquirerstress.go b/cmd/stress/inquirerstress/inquirerstress.go index beea3b81f..985568999 100644 --- a/cmd/stress/inquirerstress/inquirerstress.go +++ b/cmd/stress/inquirerstress/inquirerstress.go @@ -1,31 +1,43 @@ package main import ( + "flag" "github.com/rif/cgrates/timespans" "log" "net/rpc/jsonrpc" "time" ) +var ( + runs = flag.Int("runs", 10000, "stress cycle number") + parallel = flag.Bool("parallel", false, "run requests in parallel") +) + func main() { + flag.Parse() t1 := time.Date(2012, time.February, 02, 17, 30, 0, 0, time.UTC) t2 := time.Date(2012, time.February, 02, 18, 30, 0, 0, time.UTC) cd := timespans.CallDescriptor{CstmId: "vdf", Subject: "rif", DestinationPrefix: "0256", TimeStart: t1, TimeEnd: t2} result := timespans.CallCost{} client, _ := jsonrpc.Dial("tcp", "localhost:5090") - runs := int(1e5) i := 0 - c := make(chan string) - for ; i < runs; i++ { - go func() { - var reply string + if *parallel { + c := make(chan string) + for ; i < *runs; i++ { + go func() { + var reply string + client.Call("Responder.Get", cd, &result) + c <- reply + }() + //time.Sleep(1*time.Second) + } + for j := 0; j < *runs; j++ { + <-c + } + } else { + for j := 0; j < *runs; j++ { client.Call("Responder.Get", cd, &result) - c <- reply - }() - //time.Sleep(1*time.Second) - } - for j := 0; j < runs; j++ { - <-c + } } log.Println(result) log.Println(i) diff --git a/cmd/stress/raterstress/raterstress.go b/cmd/stress/raterstress/raterstress.go index e69de29bb..200218b7a 100644 --- a/cmd/stress/raterstress/raterstress.go +++ b/cmd/stress/raterstress/raterstress.go @@ -0,0 +1,30 @@ +package main + +import ( + "github.com/rif/cgrates/timespans" + "log" + "net/rpc/jsonrpc" + "time" + "flag" +) + +var ( + runs = flag.Int("runs", 10000, "stress cycle number") +) + + +func main() { + flag.Parse() + t1 := time.Date(2012, time.February, 02, 17, 30, 0, 0, time.UTC) + t2 := time.Date(2012, time.February, 02, 18, 30, 0, 0, time.UTC) + cd := timespans.CallDescriptor{CstmId: "vdf", Subject: "rif", DestinationPrefix: "0256", TimeStart: t1, TimeEnd: t2} + result := timespans.CallCost{} + client, _ := jsonrpc.Dial("tcp", "localhost:5090") + i := 0 + for j := 0; j < *runs; j++ { + client.Call("Storage.GetCost", cd, &result) + } + log.Println(result) + log.Println(i) + client.Close() +} diff --git a/cmd/stress/spansstress/spansstress.go b/cmd/stress/spansstress/spansstress.go index 401c27652..2e315f2e7 100644 --- a/cmd/stress/spansstress/spansstress.go +++ b/cmd/stress/spansstress/spansstress.go @@ -9,7 +9,10 @@ import ( "time" ) -var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file") +var ( + cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file") + runs = flag.Int("runs", 10000, "stress cycle number") +) func main() { flag.Parse() @@ -27,15 +30,18 @@ func main() { t2 := time.Date(2012, time.February, 02, 18, 30, 0, 0, time.UTC) cd := timespans.CallDescriptor{CstmId: "vdf", Subject: "rif", DestinationPrefix: "0256", TimeStart: t1, TimeEnd: t2} - i := 0 - result := ×pans.CallCost{} + var result *timespans.CallCost getter, _ := timespans.NewRedisStorage("", 10) defer getter.Close() + + i := 0 + log.Printf("Runnning %d cycles...", *runs) - for ; i < 1e5; i++ { + for j := 0; j < *runs; j++ { result, _ = cd.GetCost(getter) } + log.Print(result) log.Print(i) }