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-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))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const bmNumberOfRequests = 100
|
|
|
|
const numberOfRequestsAtOnce = 1000
|
|
|
|
const seconds = 15
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
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()
|
|
|
|
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)
|
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)
|
|
|
|
|
|
|
|
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()
|
|
|
|
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)
|
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)
|
|
|
|
|
|
|
|
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(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)
|
2023-04-05 21:35:06 +02:00
|
|
|
require.NoError(t, err)
|
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()
|
|
|
|
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, ":", "_")))
|
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)
|
|
|
|
|
|
|
|
defer f.Close()
|
|
|
|
log.SetOutput(f)
|
|
|
|
|
2023-04-10 13:01:19 +02:00
|
|
|
require.NoError(t, go_benchmark.Benchmark(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)
|
|
|
|
|
|
|
|
defer f.Close()
|
|
|
|
log.SetOutput(f)
|
|
|
|
|
2023-04-10 13:01:19 +02:00
|
|
|
require.NoError(t, nest_benchmark.Benchmark(numberOfRequestsAtOnce))
|
2023-04-05 21:35:06 +02:00
|
|
|
}
|