From 4cf60df572c9a5927de173e7ce00ec3b62e7897a Mon Sep 17 00:00:00 2001 From: kk10 Date: Sun, 18 Oct 2026 12:06:03 +0300 Subject: [PATCH] test(load): add load test with api replica stop --- student-tests/load_action_test.go | 146 ++++++++++++++++++++++++++++++ student-tests/load_test.go | 3 +- student.mk | 11 ++- 3 files changed, 157 insertions(+), 3 deletions(-) create mode 100644 student-tests/load_action_test.go diff --git a/student-tests/load_action_test.go b/student-tests/load_action_test.go new file mode 100644 index 00000000..e1094b8c --- /dev/null +++ b/student-tests/load_action_test.go @@ -0,0 +1,146 @@ +//go:build load && action + +package studenttests_test + +import ( + "flag" + "io" + "net/http" + "os" + "os/exec" + "strings" + "sync" + "testing" + "time" +) + +var ( + actionScript = flag.String("action", "", "script to run during the load (e.g. scripts/stop-one.sh)") + actionAt = flag.Duration("action-at", 10*time.Second, "when to run the action, from test start") + replica = flag.Int("replica", 1, "which api replica to stop (1-based)") +) + +func resolveReplica(t *testing.T, n int) string { + t.Helper() + out, err := exec.Command("docker", "compose", "ps", "-q", "api").Output() + if err != nil { + t.Fatalf("docker compose ps: %v", err) + } + ids := strings.Fields(string(out)) + if n < 1 || n > len(ids) { + t.Fatalf("replica %d requested, but %d api containers found", n, len(ids)) + } + return ids[n-1] +} + +func actionCommand(path string) *exec.Cmd { + if strings.HasSuffix(path, ".sh") { + return exec.Command("sh", path) + } + return exec.Command(path) +} + +func TestLoadWithAction(t *testing.T) { + if *actionScript == "" { + t.Skip("-action is not set") + } + + user1 := createUser(t) + user2 := createUser(t) + chat := createChat(t, user1, user1.ID, user2.ID) + + type sample struct { + at time.Duration + ok bool + } + var ( + mu sync.Mutex + samples []sample + wg sync.WaitGroup + ) + + targetID := resolveReplica(t, *replica) + start := time.Now() + deadline := start.Add(*duration) + interval := time.Second / time.Duration(*rate) + + for w := 0; w < *workers; w++ { + wg.Add(1) + go func(id int) { + defer wg.Done() + actor := user1 + if id%2 == 1 { + actor = user2 + } + ticker := time.NewTicker(interval) + defer ticker.Stop() + + for { + <-ticker.C + if time.Now().After(deadline) { + return + } + resp, err := sendLoadMessage(actor, chat.ID, "load-msg") + ok := err == nil && resp.StatusCode == http.StatusCreated + if err == nil { + io.Copy(io.Discard, resp.Body) + resp.Body.Close() + } + mu.Lock() + samples = append(samples, sample{time.Since(start), ok}) + mu.Unlock() + } + }(w) + } + + var actionStart, actionEnd time.Duration + wg.Add(1) + go func() { + defer wg.Done() + time.Sleep(*actionAt) + actionStart = time.Since(start) + cmd := actionCommand(*actionScript) + cmd.Env = append(os.Environ(), "TARGET_INSTANCE_ID="+targetID) + out, err := cmd.CombinedOutput() + actionEnd = time.Since(start) + if err != nil { + t.Errorf("action failed: %v\n%s", err, out) + } + }() + + wg.Wait() + elapsed := time.Since(start) + + seconds := int(elapsed.Seconds()) + 1 + okBySec := make([]int, seconds) + errBySec := make([]int, seconds) + var okTotal, errTotal int + var lastErr time.Duration + + for _, s := range samples { + sec := int(s.at.Seconds()) + if s.ok { + okBySec[sec]++ + okTotal++ + } else { + errBySec[sec]++ + errTotal++ + if s.at > lastErr { + lastErr = s.at + } + } + } + + for i := 0; i < seconds; i++ { + t.Logf("t=%2ds ok=%4d err=%3d", i, okBySec[i], errBySec[i]) + } + + t.Logf("action: started at %.2fs, finished at %.2fs", actionStart.Seconds(), actionEnd.Seconds()) + t.Logf("total ok=%d errors=%d, successful RPS=%.2f", okTotal, errTotal, float64(okTotal)/elapsed.Seconds()) + + if errTotal == 0 || lastErr < actionStart { + t.Logf("recovery time: 0s (no errors after action start)") + } else { + t.Logf("recovery time: %.2fs (last error at %.2fs)", (lastErr - actionStart).Seconds(), lastErr.Seconds()) + } +} diff --git a/student-tests/load_test.go b/student-tests/load_test.go index f7b09041..a0f5b0f7 100644 --- a/student-tests/load_test.go +++ b/student-tests/load_test.go @@ -70,10 +70,9 @@ func TestLoad(t *testing.T) { latency := time.Since(requestStart) if err != nil { - if atomic.CompareAndSwapInt64(&requestErrors, 0, 1) { + if atomic.AddInt64(&requestErrors, 1) == 1 { t.Logf("first transport error: %v", err) } - atomic.AddInt64(&requestErrors, 1) continue } diff --git a/student.mk b/student.mk index 012aef3c..53e457a3 100644 --- a/student.mk +++ b/student.mk @@ -1,8 +1,10 @@ LOAD_ARGS = $(if $(LOAD_WORKERS),-workers=$(LOAD_WORKERS),) \ $(if $(LOAD_DURATION),-duration=$(LOAD_DURATION),) \ $(if $(LOAD_RATE),-rate=$(LOAD_RATE),) +REPLICA ?= 1 +ACTION_AT ?= 10s -.PHONY: help-go contract-go race-go load-go failure-go +.PHONY: help-go contract-go race-go load-go failure-go load-action-go help-go: @echo "contract-go: contract tests" @@ -21,3 +23,10 @@ load-go: failure-go: go test -tags=failure ./student-tests/... -v -args -base-url=$(BASE_URL) + +load-action-go: + go test -tags=load,action ./student-tests/... -run TestLoadWithAction -v -args \ + -base-url=$(BASE_URL) $(LOAD_ARGS) \ + -replica=$(REPLICA) \ + -action=$(CURDIR)/scripts/stop-one.sh \ + -action-at=$(ACTION_AT)