ess-etl-go/benchmark/benchmark_test.go

156 lines
4.3 KiB
Go
Raw Normal View History

2023-04-05 21:35:06 +02:00
package benchmark
import (
"fmt"
"log"
"os"
"strings"
2023-04-10 13:01:19 +02:00
"sync"
2023-04-05 21:35:06 +02:00
"testing"
"time"
go_benchmark "gitea.urkob.com/urko/ess-etl-go/benchmark/go"
nest_benchmark "gitea.urkob.com/urko/ess-etl-go/benchmark/nest"
2023-04-24 11:55:33 +02:00
"gitea.urkob.com/urko/ess-etl-go/config"
2023-04-08 22:05:26 +02:00
"github.com/stretchr/testify/assert"
2023-04-05 21:35:06 +02:00
"github.com/stretchr/testify/require"
)
2023-04-10 13:01:19 +02:00
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))
}
}
2023-04-24 11:55:33 +02:00
const (
bmNumberOfRequests = 100
numberOfRequestsAtOnce = 1000
seconds = 15
)
2023-04-10 13:01:19 +02:00
2023-04-08 22:05:26 +02:00
func BenchmarkGo(b *testing.B) {
log.SetFlags(log.Lmicroseconds)
2023-04-10 13:01:19 +02:00
logFileName := fmt.Sprintf("BenchmarkGo-%s.txt", time.Now().Format(strings.ReplaceAll(time.RFC1123Z, ":", "_")))
2023-04-08 22:05:26 +02:00
f, err := os.OpenFile("./dump/"+logFileName, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0o644)
require.NoError(b, err)
2023-04-24 11:55:33 +02:00
cfg := config.NewConfig(".env")
host := cfg.TestGoHost
employeeID := cfg.TestGoEmployee
2023-04-08 22:05:26 +02:00
defer f.Close()
log.SetOutput(f)
2023-04-10 13:01:19 +02:00
b.ResetTimer()
2023-04-08 22:05:26 +02:00
for i := 0; i < b.N; i++ {
2023-04-10 13:01:19 +02:00
start := time.Now()
2023-04-24 11:55:33 +02:00
assert.NoError(b, go_benchmark.BenchmarkNoLog(host, employeeID, bmNumberOfRequests))
2023-04-10 13:01:19 +02:00
b.Logf("Request took %v", time.Since(start))
b.Logf("i %d | b.N %d", i, b.N)
2023-04-08 22:05:26 +02:00
}
}
func BenchmarkNest(b *testing.B) {
log.SetFlags(log.Lmicroseconds)
2023-04-10 13:01:19 +02:00
logFileName := fmt.Sprintf("BenchmarkNest-%s.txt", time.Now().Format(strings.ReplaceAll(time.RFC1123Z, ":", "_")))
2023-04-08 22:05:26 +02:00
f, err := os.OpenFile("./dump/"+logFileName, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0o644)
require.NoError(b, err)
2023-04-24 11:55:33 +02:00
cfg := config.NewConfig(".env")
host := cfg.TestGoHost
employeeID := cfg.TestNestEmployee
2023-04-08 22:05:26 +02:00
defer f.Close()
log.SetOutput(f)
2023-04-10 13:01:19 +02:00
b.ResetTimer()
2023-04-08 22:05:26 +02:00
for i := 0; i < b.N; i++ {
2023-04-10 13:01:19 +02:00
start := time.Now()
2023-04-24 11:55:33 +02:00
assert.NoError(b, nest_benchmark.BenchmarkNoLog(host, employeeID, bmNumberOfRequests))
2023-04-10 13:01:19 +02:00
b.Logf("Request took %v", time.Since(start))
b.Logf("i %d | b.N %d", i, b.N)
2023-04-08 22:05:26 +02:00
}
}
2023-04-10 13:01:19 +02:00
func TestGoRequestsPerSecondFor15s(t *testing.T) {
2023-04-05 21:35:06 +02:00
log.SetFlags(log.Lmicroseconds)
2023-04-10 13:01:19 +02:00
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)
2023-04-24 11:55:33 +02:00
cfg := config.NewConfig(".env")
host := cfg.TestGoHost
employeeID := cfg.TestGoEmployee
2023-04-10 13:01:19 +02:00
defer f.Close()
log.SetOutput(f)
var wg sync.WaitGroup
wg.Add(seconds)
for i := 0; i < seconds; i++ {
go func() {
defer wg.Done()
2023-04-24 11:55:33 +02:00
require.NoError(t, go_benchmark.Benchmark(host, employeeID, numberOfRequestsAtOnce))
2023-04-10 13:01:19 +02:00
}()
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)
2023-04-05 21:35:06 +02:00
require.NoError(t, err)
2023-04-10 13:01:19 +02:00
2023-04-24 11:55:33 +02:00
cfg := config.NewConfig(".env")
host := cfg.TestGoHost
employeeID := cfg.TestNestEmployee
2023-04-10 13:01:19 +02:00
defer f.Close()
log.SetOutput(f)
var wg sync.WaitGroup
wg.Add(seconds)
for i := 0; i < seconds; i++ {
go func() {
defer wg.Done()
2023-04-24 11:55:33 +02:00
require.NoError(t, nest_benchmark.Benchmark(host, employeeID, numberOfRequestsAtOnce))
2023-04-10 13:01:19 +02:00
}()
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, ":", "_")))
2023-04-05 21:35:06 +02:00
f, err := os.OpenFile("./dump/"+logFileName, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0o644)
require.NoError(t, err)
2023-04-24 11:55:33 +02:00
cfg := config.NewConfig(".env")
host := cfg.TestGoHost
employeeID := cfg.TestGoEmployee
2023-04-05 21:35:06 +02:00
defer f.Close()
log.SetOutput(f)
2023-04-24 11:55:33 +02:00
require.NoError(t, go_benchmark.Benchmark(host, employeeID, numberOfRequestsAtOnce))
2023-04-05 21:35:06 +02:00
}
2023-04-10 13:01:19 +02:00
func TestNestRequests(t *testing.T) {
2023-04-05 21:35:06 +02:00
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)
2023-04-24 11:55:33 +02:00
cfg := config.NewConfig(".env")
host := cfg.TestGoHost
employeeID := cfg.TestNestEmployee
2023-04-05 21:35:06 +02:00
defer f.Close()
log.SetOutput(f)
2023-04-24 11:55:33 +02:00
require.NoError(t, nest_benchmark.Benchmark(host, employeeID, numberOfRequestsAtOnce))
2023-04-05 21:35:06 +02:00
}