Files
magpie/internal/usage/block_price_test.go
T
yetone 926ef769ff usage: a priced gateway block is kept 30s past its last query, so the next poll reads it (#2278)
The queries reading at once already shared each gateway block's priced
copy (sharedPriced), but let it go when the last of them was done. Its
rows' sync.Pool was emptied by the collections between polls, so every
Usage poll decoded and priced the whole history again: priceBlock was
~960 MB, the pool's New ~720 MB, of 6.1 GB allocated in 80s of polls.

A block no query holds now stays pricedLinger (30s, past the 10s and 15s
polls) and is then let go by a timer; ReleaseScratch, which the window
calls once its reads rest, lets them all go at once. The key is the same
as for the sharing (requestState root|meta, the frozen block), so what a
block is priced as is no less current than when two queries share it.

Measured on mote (~/r2278 run2278d, 10 endpoints every 5s, gateway feed
from 60s), against 936bdccfa:
  allocated 120s-200s  6151 -> 5071 MB, priceBlock and the pool gone
  60s CPU profile: madvise 1.40 -> 0.85s, GC mark 4.68 -> 4.29s
  avg CPU after feed   32.9 -> 30.3%
  footprint peak 765.6 -> 769.8M, avg RSS after 60s 967 -> 966 MiB
  ps RSS peak 1010 -> 1192 MiB (footprint, what Activity Monitor
  shows, the same)
Latency is not comparable: mote's load was ~43, and the Sessions
endpoints this doesn't touch were as much slower (1.16 -> 2.01s) as
the Usage ones in the same run.
Same output: eq2.sh asked each binary heatmap, requests and usage
30d/7d/all twice; every reply byte for byte the same across binaries
and between the first and the second (kept) ask.

Rebased onto 0e2739743 (redact, codex resets); run again after it.
Run at the pushed head on mote: GOOS=linux/windows/darwin go vet -tags
nogui ./internal/usage/ ./internal/gui/ ok; go test ./internal/usage/
./internal/usageapi/ ok; ./internal/gui/ red only in
TestMainWindowCorners/macOS_26_SDK, the open #3949.
Broken by hand: release letting go at once again (the old code) turns
TestPricedBlocksSharedWhileHeld red ("0 blocks kept after every query
was done, want 2").
2026-10-29 08:19:54 +08:00

119 lines
3.7 KiB
Go

package usage
import (
"crypto/sha256"
"testing"
"time"
"github.com/yetone/magpie/internal/catalog"
"github.com/yetone/magpie/internal/provider"
)
// Queries reading at once hold one priced copy of a gateway block between
// them, kept until the last is done: the GUI's requests at a cold start
// each held the whole history's, and the app doubled (#2278). A query of
// other prices or names prices its own.
func TestPricedBlocksSharedWhileHeld(t *testing.T) {
var raw rowChunk
at := time.Date(2026, 10, 28, 12, 0, 0, 0, time.UTC)
for i := 0; i < logBlockRows; i++ {
raw.add(Row{Record: Record{Time: at, Provider: "openai", Model: "gpt-5", Input: 100 + i, Output: 10}}, "", int64(i), false)
}
packed := raw.pack()
if packed == &raw {
t.Fatal("the block was not packed")
}
pricer := func(meta string) *blockPricer {
b := newBlockPricer(provider.Renames{}, nil, func(Record) *catalog.Price { return nil })
b.share(sha256.Sum256([]byte(meta)))
return b
}
held := func() int {
sharedPriced.Lock()
defer sharedPriced.Unlock()
n := 0
for k := range sharedPriced.blocks {
if k.block == packed {
n++
}
}
return n
}
a, b, other := pricer("m"), pricer("m"), pricer("n")
ca, cb, co := a.block(packed), b.block(packed), other.block(packed)
if ca == nil || ca != cb {
t.Fatalf("two queries of one meta priced %p and %p", ca, cb)
}
if co == ca {
t.Fatal("a query of other prices read another's block")
}
if ca.row(7).Input != 107 || co.row(7).Input != 107 {
t.Fatalf("rows read back %d, %d", ca.row(7).Input, co.row(7).Input)
}
a.release()
if held() != 2 || b.block(packed) != cb {
t.Fatalf("a block one query still holds was let go: %d held", held())
}
b.release()
b.release() // a second release lets nothing go twice
other.release()
// the next poll's query reads the same priced block
if n := held(); n != 2 {
t.Fatalf("%d blocks kept after every query was done, want 2", n)
}
next := pricer("m")
if c := next.block(packed); c != ca {
t.Fatal("the next query of one meta priced the block again")
}
next.release()
// kept until pricedLinger passes with no query holding them
sharedPriced.Lock()
sweepPriced(time.Now().Add(pricedLinger/2), false)
sharedPriced.Unlock()
if n := held(); n != 2 {
t.Fatalf("%d blocks kept before pricedLinger passed, want 2", n)
}
sharedPriced.Lock()
sweepPriced(time.Now().Add(pricedLinger+time.Second), false)
sharedPriced.Unlock()
if n := held(); n != 0 {
t.Fatalf("%d blocks kept pricedLinger after every query was done", n)
}
if c := pricer("m").block(packed); c == ca {
t.Fatal("a block let go was handed out again")
}
}
// The window's rest lets every priced block no query holds go at once,
// and one a query still holds stays.
func TestPricedBlocksGoWithTheScratch(t *testing.T) {
var raw rowChunk
at := time.Date(2026, 10, 28, 12, 0, 0, 0, time.UTC)
for i := 0; i < logBlockRows; i++ {
raw.add(Row{Record: Record{Time: at, Provider: "openai", Model: "gpt-5", Input: 100 + i, Output: 10}}, "", int64(i), false)
}
packed, other := raw.pack(), (&raw).pack()
pricer := func() *blockPricer {
b := newBlockPricer(provider.Renames{}, nil, func(Record) *catalog.Price { return nil })
b.share(sha256.Sum256([]byte("scratch")))
return b
}
idle, holding := pricer(), pricer()
idle.block(packed)
idle.release()
held := holding.block(other)
ReleaseScratch()
sharedPriced.Lock()
_, kept := sharedPriced.blocks[pricedKey{sha256.Sum256([]byte("scratch")), packed}]
_, still := sharedPriced.blocks[pricedKey{sha256.Sum256([]byte("scratch")), other}]
sharedPriced.Unlock()
if kept {
t.Fatal("ReleaseScratch kept a priced block no query held")
}
if !still || held.row(3).Input != 103 {
t.Fatal("ReleaseScratch let go a block a query still held")
}
holding.release()
ReleaseScratch()
}