Files
magpie/internal/usage/heat_before_test.go
T
yetone 64aaf421ac usage: a packed row holds its time as unix ns and a zone, its tokens in 32 bits (#2278)
A packedRow was 184 bytes; it is 152 now. Its time.Time (24 bytes) is
kept as unix nanoseconds and the *time.Location it was in, read back
with Time(); its six token counts are int32s, not int64s. No call
reads or writes 2^31 tokens; one past it is kept as the most an int32
holds (tokensOf), not wrapped. The archive's encoding is varints and
is unchanged.

Run (mote, through mote-run): go build, go vet, GOOS=linux and
GOOS=windows go vet ./internal/usage, go test ./internal/usage
./internal/usageapi: ok. TestRowTokensAndZoneKept (new) packs a row in
a +05:30 zone at 123ns with 3e9 and -3e9 tokens, and checks time,
zone and the saturated counts after a pack/unpack round trip.
Broken by hand: tokensOf as a plain int32(n) makes it red ("read back
Input -1294967296 ... want 2147483647").

Real history copy on mote (cold start, magpie-ext = v0.1.2296 change):
  cold peak  this 346/342/354 MB (avg 347)  before 373/349/359 (avg 360)
  warm peak  this 273/262 MB               before 297/276
heatmap, overview 0/7/30, stats 0/30 and the request page JSON are
byte-identical to before.
2026-10-31 10:24:23 +08:00

58 lines
1.5 KiB
Go

package usage
import (
"testing"
"time"
)
// A named chunk's rows before the cut are counted once for a cut, not
// unpacked again for each read of the heatmap, the page and Codex's
// effort (#2278), and sum as counting every chunk does.
func TestHeatBeforeKeptBySerial(t *testing.T) {
now := time.Now().Truncate(time.Second)
cut := now.Add(-48 * time.Hour)
var set []*rowChunk
for k := range 5 {
c := &rowChunk{}
for i := range 40 {
r := Record{Time: now.Add(-time.Duration(k*20+i) * 2 * time.Hour), Agent: "claude", Provider: "relay", Input: 1 + i%5, Output: k}
c.add(Row{Record: r}, "", int64(i), false)
}
if k < 4 {
c.serial = nextChunkSerial()
}
set = append(set, c)
}
want := func(cut time.Time) heatSum {
var s heatSum
for _, c := range set {
for i := range c.Rows {
if p := &c.Rows[i]; p.Time().Before(cut) {
s.n++
s.at += p.Time().UnixNano()
for _, v := range p.Tokens {
s.tokens += int64(v)
}
}
}
}
return s
}
var opened int
heatBeforeOpening = func(*rowChunk) { opened++ }
t.Cleanup(func() { heatBeforeOpening = nil })
for i, step := range []struct {
cut time.Time
opened int
}{{cut, 5}, {cut, 1}, {cut, 1}, {cut.Add(-24 * time.Hour), 5}, {cut.Add(-24 * time.Hour), 1}} {
opened = 0
got := heatBefore(step.cut, [3][]*rowChunk{set[:2], set[2:4], set[4:]})
if w := want(step.cut); got != w || w.n == 0 {
t.Fatalf("read %d: %+v, want %+v", i, got, w)
}
if opened != step.opened {
t.Fatalf("read %d unpacked %d chunks, want %d", i, opened, step.opened)
}
}
}