Files
magpie/internal/provider/least_allocs_test.go
T
yetone 773902a77b provider: Find copies the provider it gives; a list asks each group once
In the same live profile (v0.1.2906, two idle minutes, 269 MB):
- provider.Find copied every provider through All, each account's keys
  and headers, to give one (9.3 MB); All's copies were 4.6 MB of their
  own. sharedAll is the providers held, for reading; Find and resolveIn
  copy only the one they give, and the pricers read sharedAll.
- buildModels asked a group's members up to five times (its compaction,
  its price twice, its protocols, its tiers), each walking the members
  and resolving each (GroupFinder 7.9 MB, membersFound 6.3 MB).
  provider.FoundOnce answers each group once per build; every caller
  only reads the members.

Run (mote, at this tree):
- gofmt -l internal, go vet ./..., GOOS=windows and GOOS=linux go vet
  ./internal/agent/ ./internal/provider/: clean
- go test -count=1 -timeout 45m ./... (all three commits' tree): 85
  packages ok, internal/gateway 748s, internal/provider 193s. Two red:
  internal/gui's TestMainWindowCorners/macOS_26_SDK, mote's macOS 27
  (#3949, filed before, untouched here), and internal/agent's
  TestCodexEffortOptionsReadTheKeptList, whose bar counted the
  environment's copy (see the effort test's comment), fixed in its test.
- then go test -count=1 -timeout 45m ./internal/agent/ with that fix:
  ok (200.1s)

Broken by hand (mote, -run):
- Find through All again: 167 allocations against a bar of 60, red;
  green it is 6 (TestFindCopiesTheOneItGives, which also checks that a
  change to what Find gives reaches neither the next Find nor All).
- FoundOnce forgetting what it found: group:a walked 5 times, red
  (TestFoundOnceAsksEachGroupOnce).

Not tried: the numbers on the live app after this build.
2026-11-06 12:48:51 +08:00

36 lines
898 B
Go

package provider
import (
"runtime"
"testing"
)
// leastAllocs is testing.AllocsPerRun's fewest over five rounds: it counts
// every goroutine's allocations, and another test's left running (a
// watcher, a poll) lands in one round now and then, never in all five.
func leastAllocs(runs int, f func()) float64 {
least := testing.AllocsPerRun(runs, f)
for range 4 {
least = min(least, testing.AllocsPerRun(runs, f))
}
return least
}
// leastBytes is the fewest bytes a run of f allocated, over five rounds of
// runs, for the reason leastAllocs gives.
func leastBytes(runs int, f func()) uint64 {
var least uint64
for i := range 5 {
var before, after runtime.MemStats
runtime.ReadMemStats(&before)
for range runs {
f()
}
runtime.ReadMemStats(&after)
if n := (after.TotalAlloc - before.TotalAlloc) / uint64(runs); i == 0 || n < least {
least = n
}
}
return least
}