From 20444bb05d9870378d365cf86355d915bbccd549 Mon Sep 17 00:00:00 2001 From: yetone Date: Wed, 21 Oct 2026 18:57:35 +0800 Subject: [PATCH] gateway: the large-request benchmark ends each request's context, as a server does BenchmarkLargeRequest served httptest.NewRequest's context.Background, which never ends. holdBody lists only a body whose context can end, so the benchmark measured a gateway that never held a body: decodeJSON copied every value, and nothing kept for a body's later readers was ever kept. A served request's context ends when it is answered; each request now gets one that does. Same tree, -benchmem, -benchtime=30x, ~2 MB conversation (B/op): anthropic 61.27 MB with Background, 55.37 MB with an ending context chat 52.64 MB 44.83 MB responses 75.28 MB 55.37 MB The rest of this series is measured with it. Run (darwin arm64, go1.26.8, sandbox HOME, load 40 to 90 from other sessions' suites): - go test ./... -count=1 -timeout 45m on the series' final tree at ba319a758: exit 0, all 68 packages ok (gateway 1498s, agent 1633s). - After the rebase onto b855cfee9 (two redact commits): go test ./internal/redact ./internal/gateway -count=1 -timeout 45m, both ok (gateway 917s). - go test -race -count=3 on the Held, ReadersOfAHeldBody, RecentRing, RequestBody, CaptureRequest, folder, WorkPlace, WorkDir and Turn tests: ok, after TestReadersOfAHeldBodyParseItOnce's byte bound was skipped under -race (the third commit says why); that test and TestHeldFactsGoWithTheirBody then passed -count=30 with and without -race. - Each commit alone: go vet ./internal/gateway and gofmt -l clean. - Final tree: gofmt -l clean; go vet ./... for darwin, GOOS=windows and GOOS=linux -tags nogui, all exit 0. Not tried: a second 60-minute soak after the series (the one before is the "before" here; the "after" numbers are the benchmark's), the GUI suite (nothing in it changed), Windows and Linux test runs (vet only). --- internal/gateway/large_body_bench_test.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/internal/gateway/large_body_bench_test.go b/internal/gateway/large_body_bench_test.go index 14ce35022..6e89f55ab 100644 --- a/internal/gateway/large_body_bench_test.go +++ b/internal/gateway/large_body_bench_test.go @@ -2,6 +2,7 @@ package gateway import ( "bytes" + "context" "encoding/json" "fmt" "net/http/httptest" @@ -110,8 +111,13 @@ func BenchmarkLargeRequest(b *testing.B) { b.SetBytes(int64(len(body))) b.ResetTimer() for range b.N { + // a served request's context ends with it, as a server's + // does: one that never ends isn't held (holdBody), and + // every reader of it decodes it again + ctx, done := context.WithCancel(context.Background()) rec := httptest.NewRecorder() - s.Handler().ServeHTTP(rec, httptest.NewRequest("POST", tc.path, bytes.NewReader(body))) + s.Handler().ServeHTTP(rec, httptest.NewRequest("POST", tc.path, bytes.NewReader(body)).WithContext(ctx)) + done() if rec.Code != 200 || f.calls == 0 { b.Fatalf("%d %s", rec.Code, rec.Body.String()) }