151 lines
3.4 KiB
Go
151 lines
3.4 KiB
Go
//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")
|
|
}
|
|
|
|
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)
|
|
|
|
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())
|
|
}
|
|
}
|