forked from mtusi2026/mtusi2026lab4
181 lines
3.9 KiB
Go
181 lines
3.9 KiB
Go
//go:build load
|
|
|
|
package studenttests_test
|
|
|
|
import (
|
|
"bytes"
|
|
"net/http"
|
|
"os/exec"
|
|
"sort"
|
|
"strconv"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func dockerMem(filter string) map[string]float64 {
|
|
out, err := exec.Command("docker", "stats", "--no-stream", "--format", "{{.Name}}\t{{.MemUsage}}").Output()
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
m := map[string]float64{}
|
|
for _, line := range strings.Split(string(out), "\n") {
|
|
parts := strings.SplitN(line, "\t", 2)
|
|
if len(parts) != 2 || (filter != "" && !strings.Contains(parts[0], filter)) {
|
|
continue
|
|
}
|
|
used := strings.SplitN(parts[1], "/", 2)[0]
|
|
m[parts[0]] = memBytes(used)
|
|
}
|
|
return m
|
|
}
|
|
|
|
func memBytes(s string) float64 {
|
|
s = strings.TrimSpace(s)
|
|
units := []struct {
|
|
suf string
|
|
mult float64
|
|
}{
|
|
{"GiB", 1 << 30}, {"MiB", 1 << 20}, {"KiB", 1 << 10},
|
|
{"GB", 1e9}, {"MB", 1e6}, {"kB", 1e3}, {"B", 1},
|
|
}
|
|
for _, u := range units {
|
|
if strings.HasSuffix(s, u.suf) {
|
|
v, _ := strconv.ParseFloat(strings.TrimSpace(strings.TrimSuffix(s, u.suf)), 64)
|
|
return v * u.mult
|
|
}
|
|
}
|
|
return 0
|
|
}
|
|
|
|
func mib(v float64) string {
|
|
return strconv.FormatFloat(v/(1<<20), 'f', 1, 64) + "MiB"
|
|
}
|
|
|
|
func TestConcurrentUploads(t *testing.T) {
|
|
user := createUser(t)
|
|
chat := createChat(t, user, user.ID)
|
|
body := makePNG(t, 1870, 1870)
|
|
|
|
url := *baseURL + "/api/v1/chats/" + chat.ID.String() + "/attachments?filename=load.png"
|
|
|
|
baseline := dockerMem("api")
|
|
|
|
stop := make(chan struct{})
|
|
memDone := make(chan struct{})
|
|
peak := map[string]float64{}
|
|
go func() {
|
|
defer close(memDone)
|
|
for {
|
|
for name, v := range dockerMem("api") {
|
|
if v > peak[name] {
|
|
peak[name] = v
|
|
}
|
|
}
|
|
select {
|
|
case <-stop:
|
|
return
|
|
case <-time.After(500 * time.Millisecond):
|
|
}
|
|
}
|
|
}()
|
|
|
|
var (
|
|
mu sync.Mutex
|
|
statuses = make(map[int]int)
|
|
latencies []time.Duration
|
|
errors int
|
|
)
|
|
start := time.Now()
|
|
deadline := start.Add(*duration)
|
|
|
|
var wg sync.WaitGroup
|
|
for w := 0; w < *workers; w++ {
|
|
wg.Add(1)
|
|
go func() {
|
|
defer wg.Done()
|
|
for time.Now().Before(deadline) {
|
|
req, err := http.NewRequest(http.MethodPost, url, bytes.NewReader(body))
|
|
if err != nil {
|
|
mu.Lock()
|
|
errors++
|
|
mu.Unlock()
|
|
return
|
|
}
|
|
req.Header.Set("Content-Type", "image/png")
|
|
req.Header.Set("Authorization", "Bearer "+user.Token)
|
|
|
|
reqStart := time.Now()
|
|
resp, err := httpClient.Do(req)
|
|
latency := time.Since(reqStart)
|
|
if err != nil {
|
|
mu.Lock()
|
|
errors++
|
|
mu.Unlock()
|
|
continue
|
|
}
|
|
|
|
mu.Lock()
|
|
statuses[resp.StatusCode]++
|
|
if resp.StatusCode == http.StatusCreated {
|
|
latencies = append(latencies, latency)
|
|
}
|
|
mu.Unlock()
|
|
resp.Body.Close()
|
|
}
|
|
}()
|
|
}
|
|
wg.Wait()
|
|
close(stop)
|
|
<-memDone
|
|
|
|
elapsed := time.Since(start)
|
|
|
|
total := 0
|
|
for _, n := range statuses {
|
|
total += n
|
|
}
|
|
total += errors
|
|
|
|
t.Logf("workers=%d duration=%.1fs elapsed=%.1fs", *workers, duration.Seconds(), elapsed.Seconds())
|
|
t.Logf("total uploads=%d transport_errors=%d", total, errors)
|
|
for status, n := range statuses {
|
|
t.Logf("HTTP %d: %d", status, n)
|
|
}
|
|
|
|
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("latency p50=%s p95=%s p99=%s",
|
|
percentile(latencies, 0.50),
|
|
percentile(latencies, 0.95),
|
|
percentile(latencies, 0.99),
|
|
)
|
|
}
|
|
|
|
names := make(map[string]struct{}, len(baseline)+len(peak))
|
|
for name := range baseline {
|
|
names[name] = struct{}{}
|
|
}
|
|
for name := range peak {
|
|
names[name] = struct{}{}
|
|
}
|
|
sorted := make([]string, 0, len(names))
|
|
for name := range names {
|
|
sorted = append(sorted, name)
|
|
}
|
|
sort.Strings(sorted)
|
|
|
|
if len(sorted) == 0 {
|
|
t.Log("memory: docker stats not available (no containers matched)")
|
|
return
|
|
}
|
|
t.Log("memory per container (idle -> peak):")
|
|
for _, name := range sorted {
|
|
t.Logf(" %s: %s -> %s", name, mib(baseline[name]), mib(peak[name]))
|
|
}
|
|
}
|