GatewaySessions cloned every session's Models and Daily for each ask,
from the projection kept for exactly these asks. The start's first
asks (the Sessions list, each agent's manage page, the stats) each
cloned them all. In a heap snapshot at the 720k-call sandbox's peak,
cloneGatewaySession held ~447 MB live of the 1362 MB. Every caller
(gatewayExternal, listedGateway) only reads the lists, and copies them
again into its own ExternalSession anyway.
The lists are now the kept projection's, shared and read only, which
GatewaySessions' doc now says. This changes the old contract that a
caller owned its copy. The kept lists are clipped, so an append by a
caller makes a slice of its own and can't write into another's.
gatewaySessionFrom shares the days in a window the same way.
GatewaySessionByID still hands out a clone.
TestGatewaySessionsHandOutTheKeptProjection writes 2000 sessions x 10
days and asks 10 times: 435,215 bytes an ask, against the 368,000-byte
list itself. With the clone put back by hand: red, 3,669,150 bytes an
ask. TestGatewaySessionsCacheRefreshAndOwnership now checks that two
asks share the days, that two callers' appends don't alias each other
or the kept list, and that the kept data is unchanged after them. With
the clone put back it is red too ("each ask has a copy of the days of
its own").
Run: go test -p 4 -count=1 ./internal/usage/ ./internal/gui/: ok (load
~157).
Full suite on this series' final tree (aa678b482 before this note was
added), rebased on origin/main f76201a84: go test -p 4 ./... -timeout
45m, exit 0, 64 packages ok and none failed (gateway 1783 s, gui 361 s),
41 minutes at load ~190-200. gofmt -l: no files. go vet: darwin, linux
(-tags nogui) and windows exit 0. The GUI suite wasn't run: no file
under internal/gui/assets changed.
61 lines
1.7 KiB
Go
61 lines
1.7 KiB
Go
package usage
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"testing"
|
|
"time"
|
|
"unsafe"
|
|
)
|
|
|
|
// GatewaySessions hands every caller the kept projection's sessions, not a
|
|
// clone of each one's models and days: the start's first asks (the
|
|
// Sessions list, Manage for each agent, Stats) each cloned all of them,
|
|
// ~450 MB of copies at once over ~720k calls (#2776).
|
|
func TestGatewaySessionsHandOutTheKeptProjection(t *testing.T) {
|
|
pageHome(t)
|
|
settleLogClock(t)
|
|
const sessionCount, days = 2000, 10
|
|
if err := os.MkdirAll(filepath.Dir(Path()), 0o700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
f, err := os.Create(Path())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
enc := json.NewEncoder(f)
|
|
now := Clock()
|
|
for d := range days {
|
|
for i := range sessionCount {
|
|
at := now.AddDate(0, 0, -d).Add(-time.Duration(i) * time.Second)
|
|
if err := enc.Encode(Record{Time: at, Agent: "pi", Session: fmt.Sprintf("s%d", i), Provider: "p", Model: "m", Input: 10, Status: 200}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
}
|
|
if err := f.Close(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := GatewaySessions(time.Time{}, nil); len(got) != sessionCount || len(got[0].Daily) < days {
|
|
t.Fatalf("%d sessions, the first with %d days", len(got), len(got[0].Daily))
|
|
}
|
|
const asks = 10
|
|
var before, after runtime.MemStats
|
|
runtime.GC()
|
|
runtime.ReadMemStats(&before)
|
|
for range asks {
|
|
GatewaySessions(time.Time{}, nil)
|
|
}
|
|
runtime.ReadMemStats(&after)
|
|
per := (after.TotalAlloc - before.TotalAlloc) / asks
|
|
list := uint64(sessionCount) * uint64(unsafe.Sizeof(GatewaySession{}))
|
|
t.Logf("an ask allocated %d bytes; the list it answers is %d", per, list)
|
|
// the list itself, and the log's check
|
|
if per > list+list/2 {
|
|
t.Fatalf("an ask allocated %d bytes, its list is %d", per, list)
|
|
}
|
|
}
|