d7e02ae08sized scanAt's buffer by the rest of the file. The other line readers of a session file still took 1 MiB per file read: - readMiniMaxCalls, readAgyCalls and readWorkBuddyCalls (WorkBuddy and CodeBuddy Code), which read a changed file whole; - scanFrames, dsh's packed (.zstd) sessions, read on from a frame; - mayHold, a search's first look at each file. (calls_pi, calls_kimi, calls_dsh and dsh.go no longer hold one since the incremental readers before a77f6ae16.) Each now takes scanBuffer(f, off): the rest of a plain file plus one byte, 16 KiB to 1 MiB. ReadBytes and readFrame read a line or frame longer than the buffer whole, as before; a packed WorkBuddy file (a zstd stream, not an *os.File) keeps 1 MiB. TestReadersOfASmallFileTakeASmallBuffer reads a two-line file 50 times through each of the five and allows 256 KiB per read. With the five readers put back (the test kept): red in all five, 1,049,161 (minimax), 1,049,271 (agy), 1,049,256 (workbuddy), 1,068,296 (frames) and 1,056,984 (search) bytes per read. Not measured on the 600k sandbox: it holds no MiniMax, agy, WorkBuddy or dsh sessions, so the saving there is the test's: ~1 MiB per file each time one of these is read again. Full suite on this series' final tree (aa678b482 before this note was added), rebased on origin/mainf76201a84: 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.
115 lines
3.5 KiB
Go
115 lines
3.5 KiB
Go
package sessions
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/klauspost/compress/zstd"
|
|
)
|
|
|
|
// A scan of a small session file takes a buffer the size of the file, not
|
|
// 1 MiB: a start reads thousands of them, and their buffers were most of
|
|
// what it allocated (#2776).
|
|
func TestScanOfASmallFileTakesASmallBuffer(t *testing.T) {
|
|
p := filepath.Join(t.TempDir(), "s.jsonl")
|
|
if err := os.WriteFile(p, []byte(`{"type":"user","n":1}`+"\n"+`{"type":"assistant","n":2}`+"\n"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
const scans = 200
|
|
lines := 0
|
|
var before, after runtime.MemStats
|
|
runtime.GC()
|
|
runtime.ReadMemStats(&before)
|
|
for range scans {
|
|
if _, err := scanAt(p, 0, nil, func(b []byte, _, _ int64) bool { lines++; return true }); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
runtime.ReadMemStats(&after)
|
|
if lines != 2*scans {
|
|
t.Fatalf("read %d lines, want %d", lines, 2*scans)
|
|
}
|
|
// 16 KiB each, with room for the file handles and anything else
|
|
if per := (after.TotalAlloc - before.TotalAlloc) / scans; per > 64<<10 {
|
|
t.Fatalf("a scan of a two-line file allocated %d bytes", per)
|
|
}
|
|
}
|
|
|
|
// A line longer than the buffer (the file grew after its size was read) is
|
|
// still handed over whole.
|
|
func TestScanReadsALineLongerThanItsBuffer(t *testing.T) {
|
|
p := filepath.Join(t.TempDir(), "s.jsonl")
|
|
if err := os.WriteFile(p, []byte("{}\n"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
f, err := os.Open(p)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
size := scanBuffer(f, 0)
|
|
f.Close()
|
|
long := `{"text":"` + strings.Repeat("x", 3*size) + `"}`
|
|
if err := os.WriteFile(p, []byte(long+"\n{}\n"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var got []int
|
|
if _, err := scanAt(p, 0, nil, func(b []byte, _, _ int64) bool { got = append(got, len(b)); return true }); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(got) != 2 || got[0] != len(long) || got[1] != 2 {
|
|
t.Fatalf("lines %v, want [%d 2]", got, len(long))
|
|
}
|
|
}
|
|
|
|
// Every other reader of a session file sizes its buffer as scanAt does:
|
|
// the calls readers of MiniMax Code, agy and WorkBuddy, dsh's packed
|
|
// frames and a search's first look each took 1 MiB per file read (#2776).
|
|
func TestReadersOfASmallFileTakeASmallBuffer(t *testing.T) {
|
|
dir := t.TempDir()
|
|
write := func(name, body string) string {
|
|
t.Helper()
|
|
p := filepath.Join(dir, name)
|
|
if err := os.WriteFile(p, []byte(body), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return p
|
|
}
|
|
line := `{"type":"user","n":1}` + "\n"
|
|
plain := write("s.jsonl", line+line)
|
|
enc, err := zstd.NewWriter(nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
framed := write("s.v3.jsonl.zstd", string(enc.EncodeAll([]byte(line+line), nil)))
|
|
enc.Close()
|
|
for _, c := range []struct {
|
|
name string
|
|
read func()
|
|
}{
|
|
{"minimax", func() { readMiniMaxCalls(file{path: plain, key: "minimax-code:s"}) }},
|
|
{"agy", func() { readAgyCalls(file{path: plain, key: "agy:s"}) }},
|
|
{"workbuddy", func() { readWorkBuddyCalls(file{agent: "workbuddy", path: plain, key: "workbuddy:s"}) }},
|
|
{"frames", func() { scanFrames(framed, 0, 0, func([]byte) bool { return true }) }},
|
|
{"search", func() { mayHold(context.Background(), plain, "absent") }},
|
|
} {
|
|
t.Run(c.name, func(t *testing.T) {
|
|
const reads = 50
|
|
c.read() // anything a first read sets up once
|
|
var before, after runtime.MemStats
|
|
runtime.GC()
|
|
runtime.ReadMemStats(&before)
|
|
for range reads {
|
|
c.read()
|
|
}
|
|
runtime.ReadMemStats(&after)
|
|
if per := (after.TotalAlloc - before.TotalAlloc) / reads; per > 256<<10 {
|
|
t.Fatalf("a read of a two-line file allocated %d bytes", per)
|
|
}
|
|
})
|
|
}
|
|
}
|