feat: improve benchmark tests
This commit is contained in:
parent
4c8da4777d
commit
b0960d89df
2
Makefile
2
Makefile
|
@ -19,6 +19,8 @@ pprof:
|
|||
go tool pprof -alloc_space profile.out
|
||||
pprof_url:# top 40 -cum
|
||||
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:
|
||||
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
|
||||
|
|
|
@ -5,6 +5,7 @@ import (
|
|||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
|
@ -14,56 +15,108 @@ import (
|
|||
"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) {
|
||||
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, ":", "_")))
|
||||
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)
|
||||
|
||||
defer f.Close()
|
||||
log.SetOutput(f)
|
||||
|
||||
b.ResetTimer()
|
||||
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) {
|
||||
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, ":", "_")))
|
||||
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)
|
||||
|
||||
defer f.Close()
|
||||
log.SetOutput(f)
|
||||
|
||||
b.ResetTimer()
|
||||
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)
|
||||
logFileName := fmt.Sprintf("%s.txt", time.Now().Format(strings.ReplaceAll(time.RFC1123Z, ":", "_")))
|
||||
err := os.MkdirAll("./dump", os.ModeTemporary)
|
||||
require.NoError(t, err)
|
||||
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)
|
||||
|
||||
defer f.Close()
|
||||
log.SetOutput(f)
|
||||
|
||||
totalRequests := 1000
|
||||
require.NoError(t, go_benchmark.Benchmark(totalRequests))
|
||||
var wg sync.WaitGroup
|
||||
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"))
|
||||
|
||||
}
|
||||
|
||||
func TestNestXRequests(t *testing.T) {
|
||||
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)
|
||||
|
@ -72,6 +125,5 @@ func TestNestXRequests(t *testing.T) {
|
|||
defer f.Close()
|
||||
log.SetOutput(f)
|
||||
|
||||
totalRequests := 1000
|
||||
require.NoError(t, nest_benchmark.Benchmark(totalRequests))
|
||||
require.NoError(t, nest_benchmark.Benchmark(numberOfRequestsAtOnce))
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue