test(attachments): add concurrent upload load test

This commit is contained in:
kk10
2026-11-03 14:04:32 +03:00
parent 5f3efc63a0
commit b19e64cc29
3 changed files with 206 additions and 26 deletions
+180
View File
@@ -0,0 +1,180 @@
//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]))
}
}
-26
View File
@@ -5,9 +5,6 @@ package studenttests_test
import (
"bytes"
"encoding/json"
"image"
"image/color"
"image/png"
"net/http"
"net/url"
"testing"
@@ -19,29 +16,6 @@ import (
const maxUploadBytes = 10 * 1024 * 1024
func makePNG(t *testing.T, width, height int) []byte {
t.Helper()
img := image.NewRGBA(image.Rect(0, 0, width, height))
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
img.Set(x, y, color.RGBA{
R: uint8((x * 7) % 256),
G: uint8((y * 13) % 256),
B: uint8((x + y) % 256),
A: 255,
})
}
}
var buf bytes.Buffer
if err := png.Encode(&buf, img); err != nil {
t.Fatalf("encode png: %v", err)
}
return buf.Bytes()
}
func uploadAttachment(
t *testing.T,
actor testUser,
+26
View File
@@ -4,6 +4,9 @@ import (
"bytes"
"encoding/json"
"flag"
"image"
"image/color"
"image/png"
"io"
"net/http"
"strings"
@@ -208,3 +211,26 @@ func sendLoadMessage(
return httpClient.Do(req)
}
func makePNG(t *testing.T, width, height int) []byte {
t.Helper()
img := image.NewRGBA(image.Rect(0, 0, width, height))
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
img.Set(x, y, color.RGBA{
R: uint8((x * 7) % 256),
G: uint8((y * 13) % 256),
B: uint8((x + y) % 256),
A: 255,
})
}
}
var buf bytes.Buffer
if err := png.Encode(&buf, img); err != nil {
t.Fatalf("encode png: %v", err)
}
return buf.Bytes()
}