Files
magpie/internal/usage/log_visit_test.go
T
yetone dc6f6d9022 usage: a read of the gateway log decodes its blocks into one buffer (#2278)
logSnapshot.visit unpacked each block of usage.jsonl's snapshot into rows
of its own. Every Sessions, Usage and Routing poll reads the log through
it (buildGatewaySessions, topSummary, buildVias), so while calls came in
it allocated ~800 MB of rows a minute: 1.3 GB of the 5.3 GB a 24s heap
profile of a fed sandbox allocated was unpackReusing's rows, strings and
text under visit. It now decodes every block into one chunkRows buffer,
each over the one before, as the page's other readers do. A record's
strings are the block's own text, not the buffer's, so fn may keep them.

Run at this head (b69f8e96a + this change), on mote:
- go test ./internal/usage -timeout 45m: ok (24s).
- TestLogVisitReusesItsRows (new): visit's records equal a per-block
  unpack's, and one visit of 20000 rows allocates at most a quarter of
  their rows. Red on the old visit (4506304 > 4320000 bytes), green now.
- go test ./internal/gui -timeout 45m: ok but TestMainWindowCorners,
  red the same as before this change (#3949).
- go vet -tags nogui ./... for darwin, linux and windows: clean.

A/B on mote, a sandbox HOME with #2278's synthetic history (34k
sessions, 3000 with gateway records), 10 page endpoints polled every 5s
and 4 gateway appends a second, 230s each, old = v0.1.1923:
                       old        new
  all polls mean     1.69s      1.09s
  heatmap mean       4.54s      2.94s
  footprint peak    648.9M     615.5M
  avg CPU after feed  70.9%      70.3%
Another session's GUI suite ran on mote through both arms, so CPU is
noisy; the poll times are the point.

Rebased onto c5cdb043e (no internal/usage change in between), then run
again there: go test ./internal/usage ok (27.7s); go vet -tags nogui of
internal/usage and internal/gui for darwin, linux and windows clean.
Then rebased onto 8273325b0, which changes site/ only.

Not tried: the user's own history; the A/B's history is made up.
2026-10-29 00:09:24 +08:00

44 lines
1.3 KiB
Go

package usage
import (
"reflect"
"runtime"
"testing"
"time"
"unsafe"
)
// A read of the log decodes its blocks into one buffer, not each into rows
// of its own: the pages' reads allocated ~800 MB of rows a minute while
// calls came in (#2278). It reads every record as decoding each block did.
func TestLogVisitReusesItsRows(t *testing.T) {
pageHome(t)
holdClock(t, time.Now())
const n = 20000
historyLog(t, n)
s := readLogSnapshot()
if len(s.blocks) < 10 {
t.Fatalf("%d blocks, want the log in many", len(s.blocks))
}
var want []Record
for _, packed := range s.blocks {
c := packed.unpack()
for i := range c.Rows {
want = append(want, c.row(i).Record)
}
}
var got []Record
s.visit(time.Time{}, func(r Record) { got = append(got, r) })
if len(got) != n || !reflect.DeepEqual(got, want) {
t.Fatalf("visit read %d records, not the %d of the blocks decoded one by one", len(got), len(want))
}
var before, after runtime.MemStats
runtime.ReadMemStats(&before)
s.visit(time.Time{}, func(Record) {})
runtime.ReadMemStats(&after)
rows := int64(n) * int64(unsafe.Sizeof(packedRow{}))
if allocated := int64(after.TotalAlloc - before.TotalAlloc); allocated > rows/4 {
t.Fatalf("a visit of %d records allocated %d bytes; their rows alone are %d", n, allocated, rows)
}
}