feat: add benchmark

This commit is contained in:
Urko 2023-04-08 22:05:26 +02:00
parent 47dc2a2ffb
commit e48ad6108b
3 changed files with 51 additions and 11 deletions

View File

@ -10,9 +10,42 @@ import (
go_benchmark "gitea.urkob.com/urko/ess-etl-go/benchmark/go" go_benchmark "gitea.urkob.com/urko/ess-etl-go/benchmark/go"
nest_benchmark "gitea.urkob.com/urko/ess-etl-go/benchmark/nest" nest_benchmark "gitea.urkob.com/urko/ess-etl-go/benchmark/nest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "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.Benchmark(1, false))
}
}
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.Benchmark(1, false))
}
}
func TestGoXRequestes(t *testing.T) { func TestGoXRequestes(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, ":", "_")))
@ -25,7 +58,7 @@ func TestGoXRequestes(t *testing.T) {
log.SetOutput(f) log.SetOutput(f)
totalRequests := 1000 totalRequests := 1000
require.NoError(t, go_benchmark.Benchmark(totalRequests)) require.NoError(t, go_benchmark.Benchmark(totalRequests, true))
require.NoError(t, os.RemoveAll("./temp")) require.NoError(t, os.RemoveAll("./temp"))
} }
@ -40,5 +73,5 @@ func TestNestXRequests(t *testing.T) {
log.SetOutput(f) log.SetOutput(f)
totalRequests := 1000 totalRequests := 1000
require.NoError(t, nest_benchmark.Benchmark(totalRequests)) require.NoError(t, nest_benchmark.Benchmark(totalRequests, true))
} }

View File

@ -44,21 +44,22 @@ func doRequest(wg *sync.WaitGroup, host, employeeID string, errChan chan error)
return nil return nil
} }
func Benchmark(totalRequests int) error { func Benchmark(totalRequests int, dolog bool) error {
cr := crono.New() cr := crono.New()
if dolog {
defer cr.Table() defer cr.Table()
}
host := "http://127.0.0.1:45654" host := "http://127.0.0.1:45654"
wg := &sync.WaitGroup{} wg := &sync.WaitGroup{}
errChan := make(chan error, 1) errChan := make(chan error, 1)
wg.Add(1) wg.Add(totalRequests + 1)
go func(errChan chan error) { go func(errChan chan error) {
defer wg.Done() defer wg.Done()
defer func() { defer func() {
errChan <- nil errChan <- nil
}() }()
for i := 0; i < totalRequests; i++ { for i := 0; i < totalRequests; i++ {
wg.Add(1)
go doRequest(wg, host, "975135", errChan) go doRequest(wg, host, "975135", errChan)
} }
}(errChan) }(errChan)
@ -68,6 +69,9 @@ func Benchmark(totalRequests int) error {
if err := <-errChan; err != nil { if err := <-errChan; err != nil {
return fmt.Errorf("Benchmark: %s", err) return fmt.Errorf("Benchmark: %s", err)
} }
if dolog {
cr.MarkAndRestart(fmt.Sprintf("go handle %d response", totalRequests)) cr.MarkAndRestart(fmt.Sprintf("go handle %d response", totalRequests))
}
return nil return nil
} }

View File

@ -70,10 +70,11 @@ func doRequest(wg *sync.WaitGroup, host string, employeeID int, errChan chan err
return nil return nil
} }
func Benchmark(totalRequests int) error { func Benchmark(totalRequests int, dolog bool) error {
cr := crono.New() cr := crono.New()
if dolog {
defer cr.Table() defer cr.Table()
}
host := "http://127.0.0.1:3458/graphql" host := "http://127.0.0.1:3458/graphql"
wg := &sync.WaitGroup{} wg := &sync.WaitGroup{}
@ -95,6 +96,8 @@ func Benchmark(totalRequests int) error {
if err := <-errChan; err != nil { if err := <-errChan; err != nil {
return fmt.Errorf("Benchmark: %s", err) return fmt.Errorf("Benchmark: %s", err)
} }
if dolog {
cr.MarkAndRestart(fmt.Sprintf("nest handle %d response", totalRequests)) cr.MarkAndRestart(fmt.Sprintf("nest handle %d response", totalRequests))
}
return nil return nil
} }