forked from mtusi2026/mtusi2026lab5
136 lines
3.7 KiB
Go
136 lines
3.7 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"log/slog"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"messenger/internal/app"
|
|
"messenger/internal/testenv"
|
|
)
|
|
|
|
var (
|
|
db *testenv.DB
|
|
kv *testenv.KV
|
|
)
|
|
|
|
func TestMain(m *testing.M) {
|
|
ctx := context.Background()
|
|
var err error
|
|
if db, err = testenv.StartPostgres(ctx); err != nil {
|
|
fmt.Fprintln(os.Stderr, err)
|
|
os.Exit(1)
|
|
}
|
|
if kv, err = testenv.StartValkey(ctx); err != nil {
|
|
db.Close()
|
|
fmt.Fprintln(os.Stderr, err)
|
|
os.Exit(1)
|
|
}
|
|
code := m.Run()
|
|
kv.Close()
|
|
db.Close()
|
|
os.Exit(code)
|
|
}
|
|
|
|
func liveServer(t *testing.T) string {
|
|
return serve(t, nil)
|
|
}
|
|
|
|
func serve(t *testing.T, wrap func(http.Handler) http.Handler) string {
|
|
t.Helper()
|
|
a := app.New(app.Deps{
|
|
Log: slog.New(slog.NewTextHandler(io.Discard, nil)),
|
|
DB: db.Pool,
|
|
Sessions: kv.Client,
|
|
SessionTTL: time.Hour,
|
|
MaxBodyBytes: 64 * 1024,
|
|
InstanceID: "loadgen-test",
|
|
})
|
|
var h http.Handler = a.Engine
|
|
if wrap != nil {
|
|
h = wrap(h)
|
|
}
|
|
srv := httptest.NewServer(h)
|
|
t.Cleanup(srv.Close)
|
|
return srv.URL
|
|
}
|
|
|
|
// Registration hashes a password with Argon2id, which is slow on a small
|
|
// replica; the setup is not measured, so it must not fail on the short
|
|
// timeout of the measured requests.
|
|
func TestSlowSetupDoesNotFailTheRun(t *testing.T) {
|
|
slowAccounts := func(next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method == http.MethodPost && (r.URL.Path == "/api/v1/users" || r.URL.Path == "/api/v1/auth/sessions") {
|
|
time.Sleep(300 * time.Millisecond)
|
|
}
|
|
next.ServeHTTP(w, r)
|
|
})
|
|
}
|
|
cfg, err := parse("bench", []string{"-base-url", serve(t, slowAccounts), "-users", "2", "-chats", "1", "-seed-messages", "1",
|
|
"-rates", "10", "-step", "1s", "-warmup", "0s", "-timeout", "100ms", "-out", t.TempDir()})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := runBench(context.Background(), cfg); err != nil {
|
|
t.Fatalf("a setup slower than -timeout failed the run: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestBenchAgainstLiveServer(t *testing.T) {
|
|
out := t.TempDir()
|
|
cfg, err := parse("bench", []string{"-base-url", liveServer(t), "-users", "4", "-chats", "2", "-seed-messages", "3",
|
|
"-rates", "20,40", "-step", "1s", "-warmup", "0s", "-out", out})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := runBench(context.Background(), cfg); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var report benchReport
|
|
raw, err := os.ReadFile(filepath.Join(out, "steps.json"))
|
|
if err != nil || json.Unmarshal(raw, &report) != nil {
|
|
t.Fatalf("steps.json: %v %s", err, raw)
|
|
}
|
|
if len(report.Steps) != 2 || report.Seeded != 6 || report.Users != 4 || report.Chats != 2 {
|
|
t.Fatalf("report = %+v", report)
|
|
}
|
|
for _, s := range report.Steps {
|
|
if s.Sent != int(s.OfferedRPS) || s.OK != s.Sent || s.ErrorShare != 0 || s.Instances["loadgen-test"] != s.Sent {
|
|
t.Fatalf("step = %+v", s)
|
|
}
|
|
}
|
|
csv, err := os.ReadFile(filepath.Join(out, "seconds.csv"))
|
|
if err != nil || !strings.HasPrefix(string(csv), "second,sent,ok,") || strings.Count(string(csv), "\n") < 3 {
|
|
t.Fatalf("seconds.csv: %v\n%s", err, csv)
|
|
}
|
|
}
|
|
|
|
func TestTimelineVerifiesHistory(t *testing.T) {
|
|
out := t.TempDir()
|
|
cfg, err := parse("timeline", []string{"-base-url", liveServer(t), "-rate", "20", "-duration", "1s", "-out", out})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := runTimeline(context.Background(), cfg); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var report timelineReport
|
|
raw, err := os.ReadFile(filepath.Join(out, "timeline.json"))
|
|
if err != nil || json.Unmarshal(raw, &report) != nil {
|
|
t.Fatalf("timeline.json: %v %s", err, raw)
|
|
}
|
|
if report.Jobs != 20 || report.Confirmed != 20 || report.History != 20 || report.Duplicates != 0 || report.Missing != 0 || !report.Passed {
|
|
t.Fatalf("report = %+v", report)
|
|
}
|
|
}
|