Files
mtusi2026lab3/cmd/bench/main.go
T
Weinmaier_AndrewandClaude Opus 5.5 2e7a80be92 lab3: балансировка, идемпотентность, отказ реплики, трек 1
- X-Instance-Id, обязательный Idempotency-Key (PostgreSQL, одна транзакция)
- HAProxy + 2..N реплик API, readiness/drain по SIGTERM, однократные миграции
- скрипты stop-one/restore/restart/rolling-update/collect-evidence
- трек 1: параметризованный стенд, cmd/bench, оркестратор и анализ
- workflow реального прогона Docker-стенда на GitHub Actions

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 17:24:54 +03:00

329 lines
9.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Command bench — генератор нагрузки для трека 1 (цена масштабирования).
//
// Профиль: смесь POST сообщения (с Idempotency-Key) и GET истории (limit=50)
// по нескольким чатам; каждый запрос идёт через LB с Bearer-токеном.
//
// Режимы:
// - closed loop (-concurrency N): N воркеров, каждый ждёт ответ перед следующим
// запросом. Показывает пропускную способность при фиксированном параллелизме.
// - open loop (-rate R): запросы назначаются по расписанию R/s независимо от
// ответов; задержка считается от НАЗНАЧЕННОГО момента, поэтому ожидание в
// очереди клиента входит в latency (поправка на coordinated omission).
//
// Повторов нет: ошибка/timeout учитывается как ошибка. Токены не печатаются.
package main
import (
"bytes"
"crypto/rand"
"encoding/base64"
"encoding/hex"
"encoding/json"
"flag"
"fmt"
"io"
"math"
mrand "math/rand/v2"
"net"
"net/http"
"os"
"sort"
"sync"
"sync/atomic"
"time"
)
type actor struct {
token string
chat string
}
type sample struct {
write bool
ok bool
latency time.Duration
class string
}
type result struct {
mu sync.Mutex
samples []sample
instances map[string]int
dropped atomic.Int64
}
func (r *result) add(s sample, instance string) {
r.mu.Lock()
r.samples = append(r.samples, s)
if instance != "" {
r.instances[instance]++
}
r.mu.Unlock()
}
func randHex(n int) string {
b := make([]byte, n)
_, _ = rand.Read(b)
return hex.EncodeToString(b)
}
func main() {
base := flag.String("base-url", "http://localhost:8080", "LB URL")
duration := flag.Duration("duration", 30*time.Second, "measured duration")
warmup := flag.Duration("warmup", 5*time.Second, "warmup (not measured)")
concurrency := flag.Int("concurrency", 8, "closed-loop workers (ignored when -rate > 0)")
rate := flag.Float64("rate", 0, "open-loop arrival rate, req/s (0 = closed loop)")
maxInflight := flag.Int("max-inflight", 512, "open-loop: max concurrent requests; excess arrivals are dropped and counted")
writeRatio := flag.Float64("write-ratio", 0.5, "fraction of POST message")
chats := flag.Int("chats", 8, "number of chats/users")
timeout := flag.Duration("timeout", 5*time.Second, "per-request timeout")
label := flag.String("label", "", "free-form label copied to output")
out := flag.String("output", "", "append JSON line to file")
flag.Parse()
transport := &http.Transport{
MaxIdleConns: 2048,
MaxIdleConnsPerHost: 2048,
IdleConnTimeout: 30 * time.Second,
DialContext: (&net.Dialer{Timeout: 2 * time.Second}).DialContext,
}
client := &http.Client{Transport: transport, Timeout: *timeout}
actors, err := setup(client, *base, *chats)
if err != nil {
fmt.Fprintln(os.Stderr, "setup failed:", err)
os.Exit(1)
}
res := &result{instances: map[string]int{}}
var measuring atomic.Bool
var seq atomic.Int64
do := func(scheduled time.Time) {
n := seq.Add(1)
a := actors[int(n)%len(actors)]
// Независимо от выбора actor: иначе при чётном числе actor и 0.5 одни бы только писали.
write := mrand.Float64() < *writeRatio
var req *http.Request
if write {
body, _ := json.Marshal(map[string]string{"text": "bench " + randHex(8)})
req, _ = http.NewRequest("POST", *base+"/api/v1/chats/"+a.chat+"/messages", bytes.NewReader(body))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Idempotency-Key", randHex(16))
} else {
req, _ = http.NewRequest("GET", *base+"/api/v1/chats/"+a.chat+"/messages?limit=50", nil)
}
req.Header.Set("Authorization", "Bearer "+a.token)
resp, err := client.Do(req)
lat := time.Since(scheduled)
s := sample{write: write, latency: lat}
inst := ""
switch {
case err != nil:
s.class = "transport"
if e, ok := err.(net.Error); ok && e.Timeout() {
s.class = "timeout"
}
default:
_, _ = io.Copy(io.Discard, resp.Body)
resp.Body.Close()
inst = resp.Header.Get("X-Instance-Id")
want := http.StatusOK
if write {
want = http.StatusCreated
}
s.ok = resp.StatusCode == want
if !s.ok {
s.class = fmt.Sprintf("http_%d", resp.StatusCode)
}
}
if measuring.Load() {
res.add(s, inst)
}
}
start := time.Now()
measureStart := start.Add(*warmup)
end := measureStart.Add(*duration)
go func() {
time.Sleep(time.Until(measureStart))
measuring.Store(true)
time.Sleep(time.Until(end))
measuring.Store(false)
}()
var wg sync.WaitGroup
if *rate > 0 {
sem := make(chan struct{}, *maxInflight)
interval := time.Duration(float64(time.Second) / *rate)
next := time.Now()
for next.Before(end) {
if d := time.Until(next); d > 0 {
time.Sleep(d)
}
scheduled := next
next = next.Add(interval)
select {
case sem <- struct{}{}:
wg.Add(1)
go func() {
defer wg.Done()
defer func() { <-sem }()
do(scheduled)
}()
default:
if measuring.Load() {
res.dropped.Add(1)
}
}
}
} else {
for i := 0; i < *concurrency; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for time.Now().Before(end) {
do(time.Now())
}
}()
}
}
wg.Wait()
report := summarize(res, *duration)
report["label"] = *label
report["mode"] = map[bool]string{true: "open", false: "closed"}[*rate > 0]
report["concurrency"] = *concurrency
report["rate"] = *rate
report["write_ratio"] = *writeRatio
report["chats"] = *chats
report["duration_s"] = duration.Seconds()
report["warmup_s"] = warmup.Seconds()
report["timestamp"] = time.Now().UTC().Format(time.RFC3339)
line, _ := json.Marshal(report)
fmt.Println(string(line))
if *out != "" {
f, err := os.OpenFile(*out, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0o644)
if err == nil {
_, _ = f.Write(append(line, '\n'))
f.Close()
}
}
}
func percentiles(lat []time.Duration) map[string]float64 {
if len(lat) == 0 {
return map[string]float64{"p50": 0, "p95": 0, "p99": 0, "max": 0}
}
sort.Slice(lat, func(i, j int) bool { return lat[i] < lat[j] })
p := func(q float64) float64 {
i := int(math.Ceil(q*float64(len(lat)))) - 1
if i < 0 {
i = 0
}
return float64(lat[i].Microseconds()) / 1000
}
return map[string]float64{"p50": p(.50), "p95": p(.95), "p99": p(.99), "max": float64(lat[len(lat)-1].Microseconds()) / 1000}
}
func summarize(r *result, d time.Duration) map[string]any {
var all, w, rd []time.Duration
errs := map[string]int{}
ok, okW, okR := 0, 0, 0
for _, s := range r.samples {
if !s.ok {
errs[s.class]++
continue
}
ok++
all = append(all, s.latency)
if s.write {
okW++
w = append(w, s.latency)
} else {
okR++
rd = append(rd, s.latency)
}
}
total := len(r.samples)
errN := total - ok
return map[string]any{
"requests": total,
"ok": ok,
"errors": errN,
"error_ratio": ratio(errN, total),
"errors_by_class": errs,
"dropped": r.dropped.Load(),
"ok_rps": float64(ok) / d.Seconds(),
"ok_write_rps": float64(okW) / d.Seconds(),
"ok_read_rps": float64(okR) / d.Seconds(),
"latency_ms": percentiles(all),
"write_ms": percentiles(w),
"read_ms": percentiles(rd),
"instances": r.instances,
}
}
func ratio(a, b int) float64 {
if b == 0 {
return 0
}
return float64(a) / float64(b)
}
// setup создаёт пары пользователей, сессии и по одному чату на пару.
func setup(c *http.Client, base string, n int) ([]actor, error) {
post := func(path string, body any, hdr map[string]string) (map[string]any, error) {
var rdr io.Reader
if body != nil {
raw, _ := json.Marshal(body)
rdr = bytes.NewReader(raw)
}
req, _ := http.NewRequest("POST", base+path, rdr)
if body != nil {
req.Header.Set("Content-Type", "application/json")
}
for k, v := range hdr {
req.Header.Set(k, v)
}
resp, err := c.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusCreated {
return nil, fmt.Errorf("POST %s: status %d", path, resp.StatusCode)
}
var m map[string]any
return m, json.NewDecoder(resp.Body).Decode(&m)
}
var actors []actor
for i := 0; i < n; i++ {
var ids [2]string
var tokens [2]string
for j := 0; j < 2; j++ {
name := "bench_" + randHex(6)
pw := "Bench-" + randHex(12)
u, err := post("/api/v1/users", map[string]string{"username": name, "display_name": "Bench", "password": pw}, nil)
if err != nil {
return nil, err
}
ids[j] = u["id"].(string)
cred := base64.StdEncoding.EncodeToString([]byte(name + ":" + pw))
s, err := post("/api/v1/auth/sessions", nil, map[string]string{"Authorization": "Basic " + cred})
if err != nil {
return nil, err
}
tokens[j] = s["token"].(string)
}
ch, err := post("/api/v1/chats", map[string]any{"title": "bench", "member_ids": []string{ids[0], ids[1]}},
map[string]string{"Authorization": "Bearer " + tokens[0]})
if err != nil {
return nil, err
}
chat := ch["id"].(string)
actors = append(actors, actor{token: tokens[0], chat: chat}, actor{token: tokens[1], chat: chat})
}
return actors, nil
}