carlos / platform Public

console perf tests: wait for the recorder's sample before reading it

The perf middleware records a request's sample when the handler returns,
which can be after the client has the whole response. Tests that read the
recorder (or the /fleet/metrics page) right after a request raced that, so
TestFleetMetricsSpeedRowsNameTheMethod lacked the GET row under CI load.
waitPerfRequests polls the recorder, bounded at 10 s, for the request count
before each read; the sibling tests with the same pattern use it too.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev 5d6419b594de0f01a77c563e904e5670c0482046 parent f30df0e
3 files changed, +45 −0
  • internal/console/perflatency_test.go +10 −0
  • internal/console/perfspeed_test.go +6 −0
  • internal/console/perfwiring_test.go +29 −0

CI passed — run details

diff --git a/internal/console/perflatency_test.go b/internal/console/perflatency_test.go
index f71939914..7ed8a30c7 100644
--- a/internal/console/perflatency_test.go
+++ b/internal/console/perflatency_test.go
@@ -178,12 +178,22 @@ func TestFixtureOverTheS3FakeServesAppPage(t *testing.T) {
}
return 0
}
+ pageRequests := func() (n int) {
+ for _, g := range fx.srv.PerfGroups() {
+ if g.Screen == screen && g.Method == "GET" {
+ n += g.Requests
+ }
+ }
+ return n
+ }
coldFrames := func() { fx.srv.frames.clearWhere(func(frameKey) bool { return true }) }
view := func() (fake, sample, background, host int64) {
t.Helper()
s0, h0 := lat.Calls()
p0, b0 := pageCalls(), fx.srv.PerfBackground()["s3"]
+ r0 := pageRequests()
resp, body := perfGet(t, c, fx.u("/apps/kass"))
+ waitPerfRequests(t, fx.srv, screen, "GET", r0+1)
if resp.StatusCode != http.StatusOK {
t.Fatalf("GET app page over the S3 fake = %d:\n%s", resp.StatusCode, body)
}
diff --git a/internal/console/perfspeed_test.go b/internal/console/perfspeed_test.go
index 2f9e22ac0..27020e10d 100644
--- a/internal/console/perfspeed_test.go
+++ b/internal/console/perfspeed_test.go
@@ -59,6 +59,10 @@ func TestFleetMetricsSpeedRowsNameTheMethod(t *testing.T) {
t.Fatal(err)
}
resp.Body.Close()
+ // The sample lands when the handler returns, which can be after the
+ // client has its response (a closed body never waits for it), and
+ // the metrics page reads whatever has landed.
+ waitPerfRequests(t, fx.srv, "/fleet/architecture", m, 1)
}
status, _, body := get(t, c, fx.ts.URL+"/fleet/metrics")
if status != 200 {
@@ -101,6 +105,7 @@ func TestFleetMetricsShowsConsoleSpeed(t *testing.T) {
if status, _, _ := get(t, c, fx.u("/apps/kass")); status != 200 {
t.Fatalf("app page = %d, want 200", status)
}
+ waitPerfRequests(t, fx.srv, "GET /a/{sqid}/apps/{app}", "GET", 1)
status, _, body := get(t, c, fx.ts.URL+"/fleet/metrics")
if status != 200 {
t.Fatalf("/fleet/metrics = %d: %s", status, body)
@@ -132,6 +137,7 @@ func TestFleetMetricsConsoleSpeedEmptyState(t *testing.T) {
t.Errorf("first /fleet/metrics lacks the empty state:\n%s", body)
}
// The metrics GET is measured once it has rendered, so the next view has a row.
+ waitPerfRequests(t, fx.srv, "GET /fleet/metrics", "GET", 1)
_, _, again := get(t, c, fx.ts.URL+"/fleet/metrics")
if strings.Contains(again, "No requests measured since the console started.") {
t.Errorf("second /fleet/metrics still shows the empty state")
diff --git a/internal/console/perfwiring_test.go b/internal/console/perfwiring_test.go
index 0ca62c495..e300d9372 100644
--- a/internal/console/perfwiring_test.go
+++ b/internal/console/perfwiring_test.go
@@ -38,6 +38,30 @@ func perfGet(t *testing.T, c *http.Client, u string) (*http.Response, string) {
return resp, string(body)
}
+// waitPerfRequests blocks until the recorder holds at least n requests for
+// screen (any method when method is ""), or fails the test after a bound.
+// The middleware records a sample when the handler returns, which can be
+// after the client has the whole response, so a test that reads the
+// recorder right after a request must wait for that request's sample.
+func waitPerfRequests(t *testing.T, srv *Server, screen, method string, n int) {
+ t.Helper()
+ count := func() (total int) {
+ for _, g := range srv.PerfGroups() {
+ if g.Screen == screen && (method == "" || g.Method == method) {
+ total += g.Requests
+ }
+ }
+ return total
+ }
+ deadline := time.Now().Add(10 * time.Second)
+ for count() < n {
+ if time.Now().After(deadline) {
+ t.Fatalf("recorder has %d requests for %s %q after 10s, want %d: %+v", count(), method, screen, n, srv.PerfGroups())
+ }
+ time.Sleep(2 * time.Millisecond)
+ }
+}
+
func findGroup(groups []perf.Group, screen string) *perf.Group {
for i := range groups {
if groups[i].Screen == screen {
@@ -72,6 +96,7 @@ func TestPerfGroupsByPattern(t *testing.T) {
fx := newConsoleFixture(t)
c := signInPaul(t, fx)
perfGet(t, c, fx.u("/apps/kass"))
+ waitPerfRequests(t, fx.srv, "GET /a/{sqid}/apps/{app}", "GET", 1)
groups := fx.srv.PerfGroups()
g := findGroup(groups, "GET /a/{sqid}/apps/{app}")
if g == nil || g.Method != "GET" {
@@ -88,6 +113,7 @@ func TestPerfGroupsMethodlessPattern(t *testing.T) {
fx := newConsoleFixture(t)
c := signInPaul(t, fx)
perfGet(t, c, fx.ts.URL+"/fleet/architecture")
+ waitPerfRequests(t, fx.srv, "/fleet/architecture", "GET", 1)
if g := findGroup(fx.srv.PerfGroups(), "/fleet/architecture"); g == nil {
t.Fatalf("no group for /fleet/architecture in %+v", fx.srv.PerfGroups())
}
@@ -98,6 +124,8 @@ func TestPerfGroupsNeverCarryPaths(t *testing.T) {
c := newBrowser(t)
perfGet(t, c, fx.ts.URL+"/invite/tok-abc123")
perfGet(t, c, fx.ts.URL+"/no/such/path")
+ waitPerfRequests(t, fx.srv, "GET /invite/{token}", "GET", 1)
+ waitPerfRequests(t, fx.srv, "unmatched", "", 1)
groups := fx.srv.PerfGroups()
for _, g := range groups {
if strings.Contains(g.Screen, "tok-abc123") || strings.Contains(g.Screen, "/no/such/path") {
@@ -263,6 +291,7 @@ func TestRunningTabTimesTheAppHost(t *testing.T) {
t.Errorf("Server-Timing = %q, want a box span", st)
}
// And the call is counted on that page's own sample, once.
+ waitPerfRequests(t, fx.srv, "GET /a/{sqid}/apps/{app}/instances", "GET", 1)
var found bool
for _, smp := range fx.srv.perf.Snapshot() {
if smp.Screen != "GET /a/{sqid}/apps/{app}/instances" {