refactor: improve benchmark to do both at same time

This commit is contained in:
Urko 2023-04-05 14:00:39 +02:00
parent d68c033726
commit 8271f8eb19
4 changed files with 92 additions and 74 deletions

2
.gitignore vendored
View File

@ -100,3 +100,5 @@ sw.*
# Vim swap files
*.swp
dump

View File

@ -1,48 +1,15 @@
package main
package go_benchmark
import (
"fmt"
"io"
"log"
"net/http"
"os"
"strings"
"sync"
"time"
"gitea.urkob.com/urko/ess-etl-go/pkg/crono"
)
func main() {
log.SetFlags(log.Lmicroseconds)
logFileName := fmt.Sprintf("%s.txt", time.Now().Format(strings.ReplaceAll(time.RFC1123Z, ":", "_")))
f, err := os.OpenFile(logFileName, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0o644)
if err != nil {
log.Fatal(err)
}
defer f.Close()
log.SetOutput(f)
cr := crono.New()
defer cr.Table()
host := "http://127.0.0.1:45654"
wg := &sync.WaitGroup{}
errChan := make(chan error, 1)
wg.Add(1)
go func(errChan chan error) {
defer wg.Done()
for i := 0; i < 1000; i++ {
wg.Add(1)
go doRequest(wg, host, "975135", errChan)
}
}(errChan)
wg.Wait()
cr.MarkAndRestart("finish handle all response")
}
func doRequest(wg *sync.WaitGroup, host, employeeID string, errChan chan error) error {
defer wg.Done()
url := host + "/employee_wi/" + employeeID
@ -76,3 +43,24 @@ func doRequest(wg *sync.WaitGroup, host, employeeID string, errChan chan error)
return nil
}
func Benchmark() {
cr := crono.New()
defer cr.Table()
host := "http://127.0.0.1:45654"
wg := &sync.WaitGroup{}
errChan := make(chan error, 1)
wg.Add(1)
totalRequests := 1000
go func(errChan chan error) {
defer wg.Done()
for i := 0; i < totalRequests; i++ {
wg.Add(1)
go doRequest(wg, host, "975135", errChan)
}
}(errChan)
wg.Wait()
cr.MarkAndRestart(fmt.Sprintf("go handle %d response", totalRequests))
}

39
benchmark/main.go Normal file
View File

@ -0,0 +1,39 @@
package main
import (
"fmt"
"log"
"os"
"strings"
"sync"
"time"
go_benchmark "gitea.urkob.com/urko/ess-etl-go/benchmark/go"
nest_benchmark "gitea.urkob.com/urko/ess-etl-go/benchmark/nest"
)
func main() {
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)
if err != nil {
log.Fatal(err)
}
defer f.Close()
log.SetOutput(f)
wg := &sync.WaitGroup{}
wg.Add(2)
go func() {
defer wg.Done()
go_benchmark.Benchmark()
}()
go func() {
defer wg.Done()
nest_benchmark.Benchmark()
}()
wg.Wait()
}

View File

@ -1,4 +1,4 @@
package main
package nest_benchmark
import (
"bytes"
@ -7,50 +7,11 @@ import (
"io"
"log"
"net/http"
"os"
"strings"
"sync"
"time"
"gitea.urkob.com/urko/ess-etl-go/pkg/crono"
)
func main() {
log.SetFlags(log.Lmicroseconds)
logFileName := fmt.Sprintf("%s.txt", time.Now().Format(strings.ReplaceAll(time.RFC1123Z, ":", "_")))
f, err := os.OpenFile(logFileName, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0o644)
if err != nil {
log.Fatal(err)
}
defer f.Close()
log.SetOutput(f)
cr := crono.New()
defer cr.Table()
host := "http://127.0.0.1:3458/graphql"
wg := &sync.WaitGroup{}
errChan := make(chan error, 1)
wg.Add(1)
go func(errChan chan error) {
defer wg.Done()
for i := 0; i < 1000; i++ {
wg.Add(1)
go doRequest(wg, host, 975153, errChan)
}
}(errChan)
go func(errChan chan error) {
if err := <-errChan; err != nil {
log.Fatalln("error process request", err)
}
}(errChan)
wg.Wait()
cr.MarkAndRestart("finish handle all response")
}
type GraphqlQuery struct {
Query string `json:"query"`
OperationName string `json:"operationName"`
@ -108,3 +69,31 @@ func doRequest(wg *sync.WaitGroup, host string, employeeID int, errChan chan err
return nil
}
func Benchmark() {
cr := crono.New()
defer cr.Table()
host := "http://127.0.0.1:3458/graphql"
wg := &sync.WaitGroup{}
errChan := make(chan error, 1)
wg.Add(1)
totalRequests := 1000
go func(errChan chan error) {
defer wg.Done()
for i := 0; i < totalRequests; i++ {
wg.Add(1)
go doRequest(wg, host, 975153, errChan)
}
}(errChan)
go func(errChan chan error) {
if err := <-errChan; err != nil {
log.Fatalln("error process request", err)
}
}(errChan)
wg.Wait()
cr.MarkAndRestart(fmt.Sprintf("nest handle %d response", totalRequests))
}