forked from mtusi2026/mtusi2026lab6
Реализация на 5
This commit is contained in:
@@ -0,0 +1,260 @@
|
||||
// Command benchmark generates paced lab5 traffic through the public load balancer.
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"math"
|
||||
"net/http"
|
||||
"os"
|
||||
"slices"
|
||||
"strings"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type client struct {
|
||||
base, token string
|
||||
http *http.Client
|
||||
}
|
||||
|
||||
func (c *client) call(method, path string, body any, key string) (map[string]any, int, error) {
|
||||
var data io.Reader
|
||||
if body != nil {
|
||||
b, e := json.Marshal(body)
|
||||
if e != nil {
|
||||
return nil, 0, e
|
||||
}
|
||||
data = bytes.NewReader(b)
|
||||
}
|
||||
req, e := http.NewRequest(method, c.base+path, data)
|
||||
if e != nil {
|
||||
return nil, 0, e
|
||||
}
|
||||
if body != nil {
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
}
|
||||
if c.token != "" {
|
||||
req.Header.Set("Authorization", "Bearer "+c.token)
|
||||
}
|
||||
if key != "" {
|
||||
req.Header.Set("Idempotency-Key", key)
|
||||
}
|
||||
// net/http otherwise treats Idempotency-Key + GetBody as permission to retry POST.
|
||||
if method == http.MethodPost {
|
||||
req.GetBody = nil
|
||||
}
|
||||
resp, e := c.http.Do(req)
|
||||
if e != nil {
|
||||
return nil, 0, e
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
b, e := io.ReadAll(io.LimitReader(resp.Body, 1<<20))
|
||||
if e != nil {
|
||||
return nil, resp.StatusCode, e
|
||||
}
|
||||
if resp.StatusCode != 200 && resp.StatusCode != 201 {
|
||||
return nil, resp.StatusCode, fmt.Errorf("HTTP %d", resp.StatusCode)
|
||||
}
|
||||
var out map[string]any
|
||||
e = json.Unmarshal(b, &out)
|
||||
return out, resp.StatusCode, e
|
||||
}
|
||||
func (c *client) setup(seed int) (string, error) {
|
||||
username := "bench_" + strings.ReplaceAll(uuid.NewString(), "-", "")[:20]
|
||||
password := "Synthetic-benchmark-123"
|
||||
user, _, e := c.call("POST", "/api/v1/users", map[string]string{"username": username, "display_name": "Benchmark", "password": password}, "")
|
||||
if e != nil {
|
||||
return "", e
|
||||
}
|
||||
req, _ := http.NewRequest("POST", c.base+"/api/v1/auth/sessions", nil)
|
||||
req.SetBasicAuth(username, password)
|
||||
resp, e := c.http.Do(req)
|
||||
if e != nil {
|
||||
return "", e
|
||||
}
|
||||
var session struct {
|
||||
Token string `json:"token"`
|
||||
}
|
||||
e = json.NewDecoder(resp.Body).Decode(&session)
|
||||
resp.Body.Close()
|
||||
if e != nil || resp.StatusCode != 201 || session.Token == "" {
|
||||
return "", fmt.Errorf("login status %d: %v", resp.StatusCode, e)
|
||||
}
|
||||
c.token = session.Token
|
||||
chat, _, e := c.call("POST", "/api/v1/chats", map[string]any{"title": "Benchmark", "member_ids": []any{user["id"]}}, "")
|
||||
if e != nil {
|
||||
return "", e
|
||||
}
|
||||
path := "/api/v1/chats/" + chat["id"].(string) + "/messages"
|
||||
// Bounded concurrent setup is excluded from the measured interval.
|
||||
var group sync.WaitGroup
|
||||
slots := make(chan struct{}, 8)
|
||||
errs := make(chan error, seed)
|
||||
for i := range seed {
|
||||
slots <- struct{}{}
|
||||
group.Go(func() {
|
||||
defer func() { <-slots }()
|
||||
_, _, e := c.call("POST", path, map[string]string{"text": fmt.Sprintf("seed-%d", i)}, uuid.NewString())
|
||||
if e != nil {
|
||||
errs <- e
|
||||
}
|
||||
})
|
||||
}
|
||||
group.Wait()
|
||||
close(errs)
|
||||
for e := range errs {
|
||||
if e != nil {
|
||||
return "", e
|
||||
}
|
||||
}
|
||||
return path, nil
|
||||
}
|
||||
|
||||
type result struct {
|
||||
MeasurementUnix float64 `json:"measurement_started_unix"`
|
||||
Label string `json:"label"`
|
||||
TargetRate int `json:"target_rps"`
|
||||
Duration float64 `json:"duration_seconds"`
|
||||
Elapsed float64 `json:"elapsed_including_drain_seconds"`
|
||||
Scheduled int `json:"scheduled"`
|
||||
Dropped int `json:"generator_dropped"`
|
||||
Success int `json:"successful_requests"`
|
||||
Errors int `json:"failed_requests"`
|
||||
ReadSuccess int `json:"read_success"`
|
||||
WriteSuccess int `json:"write_success"`
|
||||
SuccessfulRPS float64 `json:"successful_rps"`
|
||||
ErrorRatio float64 `json:"error_ratio_including_drops"`
|
||||
Latency map[string]float64 `json:"successful_latency_ms"`
|
||||
Statuses map[int]int `json:"statuses"`
|
||||
PeakInFlight int64 `json:"peak_in_flight"`
|
||||
Note string `json:"note"`
|
||||
}
|
||||
|
||||
func phase(c *client, path string, rate int, duration time.Duration, maxInFlight int, label string) result {
|
||||
out := result{Label: label, TargetRate: rate, Duration: duration.Seconds(), Statuses: map[int]int{}, Latency: map[string]float64{}, Note: "Paced open-loop, 10ms dispatch batches; 90% first-page GET, 10% POST; no POST retries. Percentiles of successes include scheduler delay; failures and capacity drops reported separately. RPS denominator includes drain."}
|
||||
start := time.Now()
|
||||
out.MeasurementUnix = float64(start.UnixNano()) / 1e9
|
||||
end := start.Add(duration)
|
||||
slots := make(chan struct{}, maxInFlight)
|
||||
var group sync.WaitGroup
|
||||
var mu sync.Mutex
|
||||
var active atomic.Int64
|
||||
var peak atomic.Int64
|
||||
latencies := make([]float64, 0, rate*int(duration.Seconds()))
|
||||
index := 0
|
||||
for tick := 0; ; tick++ {
|
||||
now := time.Now()
|
||||
if !now.Before(end) {
|
||||
break
|
||||
}
|
||||
target := start.Add(time.Duration(tick) * 10 * time.Millisecond)
|
||||
if target.After(now) {
|
||||
time.Sleep(time.Until(target))
|
||||
}
|
||||
until := min(int(time.Since(start).Seconds()*float64(rate))+1, int(duration.Seconds()*float64(rate)))
|
||||
for index < until {
|
||||
i := index
|
||||
index++
|
||||
out.Scheduled++
|
||||
scheduled := start.Add(time.Duration(float64(i) / float64(rate) * float64(time.Second)))
|
||||
select {
|
||||
case slots <- struct{}{}:
|
||||
group.Go(func() {
|
||||
n := active.Add(1)
|
||||
for old := peak.Load(); n > old; old = peak.Load() {
|
||||
if peak.CompareAndSwap(old, n) {
|
||||
break
|
||||
}
|
||||
}
|
||||
defer func() { active.Add(-1); <-slots }()
|
||||
method, p := "GET", path+"?limit=50"
|
||||
var body any
|
||||
key := ""
|
||||
if i%10 == 0 {
|
||||
method = "POST"
|
||||
p = path
|
||||
key = uuid.NewString()
|
||||
body = map[string]string{"text": key}
|
||||
}
|
||||
_, status, err := c.call(method, p, body, key)
|
||||
latency := float64(time.Since(scheduled).Microseconds()) / 1000
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
out.Statuses[status]++
|
||||
if err != nil {
|
||||
out.Errors++
|
||||
} else {
|
||||
out.Success++
|
||||
if method == "GET" {
|
||||
out.ReadSuccess++
|
||||
} else {
|
||||
out.WriteSuccess++
|
||||
}
|
||||
latencies = append(latencies, max(0, latency))
|
||||
}
|
||||
})
|
||||
default:
|
||||
out.Dropped++
|
||||
}
|
||||
}
|
||||
}
|
||||
group.Wait()
|
||||
out.Elapsed = time.Since(start).Seconds()
|
||||
out.PeakInFlight = peak.Load()
|
||||
out.SuccessfulRPS = float64(out.Success) / out.Elapsed
|
||||
if out.Scheduled > 0 {
|
||||
out.ErrorRatio = float64(out.Errors+out.Dropped) / float64(out.Scheduled)
|
||||
}
|
||||
slices.Sort(latencies)
|
||||
for name, p := range map[string]float64{"p50": .5, "p95": .95, "p99": .99} {
|
||||
if len(latencies) > 0 {
|
||||
out.Latency[name] = latencies[max(0, int(math.Ceil(p*float64(len(latencies))))-1)]
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
func main() {
|
||||
base := flag.String("base-url", "http://localhost:18086", "public LB URL")
|
||||
rate := flag.Int("rate", 100, "scheduled requests per second")
|
||||
duration := flag.Duration("duration", 30*time.Second, "measured time")
|
||||
warmup := flag.Duration("warmup", 5*time.Second, "warmup excluded from results")
|
||||
seed := flag.Int("seed", 1000, "initial messages")
|
||||
maxFlight := flag.Int("max-in-flight", 256, "bounded in-flight limit")
|
||||
label := flag.String("label", "manual", "case label")
|
||||
output := flag.String("output", "", "JSON destination")
|
||||
flag.Parse()
|
||||
if *rate < 1 || *rate > 100000 || *duration <= 0 || *warmup < 0 || *maxFlight < 1 || *seed < 0 {
|
||||
fmt.Fprintln(os.Stderr, "invalid benchmark settings")
|
||||
os.Exit(2)
|
||||
}
|
||||
c := &client{base: *base, http: &http.Client{Timeout: 2 * time.Second, Transport: &http.Transport{MaxIdleConns: 512, MaxIdleConnsPerHost: 512, MaxConnsPerHost: *maxFlight, IdleConnTimeout: 60 * time.Second}}}
|
||||
path, e := c.setup(*seed)
|
||||
if e != nil {
|
||||
fmt.Fprintln(os.Stderr, "setup:", e)
|
||||
os.Exit(1)
|
||||
}
|
||||
if *warmup > 0 {
|
||||
phase(c, path, *rate, *warmup, *maxFlight, "warmup")
|
||||
}
|
||||
row := phase(c, path, *rate, *duration, *maxFlight, *label)
|
||||
b, e := json.MarshalIndent(row, "", " ")
|
||||
if e != nil {
|
||||
panic(e)
|
||||
}
|
||||
if *output != "" {
|
||||
if e = os.WriteFile(*output, append(b, '\n'), 0644); e != nil {
|
||||
panic(e)
|
||||
}
|
||||
}
|
||||
fmt.Println(string(b))
|
||||
if row.Success == 0 {
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestLostPostResponseIsNotRetried(t *testing.T) {
|
||||
var posts atomic.Int32
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method == http.MethodGet {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write([]byte(`{}`))
|
||||
return
|
||||
}
|
||||
posts.Add(1)
|
||||
_, _ = io.Copy(io.Discard, r.Body)
|
||||
conn, _, err := w.(http.Hijacker).Hijack()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
_ = conn.Close()
|
||||
}))
|
||||
defer server.Close()
|
||||
transport := &http.Transport{MaxIdleConnsPerHost: 1}
|
||||
defer transport.CloseIdleConnections()
|
||||
c := &client{base: server.URL, http: &http.Client{Timeout: time.Second, Transport: transport}}
|
||||
if _, _, err := c.call(http.MethodGet, "/warm", nil, ""); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, _, err := c.call(http.MethodPost, "/message", map[string]string{"text": "uncertain result"}, "same-key"); err == nil {
|
||||
t.Fatal("lost response must be reported as an error")
|
||||
}
|
||||
if posts.Load() != 1 {
|
||||
t.Fatalf("transport sent %d POSTs, want exactly 1", posts.Load())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user