94 lines
3.0 KiB
Go
94 lines
3.0 KiB
Go
// bench is a closed-loop HTTP write generator with reusable connections.
|
|
// Credentials arrive only on stdin and never appear in output.
|
|
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"flag"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"os"
|
|
"sort"
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
func main() {
|
|
base := flag.String("base", "http://localhost:8083", "")
|
|
duration := flag.Duration("duration", 5*time.Second, "")
|
|
concurrency := flag.Int("concurrency", 16, "")
|
|
flag.Parse()
|
|
var fixture struct {
|
|
Token string `json:"token"`
|
|
Chats []string `json:"chats"`
|
|
}
|
|
if json.NewDecoder(os.Stdin).Decode(&fixture) != nil || len(fixture.Chats) == 0 {
|
|
panic("invalid fixture")
|
|
}
|
|
transport := &http.Transport{MaxIdleConns: 256, MaxIdleConnsPerHost: 256, MaxConnsPerHost: 256}
|
|
client := &http.Client{Transport: transport, Timeout: 4 * time.Second}
|
|
prefix := fmt.Sprint(time.Now().UnixNano())
|
|
start := time.Now()
|
|
deadline := start.Add(*duration)
|
|
var wg sync.WaitGroup
|
|
var mu sync.Mutex
|
|
all := []float64{}
|
|
errors := map[int]int{}
|
|
instances := map[string]int{}
|
|
for worker := 0; worker < *concurrency; worker++ {
|
|
wg.Add(1)
|
|
go func(worker int) {
|
|
defer wg.Done()
|
|
latencies := []float64{}
|
|
bad := map[int]int{}
|
|
observed := map[string]int{}
|
|
for n := 0; time.Now().Before(deadline); n++ {
|
|
chat := fixture.Chats[(worker+n)%len(fixture.Chats)]
|
|
req, _ := http.NewRequest("POST", *base+"/api/v1/chats/"+chat+"/messages", bytes.NewBufferString(`{"text":"`+prefix+` fixed payload 012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"}`))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
req.Header.Set("Authorization", "Bearer "+fixture.Token)
|
|
req.Header.Set("Idempotency-Key", fmt.Sprintf("%s-%d-%d", prefix, worker, n))
|
|
t := time.Now()
|
|
res, e := client.Do(req)
|
|
if e != nil {
|
|
bad[0]++
|
|
continue
|
|
}
|
|
_, e = io.Copy(io.Discard, res.Body)
|
|
res.Body.Close()
|
|
if res.StatusCode == 201 && e == nil {
|
|
latencies = append(latencies, float64(time.Since(t).Microseconds())/1000)
|
|
observed[res.Header.Get("X-Instance-Id")]++
|
|
} else {
|
|
bad[res.StatusCode]++
|
|
}
|
|
}
|
|
mu.Lock()
|
|
all = append(all, latencies...)
|
|
for k, v := range bad {
|
|
errors[k] += v
|
|
}
|
|
for k, v := range observed {
|
|
instances[k] += v
|
|
}
|
|
mu.Unlock()
|
|
}(worker)
|
|
}
|
|
wg.Wait()
|
|
elapsed := time.Since(start).Seconds()
|
|
sort.Float64s(all)
|
|
p := func(q float64) float64 {
|
|
if len(all) == 0 {
|
|
return 0
|
|
}
|
|
return all[int(float64(len(all)-1)*q)]
|
|
}
|
|
failed := 0
|
|
for _, v := range errors {
|
|
failed += v
|
|
}
|
|
json.NewEncoder(os.Stdout).Encode(map[string]any{"duration_seconds": elapsed, "concurrency": *concurrency, "successful_requests": len(all), "successful_rps": float64(len(all)) / elapsed, "failed_requests": failed, "error_fraction": float64(failed) / float64(len(all)+failed), "errors_by_status": errors, "p50_ms": p(.5), "p95_ms": p(.95), "p99_ms": p(.99), "instances": instances, "latency_scope": "successful responses only; closed loop"})
|
|
}
|