feat: remove hardcoded values

This commit is contained in:
Urko 2023-04-24 11:55:33 +02:00
parent 9fe42609bb
commit d41c7fb4a5
6 changed files with 54 additions and 25 deletions

View File

@ -11,6 +11,7 @@ import (
go_benchmark "gitea.urkob.com/urko/ess-etl-go/benchmark/go"
nest_benchmark "gitea.urkob.com/urko/ess-etl-go/benchmark/nest"
"gitea.urkob.com/urko/ess-etl-go/config"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
@ -24,9 +25,11 @@ func init() {
}
}
const bmNumberOfRequests = 100
const numberOfRequestsAtOnce = 1000
const seconds = 15
const (
bmNumberOfRequests = 100
numberOfRequestsAtOnce = 1000
seconds = 15
)
func BenchmarkGo(b *testing.B) {
log.SetFlags(log.Lmicroseconds)
@ -34,12 +37,16 @@ func BenchmarkGo(b *testing.B) {
f, err := os.OpenFile("./dump/"+logFileName, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0o644)
require.NoError(b, err)
cfg := config.NewConfig(".env")
host := cfg.TestGoHost
employeeID := cfg.TestGoEmployee
defer f.Close()
log.SetOutput(f)
b.ResetTimer()
for i := 0; i < b.N; i++ {
start := time.Now()
assert.NoError(b, go_benchmark.BenchmarkNoLog(bmNumberOfRequests))
assert.NoError(b, go_benchmark.BenchmarkNoLog(host, employeeID, bmNumberOfRequests))
b.Logf("Request took %v", time.Since(start))
b.Logf("i %d | b.N %d", i, b.N)
}
@ -50,12 +57,16 @@ func BenchmarkNest(b *testing.B) {
f, err := os.OpenFile("./dump/"+logFileName, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0o644)
require.NoError(b, err)
cfg := config.NewConfig(".env")
host := cfg.TestGoHost
employeeID := cfg.TestNestEmployee
defer f.Close()
log.SetOutput(f)
b.ResetTimer()
for i := 0; i < b.N; i++ {
start := time.Now()
assert.NoError(b, nest_benchmark.BenchmarkNoLog(bmNumberOfRequests))
assert.NoError(b, nest_benchmark.BenchmarkNoLog(host, employeeID, bmNumberOfRequests))
b.Logf("Request took %v", time.Since(start))
b.Logf("i %d | b.N %d", i, b.N)
}
@ -67,6 +78,10 @@ func TestGoRequestsPerSecondFor15s(t *testing.T) {
f, err := os.OpenFile("./dump/"+logFileName, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0o644)
require.NoError(t, err)
cfg := config.NewConfig(".env")
host := cfg.TestGoHost
employeeID := cfg.TestGoEmployee
defer f.Close()
log.SetOutput(f)
@ -75,7 +90,7 @@ func TestGoRequestsPerSecondFor15s(t *testing.T) {
for i := 0; i < seconds; i++ {
go func() {
defer wg.Done()
require.NoError(t, go_benchmark.Benchmark(numberOfRequestsAtOnce))
require.NoError(t, go_benchmark.Benchmark(host, employeeID, numberOfRequestsAtOnce))
}()
time.Sleep(time.Second)
}
@ -88,6 +103,10 @@ func TestNestRequestsPerSecondFor15s(t *testing.T) {
f, err := os.OpenFile("./dump/"+logFileName, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0o644)
require.NoError(t, err)
cfg := config.NewConfig(".env")
host := cfg.TestGoHost
employeeID := cfg.TestNestEmployee
defer f.Close()
log.SetOutput(f)
@ -96,7 +115,7 @@ func TestNestRequestsPerSecondFor15s(t *testing.T) {
for i := 0; i < seconds; i++ {
go func() {
defer wg.Done()
require.NoError(t, nest_benchmark.Benchmark(numberOfRequestsAtOnce))
require.NoError(t, nest_benchmark.Benchmark(host, employeeID, numberOfRequestsAtOnce))
}()
time.Sleep(time.Second)
}
@ -109,10 +128,14 @@ func TestGoRequests(t *testing.T) {
f, err := os.OpenFile("./dump/"+logFileName, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0o644)
require.NoError(t, err)
cfg := config.NewConfig(".env")
host := cfg.TestGoHost
employeeID := cfg.TestGoEmployee
defer f.Close()
log.SetOutput(f)
require.NoError(t, go_benchmark.Benchmark(numberOfRequestsAtOnce))
require.NoError(t, go_benchmark.Benchmark(host, employeeID, numberOfRequestsAtOnce))
}
func TestNestRequests(t *testing.T) {
@ -121,8 +144,12 @@ func TestNestRequests(t *testing.T) {
f, err := os.OpenFile("./dump/"+logFileName, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0o644)
require.NoError(t, err)
cfg := config.NewConfig(".env")
host := cfg.TestGoHost
employeeID := cfg.TestNestEmployee
defer f.Close()
log.SetOutput(f)
require.NoError(t, nest_benchmark.Benchmark(numberOfRequestsAtOnce))
require.NoError(t, nest_benchmark.Benchmark(host, employeeID, numberOfRequestsAtOnce))
}

View File

@ -44,19 +44,18 @@ func doRequest(wg *sync.WaitGroup, host, employeeID string, errChan chan error)
return nil
}
func BenchmarkNoLog(totalRequests int) error {
return benchmark(totalRequests, false)
func BenchmarkNoLog(host, employeeID string, totalRequests int) error {
return benchmark(host, employeeID, totalRequests, false)
}
func Benchmark(totalRequests int) error {
return benchmark(totalRequests, true)
func Benchmark(host, employeeID string, totalRequests int) error {
return benchmark(host, employeeID, totalRequests, true)
}
func benchmark(totalRequests int, dolog bool) error {
func benchmark(host, employeeID string, totalRequests int, dolog bool) error {
cr := crono.New()
if dolog {
defer cr.Table()
}
host := "http://127.0.0.1:45654"
wg := &sync.WaitGroup{}
errChan := make(chan error, 1)
@ -67,7 +66,7 @@ func benchmark(totalRequests int, dolog bool) error {
errChan <- nil
}()
for i := 0; i < totalRequests; i++ {
go doRequest(wg, host, "975135", errChan)
go doRequest(wg, host, employeeID, errChan)
}
}(errChan)

View File

@ -70,20 +70,19 @@ func doRequest(wg *sync.WaitGroup, host string, employeeID int, errChan chan err
return nil
}
func BenchmarkNoLog(totalRequests int) error {
return benchmark(totalRequests, false)
func BenchmarkNoLog(host string, employeeNumber, totalRequests int) error {
return benchmark(host, employeeNumber, totalRequests, false)
}
func Benchmark(totalRequests int) error {
return benchmark(totalRequests, true)
func Benchmark(host string, employeeNumber, totalRequests int) error {
return benchmark(host, employeeNumber, totalRequests, true)
}
func benchmark(totalRequests int, dolog bool) error {
func benchmark(host string, employeeNumber int, totalRequests int, dolog bool) error {
cr := crono.New()
if dolog {
defer cr.Table()
}
host := "http://127.0.0.1:3458/graphql"
wg := &sync.WaitGroup{}
errChan := make(chan error, 1)
@ -95,7 +94,7 @@ func benchmark(totalRequests int, dolog bool) error {
}()
for i := 0; i < totalRequests; i++ {
wg.Add(1)
go doRequest(wg, host, 975153, errChan)
go doRequest(wg, host, employeeNumber, errChan)
}
}(errChan)

View File

@ -77,7 +77,7 @@ func main() {
restServer := apihttp.
NewRestServer(cfg, cr).
WithEmployeeWIHandler(services.NewEmployeeWIService(ctx, professionalRepo)).
WithAMSHander()
WithAMSHandler()
cr.MarkAndRestart("dependencies loaded")
go func() {

View File

@ -20,6 +20,10 @@ type Config struct {
DbName string `required:"true" split_words:"true"`
EmployeeWorkInformationCollection string `required:"true" split_words:"true"`
EmployeeIdList []string `required:"true" split_words:"true"`
TestGoHost string `required:"true" split_words:"true"`
TestGoEmployee string `required:"true" split_words:"true"`
TestNestHost string `required:"true" split_words:"true"`
TestNestEmployee int `required:"true" split_words:"true"`
}
func NewConfig(envFile string) *Config {

View File

@ -33,7 +33,7 @@ func (s *restServer) WithEmployeeWIHandler(employeeWISrv services.EmployeeWIServ
return s
}
func (s *restServer) WithAMSHander() *restServer {
func (s *restServer) WithAMSHandler() *restServer {
s.amsEmployeeWIHdl = handler.NewAMSEmployeeWIHandler(s.cfg.AmsApi, s.cfg.AmsApiKey)
return s
}