85 lines
1.6 KiB
Go
85 lines
1.6 KiB
Go
package go_benchmark
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"log"
|
|
"net/http"
|
|
"sync"
|
|
|
|
"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()
|
|
|
|
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
|
|
}
|
|
|
|
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 {
|
|
cr := crono.New()
|
|
if dolog {
|
|
defer cr.Table()
|
|
}
|
|
host := "http://127.0.0.1:45654"
|
|
|
|
wg := &sync.WaitGroup{}
|
|
errChan := make(chan error, 1)
|
|
wg.Add(totalRequests + 1)
|
|
go func(errChan chan error) {
|
|
defer wg.Done()
|
|
defer func() {
|
|
errChan <- nil
|
|
}()
|
|
for i := 0; i < totalRequests; i++ {
|
|
go doRequest(wg, host, "975135", errChan)
|
|
}
|
|
}(errChan)
|
|
|
|
wg.Wait()
|
|
|
|
if err := <-errChan; err != nil {
|
|
return fmt.Errorf("Benchmark: %s", err)
|
|
}
|
|
if dolog {
|
|
cr.MarkAndRestart(fmt.Sprintf("go handle %d response", totalRequests))
|
|
}
|
|
|
|
return nil
|
|
}
|