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

79 lines
1.5 KiB
Go
Raw Normal View History

package main
import (
"fmt"
2023-04-05 12:33:37 +02:00
"io"
"log"
"net/http"
2023-04-05 12:33:37 +02:00
"os"
"strings"
"sync"
2023-04-05 12:33:37 +02:00
"time"
"gitea.urkob.com/urko/ess-etl-go/pkg/crono"
)
func main() {
2023-04-05 12:33:37 +02:00
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"
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()
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
}