forked from mtusi2026/mtusi2026lab3
feat(bench): add load test flags, bench matrix and plot scripts
This commit is contained in:
@@ -45,6 +45,10 @@ func TestLoadWithAction(t *testing.T) {
|
||||
t.Skip("-action is not set")
|
||||
}
|
||||
|
||||
if *actionAt+10*time.Second > *duration {
|
||||
t.Fatalf("-duration (%s) must exceed -action-at (%s) by at least 10s", *duration, *actionAt)
|
||||
}
|
||||
|
||||
user1 := createUser(t)
|
||||
user2 := createUser(t)
|
||||
chat := createChat(t, user1, user1.ID, user2.ID)
|
||||
|
||||
@@ -4,8 +4,10 @@ package studenttests_test
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"sort"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
@@ -17,6 +19,8 @@ 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) {
|
||||
@@ -129,6 +133,27 @@ func TestLoad(t *testing.T) {
|
||||
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 {
|
||||
|
||||
Reference in New Issue
Block a user