feat: improve benchmark tests

This commit is contained in:
Urko 2023-04-10 13:01:19 +02:00
parent 4c8da4777d
commit b0960d89df
2 changed files with 75 additions and 21 deletions

View File

@ -19,6 +19,8 @@ pprof:
go tool pprof -alloc_space profile.out go tool pprof -alloc_space profile.out
pprof_url:# top 40 -cum pprof_url:# top 40 -cum
go tool pprof -alloc_space http://localhost:5000/debug/pprof/allocs go tool pprof -alloc_space http://localhost:5000/debug/pprof/allocs
benchmark_server:
go test -gcflags "-m -m" -run none -bench . -benchtime 30s -benchmem -memprofile profile.out ./benchmark
benchmark_etl: benchmark_etl:
go test -gcflags "-m -m" -run none -bench . -benchtime 5s -benchmem -memprofile profile.out ./internal/etl/ go test -gcflags "-m -m" -run none -bench . -benchtime 5s -benchmem -memprofile profile.out ./internal/etl/
trace_etl: build_etl# go tool trace t.out trace_etl: build_etl# go tool trace t.out

View File

@ -5,6 +5,7 @@ import (
"log" "log"
"os" "os"
"strings" "strings"
"sync"
"testing" "testing"
"time" "time"
@ -14,56 +15,108 @@ import (
"github.com/stretchr/testify/require" "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
const numberOfRequestsAtOnce = 1000
const seconds = 15
func BenchmarkGo(b *testing.B) { func BenchmarkGo(b *testing.B) {
require.NoError(b, os.RemoveAll("./dump"))
require.NoError(b, os.MkdirAll("./dump", os.ModeTemporary))
log.SetFlags(log.Lmicroseconds) log.SetFlags(log.Lmicroseconds)
logFileName := fmt.Sprintf("%s.txt", time.Now().Format(strings.ReplaceAll(time.RFC1123Z, ":", "_"))) 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) f, err := os.OpenFile("./dump/"+logFileName, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0o644)
require.NoError(b, err) require.NoError(b, err)
defer f.Close() defer f.Close()
log.SetOutput(f) log.SetOutput(f)
b.ResetTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
assert.NoError(b, go_benchmark.BenchmarkNoLog(1)) start := time.Now()
assert.NoError(b, go_benchmark.BenchmarkNoLog(bmNumberOfRequests))
b.Logf("Request took %v", time.Since(start))
b.Logf("i %d | b.N %d", i, b.N)
} }
} }
func BenchmarkNest(b *testing.B) { func BenchmarkNest(b *testing.B) {
require.NoError(b, os.RemoveAll("./dump"))
require.NoError(b, os.MkdirAll("./dump", os.ModeTemporary))
log.SetFlags(log.Lmicroseconds) log.SetFlags(log.Lmicroseconds)
logFileName := fmt.Sprintf("%s.txt", time.Now().Format(strings.ReplaceAll(time.RFC1123Z, ":", "_"))) 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) f, err := os.OpenFile("./dump/"+logFileName, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0o644)
require.NoError(b, err) require.NoError(b, err)
defer f.Close() defer f.Close()
log.SetOutput(f) log.SetOutput(f)
b.ResetTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
assert.NoError(b, nest_benchmark.BenchmarkNoLog(1)) start := time.Now()
assert.NoError(b, nest_benchmark.BenchmarkNoLog(bmNumberOfRequests))
b.Logf("Request took %v", time.Since(start))
b.Logf("i %d | b.N %d", i, b.N)
} }
} }
func TestGoXRequestes(t *testing.T) { func TestGoRequestsPerSecondFor15s(t *testing.T) {
log.SetFlags(log.Lmicroseconds) log.SetFlags(log.Lmicroseconds)
logFileName := fmt.Sprintf("%s.txt", time.Now().Format(strings.ReplaceAll(time.RFC1123Z, ":", "_"))) logFileName := fmt.Sprintf("GoRequestsPerSecondFor15s-%s.txt", time.Now().Format(strings.ReplaceAll(time.RFC1123Z, ":", "_")))
err := os.MkdirAll("./dump", os.ModeTemporary)
require.NoError(t, err)
f, err := os.OpenFile("./dump/"+logFileName, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0o644) f, err := os.OpenFile("./dump/"+logFileName, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0o644)
require.NoError(t, err) require.NoError(t, err)
defer f.Close() defer f.Close()
log.SetOutput(f) log.SetOutput(f)
totalRequests := 1000 var wg sync.WaitGroup
require.NoError(t, go_benchmark.Benchmark(totalRequests)) wg.Add(seconds)
for i := 0; i < seconds; i++ {
go func() {
defer wg.Done()
require.NoError(t, go_benchmark.Benchmark(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)
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(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)
defer f.Close()
log.SetOutput(f)
require.NoError(t, go_benchmark.Benchmark(numberOfRequestsAtOnce))
require.NoError(t, os.RemoveAll("./temp")) require.NoError(t, os.RemoveAll("./temp"))
} }
func TestNestXRequests(t *testing.T) { func TestNestRequests(t *testing.T) {
log.SetFlags(log.Lmicroseconds) log.SetFlags(log.Lmicroseconds)
logFileName := fmt.Sprintf("%s.txt", time.Now().Format(strings.ReplaceAll(time.RFC1123Z, ":", "_"))) 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) f, err := os.OpenFile("./dump/"+logFileName, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0o644)
@ -72,6 +125,5 @@ func TestNestXRequests(t *testing.T) {
defer f.Close() defer f.Close()
log.SetOutput(f) log.SetOutput(f)
totalRequests := 1000 require.NoError(t, nest_benchmark.Benchmark(numberOfRequestsAtOnce))
require.NoError(t, nest_benchmark.Benchmark(totalRequests))
} }