feat: add benchmark test 1000 req in 1 second
This commit is contained in:
parent
01ea05330c
commit
063fc2529d
|
@ -0,0 +1,54 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"sync"
|
||||
|
||||
"gitea.urkob.com/urko/ess-etl-go/pkg/crono"
|
||||
)
|
||||
|
||||
func main() {
|
||||
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
|
||||
client := &http.Client{}
|
||||
req, err := http.NewRequest("GET", url, nil)
|
||||
|
||||
if err != nil {
|
||||
err = fmt.Errorf("http.NewRequest: %s", err)
|
||||
errChan <- err
|
||||
return err
|
||||
}
|
||||
req.Header.Add("Cache-Control", "no-cache")
|
||||
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()
|
||||
|
||||
return nil
|
||||
}
|
|
@ -0,0 +1,97 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"sync"
|
||||
|
||||
"gitea.urkob.com/urko/ess-etl-go/pkg/crono"
|
||||
)
|
||||
|
||||
func main() {
|
||||
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"`
|
||||
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{
|
||||
Query: `query ByEmployeeNumber($employeeNumber: Float!) { byEmployeeNumber(employeeNumber: $employeeNumber) { EmployeeNumber Date WorkInformation { EmployeeNumber Date Shifts { Start End ActualStart ActualEnd RoleCode ShiftCategoryCode _ReferenceId } DaysOff { DayOff { DayOffTypeCode Note _ReferenceId } } } Baselines { Shifts { DayOff { DayOffTypeCode } } BaselineType DaysOff { DayOff { DayOffTypeCode } } FullDayAbsences { FullDayAbsence { AbsenceTypeCode } } } CustomFields { CustomField { FullDayAbsence { AbsenceTypeCode } } } DataVersion FullDayAbsences { FullDayAbsence { AbsenceTypeCode Note _ReferenceId } } } }`,
|
||||
OperationName: "ByEmployeeNumber",
|
||||
Variables: ByEmployeeNumberQueryVariables{
|
||||
EmployeeNumber: float64(employeeID),
|
||||
},
|
||||
}
|
||||
body, err := json.Marshal(query)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("json.Marshal: %s", err)
|
||||
errChan <- err
|
||||
return err
|
||||
}
|
||||
|
||||
client := &http.Client{}
|
||||
req, err := http.NewRequest("POST", host, bytes.NewReader(body))
|
||||
|
||||
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()
|
||||
|
||||
//log.Println("readed", string(body))
|
||||
|
||||
// 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))
|
||||
|
||||
return nil
|
||||
}
|
|
@ -43,7 +43,8 @@ func main() {
|
|||
|
||||
restServer := http.
|
||||
NewRestServer(cfg, cr).
|
||||
WithProfessionalHandler(services.NewEmployeeWIService(ctx, professionalRepo))
|
||||
WithEmployeeWIHandler(services.NewEmployeeWIService(ctx, professionalRepo)).
|
||||
WithAMSHander()
|
||||
|
||||
cr.MarkAndRestart("dependencies loaded")
|
||||
log.Println(cfg)
|
||||
|
|
|
@ -0,0 +1,35 @@
|
|||
package handler
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"gitea.urkob.com/urko/ess-etl-go/internal/request"
|
||||
"gitea.urkob.com/urko/ess-etl-go/internal/xml_loader"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
type AMSEmployeeWIHandler struct {
|
||||
ewiLoader xml_loader.EmployeeWILoader
|
||||
}
|
||||
|
||||
func NewAMSEmployeeWIHandler(AmsApi, AmsApiKey string) *AMSEmployeeWIHandler {
|
||||
return &AMSEmployeeWIHandler{
|
||||
ewiLoader: xml_loader.NewEmployeeWILoader(request.NewRequestService(AmsApi, AmsApiKey)),
|
||||
}
|
||||
}
|
||||
|
||||
func (hdl *AMSEmployeeWIHandler) Get(c *fiber.Ctx) error {
|
||||
id := c.Params("id", "")
|
||||
if id == "" {
|
||||
return JSONError(c, fiber.StatusBadRequest, fmt.Errorf("id is empty"), defaultErrMessage)
|
||||
}
|
||||
|
||||
from, to := "2023-01-16", "2023-01-16"
|
||||
employeeWI, err := hdl.ewiLoader.LoadEmployee(id, from, to)
|
||||
if err != nil {
|
||||
return JSONError(c, fiber.StatusBadRequest, fmt.Errorf("ewiLoader.LoadEmployee: %s", err), defaultErrMessage)
|
||||
}
|
||||
|
||||
return c.Status(fiber.StatusOK).JSON(employeeWI)
|
||||
}
|
|
@ -9,17 +9,17 @@ import (
|
|||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
type EmployeeWorkInformationHandler struct {
|
||||
type EmployeeWIHandler struct {
|
||||
employeeWISrv services.EmployeeWIService
|
||||
}
|
||||
|
||||
func NewEmployeeWorkInformation(employeeWISrv services.EmployeeWIService) *EmployeeWorkInformationHandler {
|
||||
return &EmployeeWorkInformationHandler{
|
||||
func NewEmployeeWIHandler(employeeWISrv services.EmployeeWIService) *EmployeeWIHandler {
|
||||
return &EmployeeWIHandler{
|
||||
employeeWISrv: employeeWISrv,
|
||||
}
|
||||
}
|
||||
|
||||
func (hdl *EmployeeWorkInformationHandler) Get(c *fiber.Ctx) error {
|
||||
func (hdl *EmployeeWIHandler) Get(c *fiber.Ctx) error {
|
||||
id := c.Params("id", "")
|
||||
if id == "" {
|
||||
return JSONError(c, fiber.StatusBadRequest, fmt.Errorf("id is empty"), defaultErrMessage)
|
||||
|
|
|
@ -14,10 +14,11 @@ import (
|
|||
)
|
||||
|
||||
type restServer struct {
|
||||
app *fiber.App
|
||||
cfg *config.Config
|
||||
cr *crono.Crono
|
||||
employeeWIHdl *handler.EmployeeWorkInformationHandler
|
||||
app *fiber.App
|
||||
cfg *config.Config
|
||||
cr *crono.Crono
|
||||
employeeWIHdl *handler.EmployeeWIHandler
|
||||
amsEmployeeWIHdl *handler.AMSEmployeeWIHandler
|
||||
}
|
||||
|
||||
func NewRestServer(cfg *config.Config, cr *crono.Crono) *restServer {
|
||||
|
@ -27,8 +28,13 @@ func NewRestServer(cfg *config.Config, cr *crono.Crono) *restServer {
|
|||
}
|
||||
}
|
||||
|
||||
func (s *restServer) WithProfessionalHandler(employeeWISrv services.EmployeeWIService) *restServer {
|
||||
s.employeeWIHdl = handler.NewEmployeeWorkInformation(employeeWISrv)
|
||||
func (s *restServer) WithEmployeeWIHandler(employeeWISrv services.EmployeeWIService) *restServer {
|
||||
s.employeeWIHdl = handler.NewEmployeeWIHandler(employeeWISrv)
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *restServer) WithAMSHander() *restServer {
|
||||
s.amsEmployeeWIHdl = handler.NewAMSEmployeeWIHandler(s.cfg.AmsApi, s.cfg.AmsApiKey)
|
||||
return s
|
||||
}
|
||||
func (s *restServer) Start(apiPort, bearerToken string) error {
|
||||
|
@ -45,6 +51,10 @@ func (s *restServer) Start(apiPort, bearerToken string) error {
|
|||
return s.employeeWIHdl.Get(c)
|
||||
})
|
||||
|
||||
s.app.Get("/ams/employee_wi/:id", func(c *fiber.Ctx) error {
|
||||
return s.amsEmployeeWIHdl.Get(c)
|
||||
})
|
||||
|
||||
s.cr.MarkAndRestart("server up")
|
||||
if err := s.app.Listen(":" + apiPort); err != nil {
|
||||
return fmt.Errorf("app.Listen: %s", err)
|
||||
|
|
|
@ -40,7 +40,6 @@ func getPayload(employeeIDList []string) (string, error) {
|
|||
|
||||
func (r RequestService) EmployeeWorkInformation(employeeIDList []string, from, to string) (io.Reader, error) {
|
||||
url := r.api + "/EmployeeWorkInformation/Search/" + from + "/" + to + "/"
|
||||
method := "POST"
|
||||
|
||||
stringPayload, err := getPayload(employeeIDList)
|
||||
if err != nil {
|
||||
|
@ -49,7 +48,7 @@ func (r RequestService) EmployeeWorkInformation(employeeIDList []string, from, t
|
|||
|
||||
payload := strings.NewReader(stringPayload)
|
||||
client := &http.Client{}
|
||||
req, err := http.NewRequest(method, url, payload)
|
||||
req, err := http.NewRequest("POST", url, payload)
|
||||
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("http.NewRequest: %s", err)
|
||||
|
|
Loading…
Reference in New Issue