AC1: Shared loadtest-Harness (eigene Kopie, connect ist eigenes Go-Modul) plus Lasttest fuer Store.Get() gegen echte Postgres-Testdatenbank: 3000 Aufrufe, 100 parallel, 30 Connector-Instanzen, 0 Fehler, p95=15.4ms (Ziel <=30ms), 11103.1 Get()/s. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
92 lines
2.0 KiB
Go
92 lines
2.0 KiB
Go
// Package loadtest is the QA-07 shared load-test harness for Connect:
|
|
// run N concurrent operations, record latencies, report throughput and
|
|
// percentiles. No external tool (k6/vegeta) — same approach as the
|
|
// precedent in mail/internal/loadtest (QA-07 there), reimplemented here
|
|
// because connect/ is its own Go module and cannot import mail/internal.
|
|
package loadtest
|
|
|
|
import (
|
|
"context"
|
|
"sort"
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
type Result struct {
|
|
Name string
|
|
Concurrency int
|
|
Total int
|
|
Errors int
|
|
Duration time.Duration
|
|
Latencies []time.Duration // sorted ascending
|
|
}
|
|
|
|
func (r Result) Throughput() float64 {
|
|
if r.Duration <= 0 {
|
|
return 0
|
|
}
|
|
return float64(r.Total) / r.Duration.Seconds()
|
|
}
|
|
|
|
func (r Result) Percentile(p float64) time.Duration {
|
|
if len(r.Latencies) == 0 {
|
|
return 0
|
|
}
|
|
idx := int(p * float64(len(r.Latencies)-1))
|
|
if idx < 0 {
|
|
idx = 0
|
|
}
|
|
if idx >= len(r.Latencies) {
|
|
idx = len(r.Latencies) - 1
|
|
}
|
|
return r.Latencies[idx]
|
|
}
|
|
|
|
func (r Result) Max() time.Duration {
|
|
if len(r.Latencies) == 0 {
|
|
return 0
|
|
}
|
|
return r.Latencies[len(r.Latencies)-1]
|
|
}
|
|
|
|
// Run executes total calls to op over at most concurrency parallel workers,
|
|
// recording per-call latency.
|
|
func Run(ctx context.Context, name string, concurrency, total int, op func(ctx context.Context) error) Result {
|
|
var mu sync.Mutex
|
|
latencies := make([]time.Duration, 0, total)
|
|
errCount := 0
|
|
|
|
sem := make(chan struct{}, concurrency)
|
|
var wg sync.WaitGroup
|
|
start := time.Now()
|
|
for i := 0; i < total; i++ {
|
|
sem <- struct{}{}
|
|
wg.Add(1)
|
|
go func() {
|
|
defer wg.Done()
|
|
defer func() { <-sem }()
|
|
t0 := time.Now()
|
|
err := op(ctx)
|
|
elapsed := time.Since(t0)
|
|
mu.Lock()
|
|
latencies = append(latencies, elapsed)
|
|
if err != nil {
|
|
errCount++
|
|
}
|
|
mu.Unlock()
|
|
}()
|
|
}
|
|
wg.Wait()
|
|
duration := time.Since(start)
|
|
|
|
sort.Slice(latencies, func(i, j int) bool { return latencies[i] < latencies[j] })
|
|
return Result{
|
|
Name: name,
|
|
Concurrency: concurrency,
|
|
Total: total,
|
|
Errors: errCount,
|
|
Duration: duration,
|
|
Latencies: latencies,
|
|
}
|
|
}
|