Add script/loadtest to measure throughput, latency and memory (closes #81)
check / check (push) Failing after 2s

script/loadtest [duration [clients]] (make loadtest, defaults 10s and 4)
measures pixad in three scenarios: hit (one cached image), miss (a new
source image every request) and herd (each new source image asked for
by all clients at once). For each it prints vegeta's report (requests
per second, p50/p95/p99 latency, status codes), pixad's peak resident
memory and how many requests reached the origin. It is a benchmark, not
run by script/check. The origin it uses, internal/loadtestorigin behind
cmd/loadtest-origin, answers every path with one generated JPEG. Bad
arguments are refused before the build. README.md says how to run and
read it and keeps 1-5k r/s as a target not yet measured at scale.

Model: opus-5-5
This commit was merged in pull request #209.
This commit is contained in:
2026-10-05 02:58:41 +02:00
parent c434581a54
commit 55cf7f4fac
7 changed files with 406 additions and 6 deletions
+87
View File
@@ -0,0 +1,87 @@
// Package loadtestorigin is the upstream host script/loadtest points pixad
// at, run by cmd/loadtest-origin. It answers every request, whatever its path,
// with the same generated JPEG, so each new path is a new source image for
// pixad to fetch, and it logs one line per request, so its log counts pixad's
// fetches.
package loadtestorigin
import (
"bytes"
"image"
"image/color"
"image/jpeg"
"log/slog"
"math"
"net/http"
"os"
"time"
)
const (
listenAddress = ":80"
readHeaderTimeout = 10 * time.Second
imageWidth = 1600
imageHeight = 1200
jpegQuality = 85
)
// Run makes the image and serves it on port 80 until the server fails, then
// exits the process with status 1.
func Run() {
photo, err := makeJPEG()
if err != nil {
slog.Error("cannot make the image", "error", err)
os.Exit(1)
}
server := &http.Server{
Addr: listenAddress,
Handler: newHandler(photo),
ReadHeaderTimeout: readHeaderTimeout,
}
err = server.ListenAndServe()
slog.Error("server stopped", "error", err)
os.Exit(1)
}
// newHandler answers every request with photo and logs the request's path.
func newHandler(photo []byte) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
slog.Info("request", "path", r.URL.Path)
w.Header().Set("Content-Type", "image/jpeg")
_, _ = w.Write(photo)
})
}
// makeJPEG draws colour gradients crossed with a fine pattern, so the image
// has detail to decode and does not compress to almost nothing.
func makeJPEG() ([]byte, error) {
img := image.NewRGBA(image.Rect(0, 0, imageWidth, imageHeight))
// red and green count up from 0 to 255 and wrap around, along each row
// and down the image.
var green uint8
for y := range imageHeight {
var red uint8
for x := range imageWidth {
img.SetRGBA(x, y, color.RGBA{
R: red, G: green, B: red ^ green, A: math.MaxUint8,
})
red++
}
green++
}
var buf bytes.Buffer
err := jpeg.Encode(&buf, img, &jpeg.Options{Quality: jpegQuality})
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}
@@ -0,0 +1,51 @@
package loadtestorigin
import (
"bytes"
"image/jpeg"
"net/http"
"net/http/httptest"
"testing"
)
// TestEveryPathServesTheSameJPEG checks that the origin answers any path with
// 200 and the same JPEG, so every new path script/loadtest asks pixad for is
// a valid source image.
func TestEveryPathServesTheSameJPEG(t *testing.T) {
t.Parallel()
photo, err := makeJPEG()
if err != nil {
t.Fatalf("makeJPEG: %v", err)
}
size, err := jpeg.DecodeConfig(bytes.NewReader(photo))
if err != nil {
t.Fatalf("the image does not decode as a JPEG: %v", err)
}
if size.Width != imageWidth || size.Height != imageHeight {
t.Errorf("the image is %dx%d, want %dx%d",
size.Width, size.Height, imageWidth, imageHeight)
}
handler := newHandler(photo)
for _, path := range []string{"/", "/miss/1.jpg", "/herd/2.jpg"} {
rec := httptest.NewRecorder()
handler.ServeHTTP(rec, httptest.NewRequestWithContext(
t.Context(), http.MethodGet, path, nil))
if rec.Code != http.StatusOK {
t.Errorf("%s: status = %d, want %d", path, rec.Code, http.StatusOK)
}
if ct := rec.Header().Get("Content-Type"); ct != "image/jpeg" {
t.Errorf("%s: Content-Type = %q, want image/jpeg", path, ct)
}
if !bytes.Equal(rec.Body.Bytes(), photo) {
t.Errorf("%s: the body is not the image", path)
}
}
}