Files
magpie/internal/sessions/pricer_hold_test.go
T
yetone 28ac12bc90 sessions: a pass's prices build the providers once, not once a model
A model's price looks its provider up among every provider
(BasePriceIn → byIDOrWas → All), and with nothing held each look built
every provider again from their files and accounts. keptStill checks
every kept price on each 10 s refresh of the Sessions page, so this was
the largest allocator left there: provider.All 59 MB cumulative, 40.5 MB
of it from sessions.priceOf, in 4 minutes. pricer now holds the
providers for a price it hasn't seen, and what a hold built stays for
heldFor (1 s) after it, so the rest of the pass reuses it.

Run:
- TestPricerBuildsTheProvidersOnce (new) counts provider.AllBuilt
  across six models priced with the real priceOf. Fixed: pass. Broken
  by hand (Hold removed, in a copy): "built every provider 6 times".
- mote: go test ./internal/sessions ./internal/provider
  ./internal/usage ./internal/gui ok; GOOS=windows go vet and GOOS=linux
  go vet -tags nogui ./internal/sessions clean; gofmt clean.
- Real accounts (mote, sandbox HOME copy, ad4.sh: /api/sessions/stats
  for days 0, 7 and 30 every 10 s with X-Magpie-Had, heap at 120 s and
  360 s): total allocated in the 4 minutes 294.3 MB (t68) → 210.1 MB
  (t69); provider.All cum 59.3 → 18.3 MB, allProviders 50.6 → 11.7 MB,
  sessions.priceOf 41.1 → 14.2 MB.

Not tried: Windows and Linux runs of the package (vet only).
2026-11-11 10:03:28 +08:00

26 lines
878 B
Go

package sessions
import (
"testing"
"github.com/yetone/magpie/internal/provider"
)
// A pass's prices look each model's provider up among all of them, and
// the providers are built once for the pass, not once a model: keptStill
// checks every kept price on each 10 s refresh of the Sessions page, and
// each look built every provider again (~40 MB in 4 minutes).
func TestPricerBuildsTheProvidersOnce(t *testing.T) {
setup(t)
PriceOf = priceOf // setup's stub never looks a provider up
provider.Changed()
before := provider.AllBuilt.Load()
price := pricer()
for _, m := range []string{"openai/gpt-5", "anthropic/claude-opus-5-5", "deepseek/deepseek-chat", "google/gemini-3-pro", "xai/grok-4", "moonshotai/kimi-k2"} {
price(m)
}
if n := provider.AllBuilt.Load() - before; n > 1 {
t.Fatalf("six models priced built every provider %d times; want once", n)
}
}