Files
magpie/internal/gateway/capture_test.go
T
yetone 47ca891b1b gateway: a request's capture for Recent calls is copied once, not twice
captureRequestBody wrote the body's first 256 KiB into a bytes.Buffer
and then copied the buffer into a string: 512 KiB allocated for every
request of 256 KiB or more. It is now one string conversion of the same
bytes, and the same truncated flag.

BenchmarkLargeRequest, ~2 MB conversation, B/op before → after:
  anthropic        45.84 → 45.58 MB
  anthropic-group  57.30 → 57.03 MB
  chat             36.61 → 36.35 MB
  responses        46.04 → 45.78 MB
In the 60-min soak before (dee5c9d75), Buffer.String was 1.08 GB
allocated.

TestCaptureRequestBodyLimit now also checks exact sizes (0, 5 and
callBodyLimit bytes are not truncated), that a capture allocates at most
1.25× callBodyLimit, and that the capture is a copy, not the body's
memory. Broken by hand (the buffer put back): red, "capturing 262144
bytes allocated 524288".

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).
2026-10-21 19:17:09 +08:00

43 lines
1.3 KiB
Go

package gateway
import (
"net/http/httptest"
"strings"
"testing"
)
func TestCaptureRequestBodyLimit(t *testing.T) {
body := []byte(strings.Repeat("x", callBodyLimit+17))
got, truncated := captureRequestBody(body)
if len(got) != callBodyLimit || !truncated {
t.Fatalf("captured %d bytes, truncated=%v", len(got), truncated)
}
for _, n := range []int{0, 5, callBodyLimit} {
in := []byte(strings.Repeat("y", n))
if got, truncated := captureRequestBody(in); got != string(in) || truncated {
t.Fatalf("%d bytes captured as %d, truncated=%v", n, len(got), truncated)
}
}
// copied once: the capture is all a request's capture allocates
if n := allocated(func() { captureRequestBody(body) }); n > callBodyLimit*5/4 {
t.Fatalf("capturing %d bytes allocated %d", callBodyLimit, n)
}
// and it is a copy: the body's memory is not the capture's
body[0] = 'z'
if got[0] != 'x' {
t.Fatal("the capture reads the body's memory")
}
}
func TestCaptureResponseWriterPreservesResponse(t *testing.T) {
r := httptest.NewRecorder()
w := &captureResponseWriter{ResponseWriter: r}
_, _ = w.Write([]byte(`{"ok":true}`))
if got := w.body.text(); got != `{"ok":true}` {
t.Fatalf("capture = %q", got)
}
if got := r.Body.String(); got != `{"ok":true}` {
t.Fatalf("response = %q", got)
}
}