2023-04-05 14:00:39 +02:00
|
|
|
package nest_benchmark
|
2023-04-05 12:18:07 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2023-04-05 12:33:37 +02:00
|
|
|
"io"
|
2023-04-05 12:18:07 +02:00
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"sync"
|
|
|
|
|
2023-04-05 19:04:52 +02:00
|
|
|
"gitea.urkob.com/urko/crono"
|
2023-04-05 12:18:07 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type GraphqlQuery struct {
|
|
|
|
Query string `json:"query"`
|
|
|
|
OperationName string `json:"operationName"`
|
|
|
|
Variables interface{} `json:"variables"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type ByEmployeeNumberQueryVariables struct {
|
|
|
|
EmployeeNumber float64 `json:"employeeNumber"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func doRequest(wg *sync.WaitGroup, host string, employeeID int, errChan chan error) error {
|
|
|
|
defer wg.Done()
|
|
|
|
var err error
|
|
|
|
query := GraphqlQuery{
|
2023-04-10 11:43:13 +02:00
|
|
|
Query: `query ByEmployeeNumber($employeeNumber: Float!) { byEmployeeNumber(employeeNumber: $employeeNumber) { EmployeeNumber Date WorkInformation { EmployeeNumber Date Shifts { Start End RoleCode } DaysOff { DayOff { DayOffTypeCode Note _ReferenceId } } } Baselines { Shifts { Start End RoleCode } BaselineType DaysOff { DayOff { DayOffTypeCode } } FullDayAbsences { FullDayAbsence { AbsenceTypeCode } } } CustomFields { CustomField { Name } } DataVersion FullDayAbsences { FullDayAbsence { _ReferenceId } } } } `,
|
2023-04-05 12:18:07 +02:00
|
|
|
OperationName: "ByEmployeeNumber",
|
|
|
|
Variables: ByEmployeeNumberQueryVariables{
|
|
|
|
EmployeeNumber: float64(employeeID),
|
|
|
|
},
|
|
|
|
}
|
2023-04-05 12:33:37 +02:00
|
|
|
|
|
|
|
reqBody, err := json.Marshal(query)
|
2023-04-05 12:18:07 +02:00
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("json.Marshal: %s", err)
|
|
|
|
errChan <- err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
client := &http.Client{}
|
2023-04-05 12:33:37 +02:00
|
|
|
req, err := http.NewRequest("POST", host, bytes.NewReader(reqBody))
|
2023-04-05 12:18:07 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("http.NewRequest: %s", err)
|
|
|
|
errChan <- err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
req.Header.Add("Content-Type", "application/json")
|
|
|
|
|
|
|
|
res, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("client.Do: %s", err)
|
|
|
|
errChan <- err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer res.Body.Close()
|
|
|
|
|
2023-04-05 12:33:37 +02:00
|
|
|
readedBody, err := io.ReadAll(res.Body)
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("ioutil.ReadAll: %s", err)
|
|
|
|
errChan <- err
|
|
|
|
return err
|
|
|
|
}
|
2023-04-05 12:18:07 +02:00
|
|
|
|
2023-04-05 12:33:37 +02:00
|
|
|
log.Println("readed", string(readedBody))
|
2023-04-05 12:18:07 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2023-04-05 14:00:39 +02:00
|
|
|
|
2023-04-08 22:10:09 +02:00
|
|
|
func BenchmarkNoLog(totalRequests int) error {
|
|
|
|
return benchmark(totalRequests, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Benchmark(totalRequests int) error {
|
|
|
|
return benchmark(totalRequests, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func benchmark(totalRequests int, dolog bool) error {
|
2023-04-05 14:00:39 +02:00
|
|
|
cr := crono.New()
|
2023-04-08 22:05:26 +02:00
|
|
|
if dolog {
|
|
|
|
defer cr.Table()
|
|
|
|
}
|
2023-04-05 14:00:39 +02:00
|
|
|
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()
|
2023-04-05 19:04:52 +02:00
|
|
|
defer func() {
|
|
|
|
errChan <- nil
|
|
|
|
}()
|
2023-04-05 14:00:39 +02:00
|
|
|
for i := 0; i < totalRequests; i++ {
|
|
|
|
wg.Add(1)
|
|
|
|
go doRequest(wg, host, 975153, errChan)
|
|
|
|
}
|
|
|
|
}(errChan)
|
|
|
|
|
|
|
|
wg.Wait()
|
2023-04-05 19:04:52 +02:00
|
|
|
|
|
|
|
if err := <-errChan; err != nil {
|
|
|
|
return fmt.Errorf("Benchmark: %s", err)
|
|
|
|
}
|
2023-04-08 22:05:26 +02:00
|
|
|
if dolog {
|
|
|
|
cr.MarkAndRestart(fmt.Sprintf("nest handle %d response", totalRequests))
|
|
|
|
}
|
2023-04-05 19:04:52 +02:00
|
|
|
return nil
|
2023-04-05 14:00:39 +02:00
|
|
|
}
|