forked from mtusi2026/mtusi2026lab2
139 lines
2.8 KiB
Go
139 lines
2.8 KiB
Go
//go:build load
|
|
|
|
package studenttests_test
|
|
|
|
import (
|
|
"flag"
|
|
"io"
|
|
"net/http"
|
|
"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")
|
|
)
|
|
|
|
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.CompareAndSwapInt64(&requestErrors, 0, 1) {
|
|
t.Logf("first transport error: %v", err)
|
|
}
|
|
atomic.AddInt64(&requestErrors, 1)
|
|
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))
|
|
}
|
|
}
|
|
|
|
func percentile(values []time.Duration, p float64) time.Duration {
|
|
index := int(float64(len(values)-1) * p)
|
|
return values[index]
|
|
}
|