ess-etl-go/benchmark/benchmark_test.go

78 lines
2.2 KiB
Go

package benchmark
import (
"fmt"
"log"
"os"
"strings"
"testing"
"time"
go_benchmark "gitea.urkob.com/urko/ess-etl-go/benchmark/go"
nest_benchmark "gitea.urkob.com/urko/ess-etl-go/benchmark/nest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
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, ":", "_")))
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)
for i := 0; i < b.N; i++ {
assert.NoError(b, go_benchmark.BenchmarkNoLog(1))
}
}
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, ":", "_")))
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)
for i := 0; i < b.N; i++ {
assert.NoError(b, nest_benchmark.BenchmarkNoLog(1))
}
}
func TestGoXRequestes(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)
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))
require.NoError(t, os.RemoveAll("./temp"))
}
func TestNestXRequests(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)
totalRequests := 1000
require.NoError(t, nest_benchmark.Benchmark(totalRequests))
}