From e48ad6108b8412163d05c3e2451164f630bd3a8d Mon Sep 17 00:00:00 2001 From: Urko Date: Sat, 8 Apr 2023 22:05:26 +0200 Subject: [PATCH] feat: add benchmark --- benchmark/benchmark_test.go | 37 +++++++++++++++++++++++++++++++++++-- benchmark/go/go.go | 14 +++++++++----- benchmark/nest/nest.go | 11 +++++++---- 3 files changed, 51 insertions(+), 11 deletions(-) diff --git a/benchmark/benchmark_test.go b/benchmark/benchmark_test.go index cffb62a..f321b4d 100644 --- a/benchmark/benchmark_test.go +++ b/benchmark/benchmark_test.go @@ -10,9 +10,42 @@ import ( go_benchmark "gitea.urkob.com/urko/ess-etl-go/benchmark/go" nest_benchmark "gitea.urkob.com/urko/ess-etl-go/benchmark/nest" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) +func BenchmarkGo(b *testing.B) { + require.NoError(b, os.RemoveAll("./dump")) + require.NoError(b, os.MkdirAll("./dump", os.ModeTemporary)) + 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) + require.NoError(b, err) + + defer f.Close() + log.SetOutput(f) + + for i := 0; i < b.N; i++ { + assert.NoError(b, go_benchmark.Benchmark(1, false)) + } + +} +func BenchmarkNest(b *testing.B) { + require.NoError(b, os.RemoveAll("./dump")) + require.NoError(b, os.MkdirAll("./dump", os.ModeTemporary)) + 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) + require.NoError(b, err) + + defer f.Close() + log.SetOutput(f) + + for i := 0; i < b.N; i++ { + assert.NoError(b, nest_benchmark.Benchmark(1, false)) + } +} + func TestGoXRequestes(t *testing.T) { log.SetFlags(log.Lmicroseconds) logFileName := fmt.Sprintf("%s.txt", time.Now().Format(strings.ReplaceAll(time.RFC1123Z, ":", "_"))) @@ -25,7 +58,7 @@ func TestGoXRequestes(t *testing.T) { log.SetOutput(f) totalRequests := 1000 - require.NoError(t, go_benchmark.Benchmark(totalRequests)) + require.NoError(t, go_benchmark.Benchmark(totalRequests, true)) require.NoError(t, os.RemoveAll("./temp")) } @@ -40,5 +73,5 @@ func TestNestXRequests(t *testing.T) { log.SetOutput(f) totalRequests := 1000 - require.NoError(t, nest_benchmark.Benchmark(totalRequests)) + require.NoError(t, nest_benchmark.Benchmark(totalRequests, true)) } diff --git a/benchmark/go/go.go b/benchmark/go/go.go index dd8863b..88c446d 100644 --- a/benchmark/go/go.go +++ b/benchmark/go/go.go @@ -44,21 +44,22 @@ func doRequest(wg *sync.WaitGroup, host, employeeID string, errChan chan error) return nil } -func Benchmark(totalRequests int) error { +func Benchmark(totalRequests int, dolog bool) error { cr := crono.New() - defer cr.Table() + if dolog { + defer cr.Table() + } host := "http://127.0.0.1:45654" wg := &sync.WaitGroup{} errChan := make(chan error, 1) - wg.Add(1) + wg.Add(totalRequests + 1) go func(errChan chan error) { defer wg.Done() defer func() { errChan <- nil }() for i := 0; i < totalRequests; i++ { - wg.Add(1) go doRequest(wg, host, "975135", errChan) } }(errChan) @@ -68,6 +69,9 @@ func Benchmark(totalRequests int) error { if err := <-errChan; err != nil { return fmt.Errorf("Benchmark: %s", err) } - cr.MarkAndRestart(fmt.Sprintf("go handle %d response", totalRequests)) + if dolog { + cr.MarkAndRestart(fmt.Sprintf("go handle %d response", totalRequests)) + } + return nil } diff --git a/benchmark/nest/nest.go b/benchmark/nest/nest.go index f6145b4..ca91609 100644 --- a/benchmark/nest/nest.go +++ b/benchmark/nest/nest.go @@ -70,10 +70,11 @@ func doRequest(wg *sync.WaitGroup, host string, employeeID int, errChan chan err return nil } -func Benchmark(totalRequests int) error { +func Benchmark(totalRequests int, dolog bool) error { cr := crono.New() - defer cr.Table() - + if dolog { + defer cr.Table() + } host := "http://127.0.0.1:3458/graphql" wg := &sync.WaitGroup{} @@ -95,6 +96,8 @@ func Benchmark(totalRequests int) error { if err := <-errChan; err != nil { return fmt.Errorf("Benchmark: %s", err) } - cr.MarkAndRestart(fmt.Sprintf("nest handle %d response", totalRequests)) + if dolog { + cr.MarkAndRestart(fmt.Sprintf("nest handle %d response", totalRequests)) + } return nil }