An open Sessions or Usage window polls every 15 s, and a poll's reads allocate ~330 MB to keep ~105 MB. At GOGC 100 the heap grew to twice what it kept before a collection, so Activity Monitor showed ~280-360 MB during each poll. Each /api/sessions* and /api/usage* GET now runs under GOGC 25, the warm-up's usage read's setting, without the memory limit that read also takes: held at heldMemory, every poll collected over and over and the window spent 5x the CPU. underTightGC put back what it found, so two reads at once ended with the second's 25 for good when the first ended first. A count now holds it: the first read sets GOGC (and the limit, for the warm-up), the last puts back what was there before the first. Measured in a sandbox HOME over the real session and usage data (6 GUI polls 15 s apart after a warm start, footprint right after each poll / 10 s later; CPU over the 6 polls): - origin/main: 326 326 326 290 364 281 MB / 188-217 MB; 5.2 s - GOGC 25 + memory limit: 190 176 172 161 216 186 / 149-172; 20.7 s - this (GOGC 25 only): 278 217 220 197 248 214 / 186-209; 5.6 s /api/sessions 495 ms vs 653 ms on main, the other routes the same. TestTightGCOverlappingReads ends two overlapping reads in either order; with the count taken out (each read putting back what it found) it is red, "one read still in flight under GOGC=100, want 25". Run at this head (ea7c0067e+ this): gofmt -l clean; GOOS=linux, windows, darwin go vet -tags nogui ./internal/gui/ clean; go test -timeout 45m ./internal/gui/ ok (203.3s; a run before it had three reds that were all "no space left on device", the disk filled by another build of mine). Then rebased onto6eb510f78, whose only internal/gui change is web.go's home-screen manifest: gofmt and the three vets clean again, and -run 'TightGC|Warm|Held|PageRead|Sessions' ./internal/gui/ ok. Not tried: the Linux and Windows apps' own footprint.
110 lines
3.3 KiB
Go
110 lines
3.3 KiB
Go
package gui
|
|
|
|
import (
|
|
"math"
|
|
"runtime/debug"
|
|
"testing"
|
|
)
|
|
|
|
func gcPercent() int {
|
|
p := debug.SetGCPercent(100)
|
|
debug.SetGCPercent(p)
|
|
return p
|
|
}
|
|
|
|
// The usage page's read of the whole history runs under a tighter GOGC, so
|
|
// its heap (every row at once) isn't let grow to twice that, and puts back
|
|
// what was set when it ends; a lower GOGC, or off, is kept as it is.
|
|
func TestUnderTightGC(t *testing.T) {
|
|
defer debug.SetGCPercent(debug.SetGCPercent(100))
|
|
for _, before := range []int{100, 200, 10, -1} {
|
|
debug.SetGCPercent(before)
|
|
var during int
|
|
underTightGC(func() { during = gcPercent() })
|
|
want := min(before, tightGCPercent)
|
|
if before < 0 {
|
|
want = before
|
|
}
|
|
if during != want {
|
|
t.Errorf("GOGC=%d: read under %d, want %d", before, during, want)
|
|
}
|
|
if after := gcPercent(); after != before {
|
|
t.Errorf("GOGC=%d: after the read it is %d", before, after)
|
|
}
|
|
}
|
|
}
|
|
|
|
// Under it the memory Go holds is limited to what it held when the read
|
|
// began, and the limit set before is put back; a lower limit the user set
|
|
// is kept, and GOGC off gets none.
|
|
func TestUnderTightGCLimitsMemory(t *testing.T) {
|
|
defer debug.SetGCPercent(debug.SetGCPercent(100))
|
|
defer debug.SetMemoryLimit(debug.SetMemoryLimit(math.MaxInt64))
|
|
for _, c := range []struct {
|
|
name string
|
|
gogc int
|
|
before int64
|
|
low bool
|
|
}{
|
|
{"default", 100, math.MaxInt64, true},
|
|
{"user limit lower", 100, 1 << 20, false},
|
|
{"GOGC off", -1, math.MaxInt64, false},
|
|
} {
|
|
debug.SetGCPercent(c.gogc)
|
|
debug.SetMemoryLimit(c.before)
|
|
var during int64
|
|
underTightGC(func() { during = debug.SetMemoryLimit(-1) })
|
|
if c.low && during >= c.before {
|
|
t.Errorf("%s: read under limit %d, want what Go held", c.name, during)
|
|
}
|
|
if !c.low && during != c.before {
|
|
t.Errorf("%s: read under limit %d, want %d kept", c.name, during, c.before)
|
|
}
|
|
if after := debug.SetMemoryLimit(-1); after != c.before {
|
|
t.Errorf("%s: after the read the limit is %d, want %d", c.name, after, c.before)
|
|
}
|
|
}
|
|
}
|
|
|
|
// Two reads under it at once put back what was set before the first, in
|
|
// either order they end: each putting back what it found left the second's
|
|
// 25 when the first ended before it.
|
|
func TestTightGCOverlappingReads(t *testing.T) {
|
|
defer debug.SetGCPercent(debug.SetGCPercent(100))
|
|
defer debug.SetMemoryLimit(debug.SetMemoryLimit(math.MaxInt64))
|
|
for _, firstEndsFirst := range []bool{true, false} {
|
|
first := tightenGC(true)
|
|
second := tightenGC(false)
|
|
if p := gcPercent(); p != tightGCPercent {
|
|
t.Fatalf("two reads under GOGC=%d, want %d", p, tightGCPercent)
|
|
}
|
|
if firstEndsFirst {
|
|
first()
|
|
} else {
|
|
second()
|
|
}
|
|
if p := gcPercent(); p != tightGCPercent {
|
|
t.Errorf("one read still in flight under GOGC=%d, want %d", p, tightGCPercent)
|
|
}
|
|
first()
|
|
second()
|
|
if p := gcPercent(); p != 100 {
|
|
t.Errorf("first ends first %v: after both GOGC=%d, want 100", firstEndsFirst, p)
|
|
}
|
|
if l := debug.SetMemoryLimit(-1); l != math.MaxInt64 {
|
|
t.Errorf("first ends first %v: after both the limit is %d", firstEndsFirst, l)
|
|
}
|
|
}
|
|
}
|
|
|
|
// The warm-up's usage read is the one under it, and the warm-up's other
|
|
// reads are not.
|
|
func TestWarmUpTightensOnlyTheUsageRead(t *testing.T) {
|
|
defer debug.SetGCPercent(debug.SetGCPercent(100))
|
|
var during int
|
|
warmUp(func() { during = gcPercent() })
|
|
if during != 100 {
|
|
t.Fatalf("a warm-up step read under GOGC=%d, want 100", during)
|
|
}
|
|
}
|