From 74aa5d231d06d2ac7212765d4bd327e831bcc282 Mon Sep 17 00:00:00 2001 From: clawbot <35+clawbot@noreply.example.org> Date: Sun, 4 Oct 2026 22:02:06 +0000 Subject: [PATCH] Move the load-test origin's logic into internal/loadtestorigin REPO_POLICIES.md requires cmd/ to be thin: one main.go whose body is a single call into internal/ or pkg/. The image, the handler, the server and their test move unchanged into internal/loadtestorigin, whose Run does what main did; cmd/loadtest-origin/main.go only calls it. Model: opus-5-5 --- cmd/loadtest-origin/main.go | 81 +---------------- internal/loadtestorigin/loadtestorigin.go | 87 +++++++++++++++++++ .../loadtestorigin_internal_test.go | 2 +- 3 files changed, 91 insertions(+), 79 deletions(-) create mode 100644 internal/loadtestorigin/loadtestorigin.go rename cmd/loadtest-origin/origin_internal_test.go => internal/loadtestorigin/loadtestorigin_internal_test.go (98%) diff --git a/cmd/loadtest-origin/main.go b/cmd/loadtest-origin/main.go index 19dfe5e..5a8a93c 100644 --- a/cmd/loadtest-origin/main.go +++ b/cmd/loadtest-origin/main.go @@ -1,84 +1,9 @@ // Command loadtest-origin is the upstream host script/loadtest points pixad -// at. 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. +// at; internal/loadtestorigin says what it does. package main -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 -) +import "sneak.berlin/go/pixa/internal/loadtestorigin" func main() { - 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 + loadtestorigin.Run() } diff --git a/internal/loadtestorigin/loadtestorigin.go b/internal/loadtestorigin/loadtestorigin.go new file mode 100644 index 0000000..6119163 --- /dev/null +++ b/internal/loadtestorigin/loadtestorigin.go @@ -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 +} diff --git a/cmd/loadtest-origin/origin_internal_test.go b/internal/loadtestorigin/loadtestorigin_internal_test.go similarity index 98% rename from cmd/loadtest-origin/origin_internal_test.go rename to internal/loadtestorigin/loadtestorigin_internal_test.go index 740fe4b..b97b32f 100644 --- a/cmd/loadtest-origin/origin_internal_test.go +++ b/internal/loadtestorigin/loadtestorigin_internal_test.go @@ -1,4 +1,4 @@ -package main +package loadtestorigin import ( "bytes"