From d68c033726d4981f532e4dbb038841b980750a9e Mon Sep 17 00:00:00 2001 From: Urko Date: Wed, 5 Apr 2023 12:33:37 +0200 Subject: [PATCH] feat: log benchmark response into file --- benchmark/go/main.go | 24 ++++++++++++++++++++++++ benchmark/nest/main.go | 35 ++++++++++++++++++++++++----------- 2 files changed, 48 insertions(+), 11 deletions(-) diff --git a/benchmark/go/main.go b/benchmark/go/main.go index 533479b..fc5eb88 100644 --- a/benchmark/go/main.go +++ b/benchmark/go/main.go @@ -2,13 +2,28 @@ package main 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" @@ -50,5 +65,14 @@ func doRequest(wg *sync.WaitGroup, host, employeeID string, errChan chan error) } defer res.Body.Close() + readedBody, err := io.ReadAll(res.Body) + if err != nil { + err = fmt.Errorf("ioutil.ReadAll: %s", err) + errChan <- err + return err + } + + log.Println("readed", string(readedBody)) + return nil } diff --git a/benchmark/nest/main.go b/benchmark/nest/main.go index 2624f30..fdffa8a 100644 --- a/benchmark/nest/main.go +++ b/benchmark/nest/main.go @@ -4,14 +4,28 @@ import ( "bytes" "encoding/json" "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:3458/graphql" @@ -57,7 +71,8 @@ func doRequest(wg *sync.WaitGroup, host string, employeeID int, errChan chan err EmployeeNumber: float64(employeeID), }, } - body, err := json.Marshal(query) + + reqBody, err := json.Marshal(query) if err != nil { err = fmt.Errorf("json.Marshal: %s", err) errChan <- err @@ -65,7 +80,7 @@ func doRequest(wg *sync.WaitGroup, host string, employeeID int, errChan chan err } client := &http.Client{} - req, err := http.NewRequest("POST", host, bytes.NewReader(body)) + req, err := http.NewRequest("POST", host, bytes.NewReader(reqBody)) if err != nil { err = fmt.Errorf("http.NewRequest: %s", err) @@ -82,16 +97,14 @@ func doRequest(wg *sync.WaitGroup, host string, employeeID int, errChan chan err } defer res.Body.Close() - //log.Println("readed", string(body)) + readedBody, err := io.ReadAll(res.Body) + if err != nil { + err = fmt.Errorf("ioutil.ReadAll: %s", err) + errChan <- err + return err + } - // body, err = io.ReadAll(res.Body) - // if err != nil { - // err = fmt.Errorf("ioutil.ReadAll: %s", err) - // errChan <- err - // return err - // } - - // log.Println("readed", string(body)) + log.Println("readed", string(readedBody)) return nil }