Something went wrong. Try again.
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849package ui
import ( "testing" "time")
// TestFrameStats averages the latest statFrames frames, forgets older// ones, and keeps them however long the window stays idle.func TestFrameStats(t *testing.T) { var s frameStats if avg, worst := s.costs.summary(); avg != 0 || worst != 0 { t.Errorf("no frames: avg %v, worst %v", avg, worst) } now := time.Unix(1_800_000_000, 0) frame := func(after, cost time.Duration) { now = now.Add(after) s.add(now, cost) } // A slow frame, then statFrames-1 fast ones: all are counted. frame(0, 20*time.Millisecond) for range statFrames - 1 { frame(16*time.Millisecond, 2*time.Millisecond) } avg, worst := s.costs.summary() if want := (20*time.Millisecond + (statFrames-1)*2*time.Millisecond) / statFrames; avg != want { t.Errorf("avg = %v, want %v", avg, want) } if worst != 20*time.Millisecond { t.Errorf("worst = %v, want 20ms", worst) } // One more fast frame pushes the slow one out. frame(16*time.Millisecond, 2*time.Millisecond) if avg, worst := s.costs.summary(); avg != 2*time.Millisecond || worst != 2*time.Millisecond { t.Errorf("after the slow frame left: avg %v, worst %v; want 2ms both", avg, worst) }
// The time between frames is what's seen, however little they cost. if gap, _ := s.intervals.summary(); gap != 16*time.Millisecond { t.Errorf("time between frames = %v, want 16ms", gap) } // A frame after a pause is the window waiting, not drawing slowly. frame(5*time.Second, 2*time.Millisecond) frame(33*time.Millisecond, 2*time.Millisecond) if _, slowest := s.intervals.summary(); slowest != 33*time.Millisecond { t.Errorf("slowest time between frames = %v, want 33ms, the pause left out", slowest) }}