Files
mtusi2026lab2/service/cmd/benchmark/main.go
T

387 lines
11 KiB
Go

// Command benchmark measures the lab1 HTTP API in fresh, resource-limited containers.
package main
import (
"bytes"
"encoding/json"
"errors"
"flag"
"fmt"
"io"
"math"
"net/http"
"os"
"os/exec"
"path/filepath"
"slices"
"strconv"
"strings"
"sync"
"time"
)
const (
container = "mtusi-inmemory-benchmark"
baseURL = "http://127.0.0.1:18093"
)
type runResult struct {
History int `json:"history"`
Concurrency int `json:"concurrency"`
Repetition int `json:"repetition"`
ElapsedSeconds float64 `json:"elapsed_seconds"`
ReadSuccess int `json:"read_success"`
WriteSuccess int `json:"write_success"`
Errors int `json:"errors"`
SuccessfulRPS float64 `json:"successful_rps"`
LatencyMS map[string]float64 `json:"latency_ms"`
RSSBeforeKiB int `json:"rss_kib_before"`
RSSPeakKiB int `json:"rss_kib_peak"`
}
type report struct {
Image string `json:"image"`
Method map[string]any `json:"method"`
Runs []runResult `json:"runs"`
}
type workerResult struct {
reads int
writes int
errors int
latencies []float64
}
type memoryResult struct {
peak int
err error
}
func docker(args ...string) (string, error) {
command := exec.Command("docker", args...)
output, err := command.CombinedOutput()
if err != nil {
return "", fmt.Errorf("docker %s: %w: %s", strings.Join(args, " "), err, strings.TrimSpace(string(output)))
}
return strings.TrimSpace(string(output)), nil
}
func rssKiB() (int, error) {
status, err := docker("exec", container, "cat", "/proc/1/status")
if err != nil {
return 0, err
}
for line := range strings.SplitSeq(status, "\n") {
if strings.HasPrefix(line, "VmRSS:") {
fields := strings.Fields(line)
return strconv.Atoi(fields[1])
}
}
return 0, errors.New("VmRSS is unavailable")
}
func newClient(timeout time.Duration) *http.Client {
return &http.Client{
Timeout: timeout,
Transport: &http.Transport{
MaxConnsPerHost: 1,
MaxIdleConnsPerHost: 1,
IdleConnTimeout: 30 * time.Second,
},
}
}
func request(client *http.Client, method, path, user string, body any) (map[string]any, error) {
var data io.Reader
if body != nil {
encoded, err := json.Marshal(body)
if err != nil {
return nil, err
}
data = bytes.NewReader(encoded)
}
req, err := http.NewRequest(method, baseURL+path, data)
if err != nil {
return nil, err
}
req.Header.Set("Accept", "application/json")
if user != "" {
req.Header.Set("X-User-Id", user)
}
if body != nil {
req.Header.Set("Content-Type", "application/json")
}
response, err := client.Do(req)
if err != nil {
return nil, err
}
defer response.Body.Close()
content, err := io.ReadAll(io.LimitReader(response.Body, 1<<20))
if err != nil {
return nil, err
}
if response.StatusCode != http.StatusOK && response.StatusCode != http.StatusCreated {
return nil, fmt.Errorf("%s %s: HTTP %d: %s", method, path, response.StatusCode, content)
}
var payload map[string]any
if err := json.Unmarshal(content, &payload); err != nil {
return nil, err
}
return payload, nil
}
func stringField(payload map[string]any, key string) (string, error) {
value, ok := payload[key].(string)
if !ok || value == "" {
return "", fmt.Errorf("missing %s in API response", key)
}
return value, nil
}
func percentile(sorted []float64, proportion float64) float64 {
if len(sorted) == 0 {
return 0
}
index := int(math.Ceil(float64(len(sorted))*proportion)) - 1
if index < 0 {
index = 0
}
return round(sorted[index], 3)
}
func round(value float64, places int) float64 {
shift := math.Pow10(places)
return math.Round(value*shift) / shift
}
func sampleMemory(stop <-chan struct{}, initial int, result chan<- memoryResult) {
ticker := time.NewTicker(250 * time.Millisecond)
defer ticker.Stop()
peak := initial
for {
select {
case <-stop:
result <- memoryResult{peak: peak}
return
case <-ticker.C:
rss, err := rssKiB()
if err != nil {
result <- memoryResult{err: err}
return
}
peak = max(peak, rss)
}
}
}
func runCase(image string, history, concurrency, repetition int, duration time.Duration) (runResult, error) {
row := runResult{History: history, Concurrency: concurrency, Repetition: repetition}
if _, err := docker("run", "-d", "--name", container, "--cpus=1", "--memory=512m",
"-p", "127.0.0.1:18093:8080", image); err != nil {
return row, err
}
defer func() { _, _ = docker("rm", "-f", container) }()
setup := newClient(2 * time.Second)
defer setup.CloseIdleConnections()
readyDeadline := time.Now().Add(20 * time.Second)
for {
if _, err := request(setup, http.MethodGet, "/health/ready", "", nil); err == nil {
break
} else if time.Now().After(readyDeadline) {
return row, fmt.Errorf("server did not become ready: %w", err)
}
time.Sleep(100 * time.Millisecond)
}
userResponse, err := request(setup, http.MethodPost, "/api/v1/users", "", map[string]any{
"username": fmt.Sprintf("bench_%d_%d_%d", time.Now().UnixNano(), history, repetition),
"display_name": "Benchmark",
})
if err != nil {
return row, err
}
user, err := stringField(userResponse, "id")
if err != nil {
return row, err
}
chatResponse, err := request(setup, http.MethodPost, "/api/v1/chats", user, map[string]any{
"title": "Benchmark", "member_ids": []string{user},
})
if err != nil {
return row, err
}
chat, err := stringField(chatResponse, "id")
if err != nil {
return row, err
}
path := "/api/v1/chats/" + chat + "/messages"
for index := range history {
if _, err := request(setup, http.MethodPost, path, user, map[string]string{
"text": fmt.Sprintf("seed-%d", index),
}); err != nil {
return row, fmt.Errorf("seed message %d: %w", index, err)
}
}
row.RSSBeforeKiB, err = rssKiB()
if err != nil {
return row, err
}
start := make(chan struct{})
results := make(chan workerResult, concurrency)
stopSampling := make(chan struct{})
memory := make(chan memoryResult, 1)
go sampleMemory(stopSampling, row.RSSBeforeKiB, memory)
var group sync.WaitGroup
group.Add(concurrency)
deadline := time.Now().Add(duration)
started := time.Now()
for workerID := range concurrency {
go func() {
defer group.Done()
local := newClient(5 * time.Second)
defer local.CloseIdleConnections()
result := workerResult{}
<-start
for index := 0; time.Now().Before(deadline); index++ {
began := time.Now()
var err error
if index%10 == 0 {
_, err = request(local, http.MethodPost, path, user, map[string]string{
"text": fmt.Sprintf("load-%d-%d-%d", repetition, workerID, index),
})
if err == nil {
result.writes++
}
} else {
var page map[string]any
page, err = request(local, http.MethodGet, path+"?limit=50", user, nil)
if err == nil {
items, ok := page["items"].([]any)
if !ok || len(items) != 50 {
err = errors.New("short first page")
} else {
result.reads++
}
}
}
if err != nil {
result.errors++
} else {
result.latencies = append(result.latencies, float64(time.Since(began).Microseconds())/1000)
}
}
results <- result
}()
}
close(start)
group.Wait()
row.ElapsedSeconds = round(time.Since(started).Seconds(), 3)
close(stopSampling)
memorySample := <-memory
if memorySample.err != nil {
return row, memorySample.err
}
row.RSSPeakKiB = memorySample.peak
if finalRSS, err := rssKiB(); err == nil {
row.RSSPeakKiB = max(row.RSSPeakKiB, finalRSS)
} else {
return row, err
}
latencies := make([]float64, 0)
for range concurrency {
result := <-results
row.ReadSuccess += result.reads
row.WriteSuccess += result.writes
row.Errors += result.errors
latencies = append(latencies, result.latencies...)
}
if len(latencies) == 0 {
return row, errors.New("no successful requests")
}
slices.Sort(latencies)
row.SuccessfulRPS = round(float64(len(latencies))/row.ElapsedSeconds, 2)
row.LatencyMS = map[string]float64{
"p95": percentile(latencies, 0.95),
"p99": percentile(latencies, 0.99),
}
return row, nil
}
func parseValues(raw string, minimum int) ([]int, error) {
values := make([]int, 0)
for part := range strings.SplitSeq(raw, ",") {
value, err := strconv.Atoi(strings.TrimSpace(part))
if err != nil || value < minimum {
return nil, fmt.Errorf("invalid value %q", part)
}
values = append(values, value)
}
return values, nil
}
func save(path string, data report) error {
encoded, err := json.MarshalIndent(data, "", " ")
if err != nil {
return err
}
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
return err
}
return os.WriteFile(path, append(encoded, '\n'), 0o644)
}
func main() {
image := flag.String("image", "", "Docker image to benchmark")
output := flag.String("output", "", "JSON output path")
historiesArg := flag.String("histories", "100,500,1500", "Comma-separated history sizes")
concurrenciesArg := flag.String("concurrencies", "1,4,10", "Comma-separated client counts")
repeats := flag.Int("repeats", 3, "Runs per combination")
duration := flag.Duration("duration", 5*time.Second, "Measured duration per run")
flag.Parse()
if *image == "" || *output == "" || *repeats < 1 || *duration <= 0 {
fmt.Fprintln(os.Stderr, "image, output, positive repeats and duration are required")
os.Exit(2)
}
histories, err := parseValues(*historiesArg, 50)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(2)
}
concurrencies, err := parseValues(*concurrenciesArg, 1)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(2)
}
data := report{
Image: *image,
Method: map[string]any{
"histories": histories, "concurrencies": concurrencies,
"repeats": *repeats, "duration_seconds": duration.Seconds(),
"mix": "90% first-page GET, 10% POST per client; persistent HTTP connections",
"limits": "1 CPU, 512 MiB; fresh container and history per case",
"rss": "VmRSS from /proc/1/status every 250 ms (sampled peak)",
"latency": "successful requests only; errors counted separately; closed-loop workload",
},
Runs: make([]runResult, 0, len(histories)*len(concurrencies)*(*repeats)),
}
for _, history := range histories {
for _, concurrency := range concurrencies {
for repetition := 1; repetition <= *repeats; repetition++ {
row, err := runCase(*image, history, concurrency, repetition, *duration)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
data.Runs = append(data.Runs, row)
if err := save(*output, data); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
encoded, _ := json.Marshal(row)
fmt.Println(string(encoded))
}
}
}
}