feat: log benchmark response into file

This commit is contained in:
Urko 2023-04-05 12:33:37 +02:00
parent 063fc2529d
commit d68c033726
2 changed files with 48 additions and 11 deletions

View File

@ -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
}

View File

@ -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
}