//go:build load package studenttests_test import ( "flag" "fmt" "io" "net/http" "os" "sort" "sync" "sync/atomic" "testing" "time" ) var ( workers = flag.Int("workers", 20, "number of concurrent workers") duration = flag.Duration("duration", 5*time.Second, "test duration") rate = flag.Int("rate", 20, "maximum requests per second per worker") csvPath = flag.String("csv", "", "append result row to this CSV file") label = flag.String("label", "", "configuration label for CSV row") ) func TestLoad(t *testing.T) { var ( statuses = make(map[int]int64) requestErrors int64 latencies []time.Duration statusMu sync.Mutex latencyMu sync.Mutex wg sync.WaitGroup ) user1 := createUser(t) user2 := createUser(t) chat := createChat(t, user1, user1.ID, user2.ID) start := time.Now() deadline := start.Add(*duration) interval := time.Second / time.Duration(*rate) for worker := 0; worker < *workers; worker++ { wg.Add(1) go func(workerID int) { defer wg.Done() actor := user1 if workerID%2 == 1 { actor = user2 } ticker := time.NewTicker(interval) defer ticker.Stop() for time.Now().Before(deadline) { <-ticker.C if time.Now().After(deadline) { return } requestStart := time.Now() resp, err := sendLoadMessage( actor, chat.ID, "load-msg", ) latency := time.Since(requestStart) if err != nil { if atomic.AddInt64(&requestErrors, 1) == 1 { t.Logf("first transport error: %v", err) } continue } statusMu.Lock() statuses[resp.StatusCode]++ statusMu.Unlock() if resp.StatusCode == http.StatusCreated { latencyMu.Lock() latencies = append(latencies, latency) latencyMu.Unlock() } io.Copy(io.Discard, resp.Body) resp.Body.Close() } }(worker) } wg.Wait() elapsed := time.Since(start) var total int64 for _, count := range statuses { total += count } total += requestErrors t.Logf("duration: %.3fs", elapsed.Seconds()) t.Logf("workers: %d", *workers) t.Logf("rate per worker: %d req/s", *rate) t.Logf("target rate: %d req/s", *workers**rate) t.Logf("total requests: %d", total) for status, count := range statuses { t.Logf("HTTP %d: %d", status, count) } t.Logf("request errors: %d", requestErrors) successful := statuses[http.StatusCreated] t.Logf("successful RPS: %.2f", float64(successful)/elapsed.Seconds(), ) if len(latencies) > 0 { sort.Slice(latencies, func(i, j int) bool { return latencies[i] < latencies[j] }) t.Logf("successful latency p50: %s", percentile(latencies, 0.50)) t.Logf("successful latency p95: %s", percentile(latencies, 0.95)) t.Logf("successful latency p99: %s", percentile(latencies, 0.99)) } if *csvPath != "" { var p50, p95, p99 time.Duration if len(latencies) > 0 { p50 = percentile(latencies, 0.50) p95 = percentile(latencies, 0.95) p99 = percentile(latencies, 0.99) } f, err := os.OpenFile(*csvPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644) if err != nil { t.Fatalf("open csv: %v", err) } defer f.Close() fmt.Fprintf(f, "%s,%d,%d,%d,%d,%d,%d,%.2f,%.1f,%.1f,%.1f\n", *label, *workers, *rate, *workers**rate, successful, total-successful-requestErrors, requestErrors, float64(successful)/elapsed.Seconds(), float64(p50.Microseconds())/1000, float64(p95.Microseconds())/1000, float64(p99.Microseconds())/1000) } } func percentile(values []time.Duration, p float64) time.Duration { index := int(float64(len(values)-1) * p) return values[index] }