A day's hours were a [24]int64 (192 bytes) whether or not it had any. The owner's real cache has 18,331 files and 19,191 days; 13,406 days had hours, but only 19,107 hours across them had any time, so ~2.6 MB of zeros sat in the idle heap for good. hours is now a sorted []uint64 of hour<<58|ms, one entry per hour with time. Its JSON is still the 24-number array, so sessions.json reads and writes as before; an all-zero array read back keeps one zero entry so the day still counts as having hours. Run on mote at this head: - go test ./internal/sessions: ok - GOOS=darwin/linux/windows go vet ./internal/sessions: ok - gofmt -l: clean - Broken by hand: without hours.MarshalJSON, TestHoursKeepTheCacheShape is red (the packed entries went out as raw numbers). - Real-account copy (~/r2278/acct): idle heap at 180s, t1 (v0.1.3398's tree) vs this: inuse 112.94 MB -> 110.65 MB (-2.3 MB; the 2.6 MB estimate less sampling noise).
55 lines
1.3 KiB
Go
55 lines
1.3 KiB
Go
package sessions
|
|
|
|
import (
|
|
"encoding/json"
|
|
"testing"
|
|
)
|
|
|
|
// TestHoursKeepTheCacheShape: a day's hours read from the cache's 24
|
|
// numbers are written back as the same 24, an hour of 0ms included, and
|
|
// hold only the hours that have some.
|
|
func TestHoursKeepTheCacheShape(t *testing.T) {
|
|
for _, src := range []string{
|
|
`{"h":[0,0,0,0,0,0,0,0,0,1500,0,0,0,0,0,0,0,0,0,0,0,0,0,42]}`,
|
|
`{"h":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]}`,
|
|
`{"a":3}`,
|
|
} {
|
|
var d day
|
|
if err := json.Unmarshal([]byte(src), &d); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(d.Hours) > 2 || cap(d.Hours) > 2 {
|
|
t.Fatalf("%s: %d entries (cap %d)", src, len(d.Hours), cap(d.Hours))
|
|
}
|
|
b, _ := json.Marshal(&d)
|
|
if string(b) != src {
|
|
t.Fatalf("%s came back as %s", src, b)
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestHoursAddInOrder: a day's hours add up per hour, in hour order,
|
|
// as the 24 numbers did.
|
|
func TestHoursAddInOrder(t *testing.T) {
|
|
var hs hours
|
|
var want [24]int64
|
|
for i, h := range []int{9, 3, 23, 9, 0, 3, 12} {
|
|
ms := int64(i*1000 + 7)
|
|
hs.add(h, ms)
|
|
want[h] += ms
|
|
}
|
|
hs.add(5, 0)
|
|
var got [24]int64
|
|
last := -1
|
|
for _, e := range hs {
|
|
if hourOf(e) <= last {
|
|
t.Fatalf("hours out of order: %v", hs)
|
|
}
|
|
last = hourOf(e)
|
|
got[hourOf(e)] = msOf(e)
|
|
}
|
|
if got != want || len(hs) != 6 {
|
|
t.Fatalf("got %v (%d entries), want %v", got, len(hs), want)
|
|
}
|
|
}
|