package main import ( "bytes" "context" "encoding/json" "flag" "fmt" "io" "net" "net/http" "net/url" "sort" "strings" "sync" "sync/atomic" "time" p "aeternalis/server/internal/protocol" "github.com/coder/websocket" ) func main() { base := flag.String("url", "http://127.0.0.1:18080", "loopback development world") clients := flag.Int("clients", 20, "concurrent clients (1-100)") duration := flag.Duration("duration", 20*time.Second, "measurement duration after connections") flag.Parse() u, err := url.Parse(*base) if err != nil || net.ParseIP(u.Hostname()) == nil || !net.ParseIP(u.Hostname()).IsLoopback() { panic("load tool only targets explicit loopback development servers") } if *clients < 1 || *clients > 100 { panic("clients must be 1-100") } var received, frames, failures atomic.Int64 var mu sync.Mutex intervals := []float64{} ctx, cancel := context.WithCancel(context.Background()) defer cancel() var readers, writers sync.WaitGroup client := &http.Client{Timeout: 5 * time.Second} post := func(path, body string, cookie *http.Cookie) (*http.Response, error) { req, _ := http.NewRequestWithContext(ctx, "POST", *base+path, strings.NewReader(body)) req.Header.Set("Origin", *base) req.Header.Set("Content-Type", "application/json") if cookie != nil { req.AddCookie(cookie) } return client.Do(req) } for i := 0; i < *clients; i++ { r, err := post("/auth/dev", "{}", nil) if err != nil { panic(err) } cookies := r.Cookies() io.Copy(io.Discard, r.Body) r.Body.Close() if r.StatusCode != 200 || len(cookies) == 0 { panic(fmt.Sprintf("auth status %d", r.StatusCode)) } r, err = post("/api/join", fmt.Sprintf(`{"name":"Load %d","passage":""}`, i), cookies[0]) if err != nil { panic(err) } data, _ := io.ReadAll(r.Body) r.Body.Close() if r.StatusCode != 200 { panic(fmt.Sprintf("join %d %s", r.StatusCode, data)) } conn, _, err := websocket.Dial(ctx, "ws"+strings.TrimPrefix(*base, "http")+"/ws", &websocket.DialOptions{HTTPHeader: http.Header{"Origin": []string{*base}, "Cookie": []string{cookies[0].String()}}}) if err != nil { panic(err) } conn.SetReadLimit(4 << 20) readers.Add(1) go func() { defer readers.Done() defer conn.CloseNow() last := time.Time{} for { _, data, err := conn.Read(ctx) if err != nil { if ctx.Err() == nil { failures.Add(1) } return } now := time.Now() if !last.IsZero() { mu.Lock() intervals = append(intervals, now.Sub(last).Seconds()*1000) mu.Unlock() } last = now received.Add(int64(len(data))) frames.Add(1) if !bytes.Contains(data, []byte(`"type":"snapshot"`)) { failures.Add(1) } } }() writers.Add(1) go func() { defer writers.Done() ticker := time.NewTicker(50 * time.Millisecond) defer ticker.Stop() seq := int64(0) for { select { case <-ctx.Done(): return case <-ticker.C: seq++ data, _ := json.Marshal(p.Intent{Op: "move", Seq: seq}) writeCtx, stop := context.WithTimeout(ctx, time.Second) err := conn.Write(writeCtx, websocket.MessageText, data) stop() if err != nil { return } } } }() time.Sleep(400 * time.Millisecond) } baselineBytes, baselineFrames := received.Load(), frames.Load() start := time.Now() time.Sleep(*duration) elapsed := time.Since(start).Seconds() cancel() readers.Wait() writers.Wait() sort.Float64s(intervals) p95 := float64(0) if len(intervals) > 0 { p95 = intervals[int(float64(len(intervals)-1)*.95)] } fmt.Printf("clients=%d duration=%.1fs snapshots=%d payload_KiB_per_second=%.1f p95_snapshot_interval_ms=%.1f disconnects=%d\n", *clients, elapsed, frames.Load()-baselineFrames, float64(received.Load()-baselineBytes)/1024/elapsed, p95, failures.Load()) if failures.Load() > 0 { panic("unexpected client disconnects") } }