Files
magpie/internal/usage/kept_strings_test.go
T
yetone 6359b2c27c usage: Vias hands every caller the one copy it keeps, and a session's list is copied only when read (#2776)
Vias cloned every session's list for each ask, from a cache built for
exactly this. The Sessions list, the manage pages and a session's
detail each asked, and read the lists of the sessions they show, or of
one. Over ~720k calls one ask was ~66 MB.

Vias now answers a ViaIndex over the kept map. Of(key) is a session's
list as a copy of its own, so a caller still can't change the kept
one; Len is how many sessions have one. The three GUI callers and the
tests read through Of.

TestViasHandOutTheKeptCopy writes 20,000 sessions and asks 20 times,
reading one list each: 2,590 bytes an ask. With the clone of every
list put back by hand (under ViaIndex): red, 3,817,492 bytes an ask.
It also changes a list read through Of and checks the next ask still
says 1 call.

Full suite on this series' final tree (aa678b482 before this note was
added), rebased on origin/main f76201a84: go test -p 4 ./... -timeout
45m, exit 0, 64 packages ok and none failed (gateway 1783 s, gui 361 s),
41 minutes at load ~190-200. gofmt -l: no files. go vet: darwin, linux
(-tags nogui) and windows exit 0. The GUI suite wasn't run: no file
under internal/gui/assets changed.
2026-10-19 14:34:18 +08:00

77 lines
2.2 KiB
Go

package usage
import (
"bufio"
"crypto/rand"
"encoding/hex"
"encoding/json"
"os"
"path/filepath"
"runtime"
"testing"
"time"
)
// An unpacked block's strings share one allocation, so a cache that keeps a
// session id or a model name read from a block must copy it out, or it keeps
// every string of that block: over a long history, the decoded text of all
// of it. This test fills each block with 1 KB of unique error text and gives
// each block its own session, then measures what the Sessions and Routing
// caches hold once the log index itself is let go.
func TestSessionCachesHoldNoDecodedBlocks(t *testing.T) {
resetUsageTest(t)
const blocks, text = 16, 1024
path := Path()
if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil {
t.Fatal(err)
}
f, err := os.Create(path)
if err != nil {
t.Fatal(err)
}
w := bufio.NewWriter(f)
at := time.Now().Add(-time.Hour)
noise := make([]byte, text/2)
for b := range blocks {
for range logBlockRows {
rand.Read(noise)
line, _ := json.Marshal(Record{Time: at, Agent: "codex", Session: "s" + string(rune('a'+b)), Provider: "p", Model: "m", Effort: "high", Input: 10, Output: 2, Status: 200, Error: hex.EncodeToString(noise)})
w.Write(append(line, '\n'))
}
}
if err := w.Flush(); err != nil {
t.Fatal(err)
}
f.Close()
held := func() uint64 {
var m runtime.MemStats
runtime.GC()
runtime.GC()
runtime.ReadMemStats(&m)
return m.HeapAlloc
}
if got := GatewaySessions(time.Time{}, nil); len(got) != blocks {
t.Fatalf("sessions: %d, want %d", len(got), blocks)
}
if got := Vias(time.Time{}); got.Len() != blocks {
t.Fatalf("vias: %d, want %d", got.Len(), blocks)
}
// the index is kept by design; only what the caches hold is measured
logIndex.Lock()
logIndex.snapshot = nil
logIndex.Unlock()
withCaches := held()
gatewayCache.Lock()
gatewayCache.entry = nil
gatewayCache.Unlock()
viasCache.Lock()
viasCache.value = nil
viasCache.Unlock()
without := held()
decoded := uint64(blocks * logBlockRows * text)
if withCaches > without && withCaches-without > decoded/8 {
t.Fatalf("the caches hold %d KB for %d sessions: the decoded text of their blocks (%d KB)", (withCaches-without)>>10, blocks, decoded>>10)
}
}