An idle magpie serve still woke ~6 times a second with nothing to do. Its loops each kept a ticker of their own, started at their own instant: awake.Keep (5s), the plugin config's KeepFresh (5s), the middleware observer (5s), OTel's session watch (5s idle), the Codex daemon check (15s), dsh's wiring (30s), the model lists' look (1m). Every one woke the process apart, a few threads each time. internal/beat's Next(every) fires at the next multiple of every on the clock, so the 5s loops come due at one instant, the 15s and 30s ones on every third and sixth of it, and the runtime runs timers due together in one wakeup. Each loop now waits on beat.Next instead of its ticker. No channel is shared between callers (a synctest bubble's channel reached from another crashes the process, #3632). Run at this head (05ea29ac8+ this): - Sandbox HOME serve, idle, 90s warm-up then 120s of proc_pid_rusage (pkg_idle_wkups + interrupt_wkups), old (origin/main19253f67b) and new taking turns: old 6.1, 5.3, 6.2 wakeups/s; new 2.7, 2.6 (an earlier shared-timer form of beat), 3.0 (this one). CPU 0.20-0.36 ms/s old, 0.11-0.22 new. - TestIntervalsMeet (5s/15s/30s/1m beats from 09:00:03.2 and on a beat), TestNextFires; go test ./internal/beat ./internal/awake ./internal/middleware ./internal/otlp -count=3, ./internal/pluginrt, ./internal/agent -run 'Dsh|CodexDaemon', ./internal/provider -skip TestUnreadLoginsWriteErrorsKeepCredentials (#3632): ok. ./internal/pluginrt -count=3 fails TestKeepFreshFollowsPluginsYml ("two plugins fresh-test") on origin/main too; -count=1 is ok. - gofmt; go vet darwin, GOOS=linux and GOOS=windows -tags nogui. Not tried: a GUI build's wakeups (the tray process has AppKit's own).
40 lines
1.3 KiB
Go
40 lines
1.3 KiB
Go
package beat
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// Loops whose intervals are multiples of one another wake at one time: a
|
|
// 5s loop's every third beat is a 15s loop's, and its every sixth a 30s
|
|
// loop's.
|
|
func TestIntervalsMeet(t *testing.T) {
|
|
now := time.Date(2026, 10, 26, 9, 0, 3, 200e6, time.UTC)
|
|
for _, c := range []struct {
|
|
every time.Duration
|
|
want time.Time
|
|
}{
|
|
{5 * time.Second, time.Date(2026, 10, 26, 9, 0, 5, 0, time.UTC)},
|
|
{15 * time.Second, time.Date(2026, 10, 26, 9, 0, 15, 0, time.UTC)},
|
|
{30 * time.Second, time.Date(2026, 10, 26, 9, 0, 30, 0, time.UTC)},
|
|
{time.Minute, time.Date(2026, 10, 26, 9, 1, 0, 0, time.UTC)},
|
|
} {
|
|
if got := after(now, c.every); !got.Equal(c.want) {
|
|
t.Errorf("after(%s, %s) = %s, want %s", now.Format(time.TimeOnly), c.every, got.Format(time.TimeOnly), c.want.Format(time.TimeOnly))
|
|
}
|
|
}
|
|
// on a beat, the next is the one after it
|
|
if got := after(time.Date(2026, 10, 26, 9, 0, 5, 0, time.UTC), 5*time.Second); !got.Equal(time.Date(2026, 10, 26, 9, 0, 10, 0, time.UTC)) {
|
|
t.Errorf("the beat after one on 9:00:05 is %s", got.Format(time.TimeOnly))
|
|
}
|
|
}
|
|
|
|
// A beat comes: a 10ms one well within 10s.
|
|
func TestNextFires(t *testing.T) {
|
|
select {
|
|
case <-Next(10 * time.Millisecond):
|
|
case <-time.After(10 * time.Second):
|
|
t.Fatal("a 10ms beat didn't come in 10s")
|
|
}
|
|
}
|