From 8271f8eb1973d727b7b51461b054e4fd757c8dda Mon Sep 17 00:00:00 2001 From: Urko Date: Wed, 5 Apr 2023 14:00:39 +0200 Subject: [PATCH] refactor: improve benchmark to do both at same time --- .gitignore | 2 + benchmark/go/{main.go => go.go} | 56 +++++++++-------------- benchmark/main.go | 39 ++++++++++++++++ benchmark/nest/{main.go => nest.go} | 69 ++++++++++++----------------- 4 files changed, 92 insertions(+), 74 deletions(-) rename benchmark/go/{main.go => go.go} (72%) create mode 100644 benchmark/main.go rename benchmark/nest/{main.go => nest.go} (85%) diff --git a/.gitignore b/.gitignore index 2282dbe..0205dcc 100644 --- a/.gitignore +++ b/.gitignore @@ -100,3 +100,5 @@ sw.* # Vim swap files *.swp + +dump \ No newline at end of file diff --git a/benchmark/go/main.go b/benchmark/go/go.go similarity index 72% rename from benchmark/go/main.go rename to benchmark/go/go.go index fc5eb88..d44b468 100644 --- a/benchmark/go/main.go +++ b/benchmark/go/go.go @@ -1,48 +1,15 @@ -package main +package go_benchmark import ( "fmt" "io" "log" "net/http" - "os" - "strings" "sync" - "time" "gitea.urkob.com/urko/ess-etl-go/pkg/crono" ) -func main() { - log.SetFlags(log.Lmicroseconds) - logFileName := fmt.Sprintf("%s.txt", time.Now().Format(strings.ReplaceAll(time.RFC1123Z, ":", "_"))) - f, err := os.OpenFile(logFileName, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0o644) - if err != nil { - log.Fatal(err) - } - - defer f.Close() - log.SetOutput(f) - - cr := crono.New() - defer cr.Table() - host := "http://127.0.0.1:45654" - - wg := &sync.WaitGroup{} - errChan := make(chan error, 1) - wg.Add(1) - go func(errChan chan error) { - defer wg.Done() - for i := 0; i < 1000; i++ { - wg.Add(1) - go doRequest(wg, host, "975135", errChan) - } - }(errChan) - - wg.Wait() - cr.MarkAndRestart("finish handle all response") -} - func doRequest(wg *sync.WaitGroup, host, employeeID string, errChan chan error) error { defer wg.Done() url := host + "/employee_wi/" + employeeID @@ -76,3 +43,24 @@ func doRequest(wg *sync.WaitGroup, host, employeeID string, errChan chan error) return nil } + +func Benchmark() { + cr := crono.New() + defer cr.Table() + host := "http://127.0.0.1:45654" + + wg := &sync.WaitGroup{} + errChan := make(chan error, 1) + wg.Add(1) + totalRequests := 1000 + go func(errChan chan error) { + defer wg.Done() + for i := 0; i < totalRequests; i++ { + wg.Add(1) + go doRequest(wg, host, "975135", errChan) + } + }(errChan) + + wg.Wait() + cr.MarkAndRestart(fmt.Sprintf("go handle %d response", totalRequests)) +} diff --git a/benchmark/main.go b/benchmark/main.go new file mode 100644 index 0000000..fca95f6 --- /dev/null +++ b/benchmark/main.go @@ -0,0 +1,39 @@ +package main + +import ( + "fmt" + "log" + "os" + "strings" + "sync" + "time" + + go_benchmark "gitea.urkob.com/urko/ess-etl-go/benchmark/go" + nest_benchmark "gitea.urkob.com/urko/ess-etl-go/benchmark/nest" +) + +func main() { + log.SetFlags(log.Lmicroseconds) + logFileName := fmt.Sprintf("%s.txt", time.Now().Format(strings.ReplaceAll(time.RFC1123Z, ":", "_"))) + f, err := os.OpenFile("./dump/"+logFileName, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0o644) + if err != nil { + log.Fatal(err) + } + + defer f.Close() + log.SetOutput(f) + + wg := &sync.WaitGroup{} + wg.Add(2) + + go func() { + defer wg.Done() + go_benchmark.Benchmark() + }() + go func() { + defer wg.Done() + nest_benchmark.Benchmark() + }() + + wg.Wait() +} diff --git a/benchmark/nest/main.go b/benchmark/nest/nest.go similarity index 85% rename from benchmark/nest/main.go rename to benchmark/nest/nest.go index fdffa8a..6aa72d4 100644 --- a/benchmark/nest/main.go +++ b/benchmark/nest/nest.go @@ -1,4 +1,4 @@ -package main +package nest_benchmark import ( "bytes" @@ -7,50 +7,11 @@ import ( "io" "log" "net/http" - "os" - "strings" "sync" - "time" "gitea.urkob.com/urko/ess-etl-go/pkg/crono" ) -func main() { - log.SetFlags(log.Lmicroseconds) - logFileName := fmt.Sprintf("%s.txt", time.Now().Format(strings.ReplaceAll(time.RFC1123Z, ":", "_"))) - f, err := os.OpenFile(logFileName, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0o644) - if err != nil { - log.Fatal(err) - } - - defer f.Close() - log.SetOutput(f) - - cr := crono.New() - defer cr.Table() - host := "http://127.0.0.1:3458/graphql" - - wg := &sync.WaitGroup{} - errChan := make(chan error, 1) - wg.Add(1) - go func(errChan chan error) { - defer wg.Done() - for i := 0; i < 1000; i++ { - wg.Add(1) - go doRequest(wg, host, 975153, errChan) - } - }(errChan) - - go func(errChan chan error) { - if err := <-errChan; err != nil { - log.Fatalln("error process request", err) - } - }(errChan) - - wg.Wait() - cr.MarkAndRestart("finish handle all response") -} - type GraphqlQuery struct { Query string `json:"query"` OperationName string `json:"operationName"` @@ -108,3 +69,31 @@ func doRequest(wg *sync.WaitGroup, host string, employeeID int, errChan chan err return nil } + +func Benchmark() { + cr := crono.New() + defer cr.Table() + + host := "http://127.0.0.1:3458/graphql" + + wg := &sync.WaitGroup{} + errChan := make(chan error, 1) + wg.Add(1) + totalRequests := 1000 + go func(errChan chan error) { + defer wg.Done() + for i := 0; i < totalRequests; i++ { + wg.Add(1) + go doRequest(wg, host, 975153, errChan) + } + }(errChan) + + go func(errChan chan error) { + if err := <-errChan; err != nil { + log.Fatalln("error process request", err) + } + }(errChan) + + wg.Wait() + cr.MarkAndRestart(fmt.Sprintf("nest handle %d response", totalRequests)) +}