carlos / platform Public

console: time scale queries by their thread's CPU clock, not wall time

TestOperatorCountsAtScale failed on CI with the dashboard's reads at
183 ms against the 150 ms warm budget, while the same reads take 54 ms
on an idle machine. The gate runs many test processes side by side,
and on a saturated worker even the best of five wall-clock runs mostly
measured the scheduler. Each run is now timed by
CLOCK_THREAD_CPUTIME_ID on a locked OS thread, which leaves out the
time the thread waited to run (wall clock elsewhere; CI is Linux).

Measured on DOCS-T1 with 32 spinners on 16 cores: 129 ms wall against
103 ms CPU, and 52 ms idle. Shared cores and caches still cost
something, but waiting to run no longer counts against the budget.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev e49c209cfe869c850f85530d86267e4e97635b29 parent 991aaf9
3 files changed, +46 −4
  • internal/console/opstore_test.go +3 −4
  • internal/console/scalecost_linux_test.go +30 −0
  • internal/console/scalecost_other_test.go +13 −0
diff --git a/internal/console/opstore_test.go b/internal/console/opstore_test.go
index 1126775e4..dec2bdb71 100644
--- a/internal/console/opstore_test.go
+++ b/internal/console/opstore_test.go
@@ -890,13 +890,12 @@ func TestOperatorCountsAtScale(t *testing.T) {
// 102.5 ms against 100), so one run measures the worker, not the query.
const scaleTimingRuns = 5
-// bestOfScaleRuns runs run scaleTimingRuns times and returns the fastest.
+// bestOfScaleRuns runs run scaleTimingRuns times and returns the fastest,
+// each timed by scaleCost.
func bestOfScaleRuns(run func()) time.Duration {
var best time.Duration
for i := 0; i < scaleTimingRuns; i++ {
- start := time.Now()
- run()
- if took := time.Since(start); i == 0 || took < best {
+ if took := scaleCost(run); i == 0 || took < best {
best = took
}
}
diff --git a/internal/console/scalecost_linux_test.go b/internal/console/scalecost_linux_test.go
new file mode 100644
index 000000000..126da6b0c
--- /dev/null
+++ b/internal/console/scalecost_linux_test.go
@@ -0,0 +1,30 @@
+package console
+
+import (
+ "runtime"
+ "time"
+
+ "golang.org/x/sys/unix"
+)
+
+// scaleCost is the CPU time run takes on its own OS thread. A scale
+// query's budget is what the query costs, and the gate runs many test
+// processes side by side, so on a saturated CI worker even the best of
+// several wall-clock runs measured the scheduler: OperatorCounts' reads
+// came to 183 ms against 150 on CI and 54 ms on an idle machine (#897).
+// The thread's CPU clock leaves out the time the thread waited to run.
+func scaleCost(run func()) time.Duration {
+ runtime.LockOSThread()
+ defer runtime.UnlockOSThread()
+ var before, after unix.Timespec
+ if unix.ClockGettime(unix.CLOCK_THREAD_CPUTIME_ID, &before) != nil {
+ start := time.Now()
+ run()
+ return time.Since(start)
+ }
+ run()
+ if unix.ClockGettime(unix.CLOCK_THREAD_CPUTIME_ID, &after) != nil {
+ panic("CLOCK_THREAD_CPUTIME_ID failed after succeeding")
+ }
+ return time.Duration(after.Nano() - before.Nano())
+}
diff --git a/internal/console/scalecost_other_test.go b/internal/console/scalecost_other_test.go
new file mode 100644
index 000000000..7ca54d1aa
--- /dev/null
+++ b/internal/console/scalecost_other_test.go
@@ -0,0 +1,13 @@
+//go:build !linux
+
+package console
+
+import "time"
+
+// scaleCost is wall-clock time where there is no per-thread CPU clock;
+// the budgets are enforced on CI, which is Linux.
+func scaleCost(run func()) time.Duration {
+ start := time.Now()
+ run()
+ return time.Since(start)
+}