Files
magpie/internal/sessions/stats_native_test.go
T
yetone 67d2df72d9 sessions: a range's stats are aliased once, as made, and kept aliased (#2278)
With any model alias set, every Stats read copied each kept range's
days and every session's summary into a second, aliased Stats, and kept
that copy (aliasedKept) beside the unaliased one. With a long history
(~14k sessions) the copy held ~5 MB of summaries for as long as the
kept ranges lived.

Now statsKept aliases a range's Stats in place right after
statsOfRange made it (aliasOwned: only st's own Days and Sessions
arrays are written; the usage, model and effort slices are replaced,
never written into) and keeps that one. keptStats carries the alias
names it was made under, so a changed alias makes the ranges again,
as a changed price or title already did. aliasedKept, aliasKept and
aliasStats are gone.

Tests: TestSessionsCountAliasedModelsAsOne now checks that the kept
range's summaries are the very ones the read returned. Broken by hand
(a copy of Sessions before keeping): it went red ("an aliased read
keeps a copy of its summaries beside the kept ones"), and so did
alias_kept_test's "range 0 named again with nothing changed".
TestAliasOwnedLeavesSharedSlicesAlone replaces
TestAliasStatsLeavesItsInputAlone.

Run at 846cc2b9b (rebased on 96c65e3e7), on mica: gofmt -l clean; go vet,
GOOS=windows go vet and GOOS=linux go vet -tags nogui ./... ok;
go test -timeout 45m -count=1 ./...: 97 packages ok, none failed.

Real account (mote, sandbox HOME with the owner's history and an alias
set), late.sh at 120s, v0.1.3814 vs this: footprint 127 → 124 MB, live
heap after GC 100.8 → 95.9 MB; aliasStats' 5.01 MB is gone from the
profile.

Not tried: Windows and Linux runs of the package (no OS-specific code).
2026-11-12 08:39:44 +08:00

64 lines
1.6 KiB
Go

package sessions
import (
"reflect"
"testing"
"time"
)
// The overview's stats and the sessions that tell its gateway sessions
// apart come of one pass over every agent's files (#2776): it took a second
// pass, through List(All), on every refresh. The keys are List's.
func TestStatsAndNativeOnePass(t *testing.T) {
setup(t)
want := map[string]bool{}
for _, s := range List(All) {
want[s.Agent+"|"+s.ID] = true
}
passes := 0
filesHook = func() { passes++ }
t.Cleanup(func() { filesHook = nil })
st, native := StatsAndNative(0)
if passes != 1 {
t.Fatalf("%d passes over the files", passes)
}
if len(st.Sessions) == 0 {
t.Fatalf("no stats: %+v", st)
}
if len(native) != len(want) || len(want) < 2 {
t.Fatalf("native %v, List's %v", native, want)
}
for k := range want {
if !native[k] {
t.Fatalf("native %v, List's %v", native, want)
}
}
}
// TestStatsAllRangesOnePass: the ranges statsFor keeps are read again in
// one pass, and each comes out as its own read would (#2278).
func TestStatsAllRangesOnePass(t *testing.T) {
setup(t)
now := time.Now()
ranges := []int{0, 1, 7, 30}
passes := 0
filesHook = func() { passes++ }
t.Cleanup(func() { filesHook = nil })
all := statsReadAll(ranges, now, map[string]bool{}, nil, "")
if passes != 1 {
t.Fatalf("%d passes over the files", passes)
}
if len(all[0].Sessions) == 0 {
t.Fatalf("no stats: %+v", all[0])
}
for i, days := range ranges {
// read again, not the ranges kept
mu.Lock()
forgetKeptStats()
mu.Unlock()
if one := statsRead(days, now, nil); !reflect.DeepEqual(all[i], one) {
t.Fatalf("days=%d: one pass %+v\nalone %+v", days, all[i], one)
}
}
}