carlos / platform Public

console: perf-budgets.txt grammar and the screen inventory from registerRoute

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev 51756632f788eea36a2a3e45574df51bcefb9046 parent 143055f
2 files changed, +305 −0
  • internal/console/perfbudgets_test.go +289 −0
  • internal/console/testdata/perf-budgets.txt +16 −0
diff --git a/internal/console/perfbudgets_test.go b/internal/console/perfbudgets_test.go
new file mode 100644
index 000000000..11bf47f0e
--- /dev/null
+++ b/internal/console/perfbudgets_test.go
@@ -0,0 +1,289 @@
+package console
+
+import (
+ "bufio"
+ "errors"
+ "fmt"
+ "io"
+ "os"
+ "slices"
+ "strconv"
+ "strings"
+ "testing"
+)
+
+// perfRecord is one line of testdata/perf-budgets.txt. Kind is "over", "skip",
+// "deferred" or "calls"; Calls is set only for "calls". Line is the 1-based
+// line in the file, for messages.
+type perfRecord struct {
+ Screen, Kind, Reason string
+ Calls, Line int
+}
+
+// parsePerfBudgets reads the budgets file: one record per line, blank lines
+// and lines whose first non-blank character is # ignored. A record is
+//
+// <screen> over|skip|deferred: <reason>
+// <screen> calls <n>: <reason>
+//
+// where <screen> is a registered pattern verbatim, "METHOD /path" with an
+// upper-case method or a bare "/path" for a method-less registration. Every
+// bad line is reported, in one error, with its line number.
+func parsePerfBudgets(r io.Reader) (map[string]perfRecord, error) {
+ out := map[string]perfRecord{}
+ var errs []error
+ bad := func(n int, format string, a ...any) {
+ errs = append(errs, fmt.Errorf("line %d: %s", n, fmt.Sprintf(format, a...)))
+ }
+ sc := bufio.NewScanner(r)
+ n := 0
+ for sc.Scan() {
+ n++
+ line := strings.TrimSpace(sc.Text())
+ if line == "" || strings.HasPrefix(line, "#") {
+ continue
+ }
+ screen, rest, ok := perfSplitScreen(line)
+ if !ok {
+ switch f := strings.Fields(line)[0]; {
+ case strings.HasPrefix(f, "/"):
+ bad(n, "no kind after the screen %q (want over:, calls <n>:, deferred: or skip:)", f)
+ case f != strings.ToUpper(f):
+ bad(n, "the method in %q must be upper-case", f)
+ default:
+ bad(n, "want a screen (METHOD /path or /path), got %q", f)
+ }
+ continue
+ }
+ head, reason, found := strings.Cut(rest, ":")
+ if !found {
+ bad(n, "%s: want over:, calls <n>:, deferred: or skip: and a reason", screen)
+ continue
+ }
+ reason = strings.TrimSpace(reason)
+ rec := perfRecord{Screen: screen, Reason: reason, Line: n}
+ kind := strings.Fields(head)
+ switch {
+ case len(kind) == 1 && (kind[0] == "over" || kind[0] == "skip" || kind[0] == "deferred"):
+ rec.Kind = kind[0]
+ case len(kind) >= 1 && kind[0] == "calls":
+ if len(kind) != 2 {
+ bad(n, "%s: calls wants a count, as in \"calls 2: reason\"", screen)
+ continue
+ }
+ c, err := strconv.Atoi(kind[1])
+ if err != nil || c < 1 {
+ bad(n, "%s: calls wants a whole number of at least 1, got %q", screen, kind[1])
+ continue
+ }
+ rec.Kind, rec.Calls = "calls", c
+ default:
+ bad(n, "%s: unknown kind %q (want over, calls <n>, deferred or skip)", screen, strings.TrimSpace(head))
+ continue
+ }
+ if reason == "" {
+ bad(n, "%s: %s needs a reason after the colon", screen, rec.Kind)
+ continue
+ }
+ if prev, dup := out[screen]; dup {
+ bad(n, "duplicate screen %q (first on line %d)", screen, prev.Line)
+ continue
+ }
+ out[screen] = rec
+ }
+ if err := sc.Err(); err != nil {
+ errs = append(errs, err)
+ }
+ if len(errs) > 0 {
+ return nil, errors.Join(errs...)
+ }
+ return out, nil
+}
+
+// perfSplitScreen cuts a trimmed line into its screen and the text after it.
+// ok is false when the line does not begin with an upper-case method and a
+// path, or a bare path, followed by something.
+func perfSplitScreen(line string) (screen, rest string, ok bool) {
+ f := strings.Fields(line)
+ if strings.HasPrefix(f[0], "/") {
+ rest = strings.TrimSpace(strings.TrimPrefix(line, f[0]))
+ return f[0], rest, rest != ""
+ }
+ if len(f) < 2 || !strings.HasPrefix(f[1], "/") || f[0] != strings.ToUpper(f[0]) {
+ return "", "", false
+ }
+ rest = strings.TrimSpace(strings.TrimPrefix(strings.TrimSpace(strings.TrimPrefix(line, f[0])), f[1]))
+ return f[0] + " " + f[1], rest, rest != ""
+}
+
+// loadPerfBudgets reads testdata/perf-budgets.txt.
+func loadPerfBudgets(t testing.TB) map[string]perfRecord {
+ t.Helper()
+ f, err := os.Open("testdata/perf-budgets.txt")
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer f.Close()
+ recs, err := parsePerfBudgets(f)
+ if err != nil {
+ t.Fatalf("testdata/perf-budgets.txt:\n%v", err)
+ }
+ return recs
+}
+
+// perfInventory is the screens the walk can request: every registered route
+// that answers GET (pattern "GET /x" or method-less "/x"), minus the CLI JSON
+// doors under /api/ and /healthz. Sorted.
+func perfInventory(entries []claimEntry) []string {
+ var out []string
+ for _, e := range entries {
+ if e.Kind != "route" || !(strings.HasPrefix(e.Pattern, "GET ") || strings.HasPrefix(e.Pattern, "/")) {
+ continue
+ }
+ path, _ := perfScreenPath(e.Pattern)
+ if path == "/api" || strings.HasPrefix(path, "/api/") || path == "/healthz" {
+ continue
+ }
+ out = append(out, e.Pattern)
+ }
+ slices.Sort(out)
+ return out
+}
+
+// perfScreenPath is a screen's path: the method stripped and a trailing {$}
+// removed. hasParams is true when a {wildcard} remains.
+func perfScreenPath(screen string) (path string, hasParams bool) {
+ path = screen
+ if i := strings.Index(path, " "); i >= 0 {
+ path = path[i+1:]
+ }
+ path = strings.TrimSuffix(path, "{$}")
+ return path, strings.Contains(path, "{")
+}
+
+func TestParsePerfBudgets(t *testing.T) {
+ src := `# a comment, then a blank line
+
+GET /a/{sqid}/apps/{app} calls 2: reads the edges file
+GET /slow over: known slow today
+GET /part deferred: a deferred-part endpoint
+POST /door skip: answers GET with the gate's 404
+/fleet/architecture skip: x
+`
+ got, err := parsePerfBudgets(strings.NewReader(src))
+ if err != nil {
+ t.Fatal(err)
+ }
+ want := map[string]perfRecord{
+ "GET /a/{sqid}/apps/{app}": {Screen: "GET /a/{sqid}/apps/{app}", Kind: "calls", Calls: 2, Reason: "reads the edges file", Line: 3},
+ "GET /slow": {Screen: "GET /slow", Kind: "over", Reason: "known slow today", Line: 4},
+ "GET /part": {Screen: "GET /part", Kind: "deferred", Reason: "a deferred-part endpoint", Line: 5},
+ "POST /door": {Screen: "POST /door", Kind: "skip", Reason: "answers GET with the gate's 404", Line: 6},
+ "/fleet/architecture": {Screen: "/fleet/architecture", Kind: "skip", Reason: "x", Line: 7},
+ }
+ if len(got) != len(want) {
+ t.Fatalf("got %d records, want %d: %+v", len(got), len(want), got)
+ }
+ for k, w := range want {
+ if got[k] != w {
+ t.Errorf("%s: got %+v, want %+v", k, got[k], w)
+ }
+ }
+}
+
+func TestParsePerfBudgetsRefuses(t *testing.T) {
+ src := `# header
+GET /a over:
+GET /b slow: x
+GET /c calls 0: x
+GET /d calls two: x
+GET /e over: first
+GET /e skip: second
+get /f over: x
+GET /g
+/h
+GET /i calls: x
+`
+ _, err := parsePerfBudgets(strings.NewReader(src))
+ if err == nil {
+ t.Fatal("want an error")
+ }
+ msg := err.Error()
+ for _, line := range []string{"line 2", "line 3", "line 4", "line 5", "line 7", "line 8", "line 9", "line 10", "line 11"} {
+ if !strings.Contains(msg, line+":") {
+ t.Errorf("error does not name %s:\n%s", line, msg)
+ }
+ }
+ if strings.Contains(msg, "line 6:") {
+ t.Errorf("the first of a duplicate pair is not at fault:\n%s", msg)
+ }
+ for _, frag := range []string{"reason", "unknown kind", "calls", "duplicate", "upper-case"} {
+ if !strings.Contains(msg, frag) {
+ t.Errorf("error does not mention %q:\n%s", frag, msg)
+ }
+ }
+}
+
+func TestPerfInventoryIncludesMethodlessScreens(t *testing.T) {
+ got := perfInventory([]claimEntry{
+ {Kind: "route", Pattern: "GET /a"},
+ {Kind: "route", Pattern: "/fleet/architecture"},
+ {Kind: "route", Pattern: "POST /b"},
+ {Kind: "route", Pattern: "/api/economics/bill"},
+ {Kind: "route", Pattern: "GET /api/cli/x"},
+ {Kind: "route", Pattern: "GET /healthz"},
+ {Kind: "worker", Pattern: "StartX"},
+ })
+ want := []string{"/fleet/architecture", "GET /a"}
+ if strings.Join(got, "|") != strings.Join(want, "|") {
+ t.Fatalf("got %q, want %q", got, want)
+ }
+}
+
+func TestPerfInventoryOfTheRealConsole(t *testing.T) {
+ fx := newConsoleFixture(t)
+ inv := perfInventory(fx.srv.claimEntries.snapshot())
+ have := map[string]bool{}
+ for _, s := range inv {
+ have[s] = true
+ if strings.HasPrefix(s, "POST ") {
+ t.Errorf("inventory holds %q", s)
+ }
+ if p, _ := perfScreenPath(s); p == "/api" || strings.HasPrefix(p, "/api/") {
+ t.Errorf("inventory holds %q", s)
+ }
+ }
+ for _, want := range []string{
+ "GET /a/{sqid}/apps/{app}",
+ "GET /a/{sqid}/apps/{app}/settings/{section}",
+ "/fleet/architecture",
+ "GET /fleet/metrics",
+ } {
+ if !have[want] {
+ t.Errorf("inventory lacks %q", want)
+ }
+ }
+ if have["GET /healthz"] {
+ t.Error("inventory holds GET /healthz")
+ }
+}
+
+func TestPerfScreenPath(t *testing.T) {
+ for _, c := range []struct {
+ screen, path string
+ params bool
+ }{
+ {"GET /{$}", "/", false},
+ {"GET /a/{sqid}/{$}", "/a/{sqid}/", true},
+ {"/fleet/architecture", "/fleet/architecture", false},
+ } {
+ path, params := perfScreenPath(c.screen)
+ if path != c.path || params != c.params {
+ t.Errorf("%s: got %q, %v; want %q, %v", c.screen, path, params, c.path, c.params)
+ }
+ }
+}
+
+func TestPerfBudgetsFileParses(t *testing.T) {
+ loadPerfBudgets(t)
+}
diff --git a/internal/console/testdata/perf-budgets.txt b/internal/console/testdata/perf-budgets.txt
new file mode 100644
index 000000000..8660a1112
--- /dev/null
+++ b/internal/console/testdata/perf-budgets.txt
@@ -0,0 +1,16 @@
+# Console perf budgets. One record per line; a line whose first character is #
+# is a comment. Read by the perf walk (perfbudgets_test.go).
+#
+# <screen> over: <reason> known slow today; measured and printed, never
+# failed; plan 2 deletes the record
+# <screen> calls <n>: <reason> this screen may make n remote calls on a
+# warm-cache GET (n >= 1)
+# <screen> deferred: <reason> a deferred-part endpoint: timed and printed,
+# exempt from 150 ms and the call count
+# <screen> skip: <reason> not requested (a POST-only door that answers
+# GET with the gate's 404, a JSON door)
+#
+# <screen> is the registered pattern verbatim: "GET /a/{sqid}/apps/{app}" (an
+# upper-case method, a space, the path) or "/fleet/architecture" (a method-less
+# registration). Every record needs a reason after the colon. A screen may
+# appear once.