package benchmark import ( "fmt" "log" "os" "strings" "sync" "testing" "time" go_benchmark "gitea.urkob.com/urko/ess-etl-go/benchmark/go" nest_benchmark "gitea.urkob.com/urko/ess-etl-go/benchmark/nest" "gitea.urkob.com/urko/ess-etl-go/config" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) func init() { if err := os.RemoveAll("./dump"); err != nil { panic(fmt.Errorf("os.RemoveAll: %s", err)) } if err := os.MkdirAll("./dump", os.ModeTemporary); err != nil { panic(fmt.Errorf("os.MkdirAll: %s", err)) } } const ( bmNumberOfRequests = 100 numberOfRequestsAtOnce = 1000 seconds = 15 ) func BenchmarkGo(b *testing.B) { log.SetFlags(log.Lmicroseconds) logFileName := fmt.Sprintf("BenchmarkGo-%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) cfg := config.NewConfig(".env") host := cfg.TestGoHost employeeID := cfg.TestGoEmployee defer f.Close() log.SetOutput(f) b.ResetTimer() for i := 0; i < b.N; i++ { start := time.Now() assert.NoError(b, go_benchmark.BenchmarkNoLog(host, employeeID, bmNumberOfRequests)) b.Logf("Request took %v", time.Since(start)) b.Logf("i %d | b.N %d", i, b.N) } } func BenchmarkNest(b *testing.B) { log.SetFlags(log.Lmicroseconds) logFileName := fmt.Sprintf("BenchmarkNest-%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) cfg := config.NewConfig(".env") host := cfg.TestGoHost employeeID := cfg.TestNestEmployee defer f.Close() log.SetOutput(f) b.ResetTimer() for i := 0; i < b.N; i++ { start := time.Now() assert.NoError(b, nest_benchmark.BenchmarkNoLog(host, employeeID, bmNumberOfRequests)) b.Logf("Request took %v", time.Since(start)) b.Logf("i %d | b.N %d", i, b.N) } } func TestGoRequestsPerSecondFor15s(t *testing.T) { log.SetFlags(log.Lmicroseconds) logFileName := fmt.Sprintf("GoRequestsPerSecondFor15s-%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(t, err) cfg := config.NewConfig(".env") host := cfg.TestGoHost employeeID := cfg.TestGoEmployee defer f.Close() log.SetOutput(f) var wg sync.WaitGroup wg.Add(seconds) for i := 0; i < seconds; i++ { go func() { defer wg.Done() require.NoError(t, go_benchmark.Benchmark(host, employeeID, numberOfRequestsAtOnce)) }() time.Sleep(time.Second) } wg.Wait() } func TestNestRequestsPerSecondFor15s(t *testing.T) { log.SetFlags(log.Lmicroseconds) logFileName := fmt.Sprintf("NestRequestsPerSecondFor15s-%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(t, err) cfg := config.NewConfig(".env") host := cfg.TestGoHost employeeID := cfg.TestNestEmployee defer f.Close() log.SetOutput(f) var wg sync.WaitGroup wg.Add(seconds) for i := 0; i < seconds; i++ { go func() { defer wg.Done() require.NoError(t, nest_benchmark.Benchmark(host, employeeID, numberOfRequestsAtOnce)) }() time.Sleep(time.Second) } wg.Wait() } func TestGoRequests(t *testing.T) { 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(t, err) cfg := config.NewConfig(".env") host := cfg.TestGoHost employeeID := cfg.TestGoEmployee defer f.Close() log.SetOutput(f) require.NoError(t, go_benchmark.Benchmark(host, employeeID, numberOfRequestsAtOnce)) } func TestNestRequests(t *testing.T) { 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(t, err) cfg := config.NewConfig(".env") host := cfg.TestGoHost employeeID := cfg.TestNestEmployee defer f.Close() log.SetOutput(f) require.NoError(t, nest_benchmark.Benchmark(host, employeeID, numberOfRequestsAtOnce)) }