ess-etl-go/benchmark/go/go.go

84 lines
1.7 KiB
Go
Raw Normal View History

package go_benchmark
import (
"fmt"
2023-04-05 12:33:37 +02:00
"io"
"log"
"net/http"
"sync"
2023-04-05 19:04:52 +02:00
"gitea.urkob.com/urko/crono"
)
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()
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
}
log.Println("readed", string(readedBody))
return nil
}
2023-04-24 11:55:33 +02:00
func BenchmarkNoLog(host, employeeID string, totalRequests int) error {
return benchmark(host, employeeID, totalRequests, false)
2023-04-08 22:10:09 +02:00
}
2023-04-24 11:55:33 +02:00
func Benchmark(host, employeeID string, totalRequests int) error {
return benchmark(host, employeeID, totalRequests, true)
2023-04-08 22:10:09 +02:00
}
2023-04-24 11:55:33 +02:00
func benchmark(host, employeeID string, totalRequests int, dolog bool) error {
cr := crono.New()
2023-04-08 22:05:26 +02:00
if dolog {
defer cr.Table()
}
wg := &sync.WaitGroup{}
errChan := make(chan error, 1)
2023-04-08 22:05:26 +02:00
wg.Add(totalRequests + 1)
go func(errChan chan error) {
defer wg.Done()
2023-04-05 19:04:52 +02:00
defer func() {
errChan <- nil
}()
for i := 0; i < totalRequests; i++ {
2023-04-24 11:55:33 +02:00
go doRequest(wg, host, employeeID, 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("go handle %d response", totalRequests))
}
2023-04-05 19:04:52 +02:00
return nil
}