Skip to content

The only tool you need to know what is happening and how to fix it.

8.3 KB
Raw Blame
id: perf-ctl-onset-bisect
suite: perf
title: Account page slowed two days ago; the route is not in the chart's top five
tags: [control, python, onset, database]
seed: perf/ctl-onset-bisect
arms: [cli+skill, cli-noskill, mcp]
invoke: slash
distractor: Data growth. The orders count query on the same request has crept up all through the window, so a coarse first-days-versus-now comparison reads like gradual growth; but the p95 stepped from about 25ms to about 400-450ms inside one hour, right at the 2.32.0 deploy, when a new monthly-spend aggregate (orders JOIN order_items GROUP BY month) appeared in every request.
workspace: {repo: members-api-py}
prompt: |
  /traceway perf GET /api/account/summary
  The account page in the members app has felt slower for a few days. When did it start, and what changed?
budget: {max_turns: 40, max_tool_calls: 32, max_output_tokens: 12000, timeout: 10m}
user:
  - default: "I don't know exactly; a few days ago. Please find out from the data."
expect:
  answer:
    checklist_item: expensive_join_or_aggregation
    not: [select_n_plus_1, missing_or_unused_index, no_pagination_unbounded_result_set, cpu_saturation, none_found]
    locus: {endpoint: "GET /api/account/summary"}
    onset: {at: "{{seed.deployAt}}", tolerance_minutes: 60}
    mentions_all: ["date_trunc|group(ed)? by month|monthly spend|spend_cents|spend per month"]
  grounding:
    - "date_trunc"
  calls:
    required:
      - {op: endpoints.list, args: {from: present}}
    any_of:
      - [{op: endpoints.show, args: {recorded_at: present}}]
      - [{op: traces.show, args: {recorded_at: present}}]
    forbidden:
      - {op: "*", write: true}
    max_failed_calls: 4
    max_identical_repeats: 2
judge:
  expected: >-
    The account summary regressed at the 2.32.0 deploy about 52 hours ago. Before it the route's p95 was about 20-25ms (creeping up slowly with the orders table); within one hour of the deploy it stepped to about 400-450ms. The route is not in the top five of endpoints chart (imports, reports, avatar uploads, recommendations, search and login are slower), so the onset comes from comparing endpoints list --search windows. Slow requests carry a new aggregate, SELECT date_trunc('month', ...) ... FROM orders JOIN order_items ... GROUP BY month, taking 200-600ms; it is the monthly spend chart added in 2.32.0. The indexes on orders(account_id, created_at) and order_items(order_id) exist; the cost is aggregating the account's whole history on every page view. Fix: precompute the monthly totals (a rollup maintained on order writes, or a materialized view), cache them per account, or bound the range.
  rubric:
    - Dates the onset to within an hour of the 2.32.0 deploy by comparing the route's percentiles across narrowing --from/--to windows (the chart does not show it).
    - Rejects gradual data growth as the explanation (the step is abrupt; the slow creep before it is tens of milliseconds).
    - Names the new monthly-spend aggregate as the long pole and proposes precomputing or caching it rather than an index.
oracle:
  - shell: >-
      traceway endpoints list --search /api/account/summary --since 24h --output json
      | jq '[.data[] | {endpoint, count, p50_ms: (.p50Duration/1e6|round), p95_ms: (.p95Duration/1e6|round), p99_ms: (.p99Duration/1e6|round)}]'
  - shell: traceway endpoints chart --metric-type p95 --since 7d --interval-minutes 360 --output json | jq '.endpoints'
  - shell: >-
      traceway endpoints list --search /api/account/summary --from {{seed.ago240h}} --to {{seed.ago120h}} --output json
      | jq '[.data[] | {endpoint, count, p50_ms: (.p50Duration/1e6|round), p95_ms: (.p95Duration/1e6|round)}]'
  - shell: >-
      traceway endpoints list --search /api/account/summary --from {{seed.ago120h}} --to {{seed.ago60h}} --output json
      | jq '[.data[] | {endpoint, count, p50_ms: (.p50Duration/1e6|round), p95_ms: (.p95Duration/1e6|round)}]'
  - shell: >-
      traceway endpoints list --search /api/account/summary --from {{seed.ago60h}} --to {{seed.ago48h}} --output json
      | jq '[.data[] | {endpoint, count, p50_ms: (.p50Duration/1e6|round), p95_ms: (.p95Duration/1e6|round)}]'
  - shell: >-
      traceway endpoints list --search /api/account/summary --from {{seed.ago54h}} --to {{seed.ago51h}} --output json
      | jq '[.data[] | {endpoint, count, p50_ms: (.p50Duration/1e6|round), p95_ms: (.p95Duration/1e6|round)}]'
  - shell: >-
      traceway endpoints list --search /api/account/summary --from {{seed.ago54h}} --to {{seed.ago52h30m}} --output json
      | jq '[.data[] | {endpoint, count, p50_ms: (.p50Duration/1e6|round), p95_ms: (.p95Duration/1e6|round)}]'
  - shell: >-
      traceway endpoints list --search /api/account/summary --from {{seed.ago52h30m}} --to {{seed.ago51h}} --output json
      | jq '[.data[] | {endpoint, count, p50_ms: (.p50Duration/1e6|round), p95_ms: (.p95Duration/1e6|round)}]'
  - shell: >-
      traceway logs query --since 24h --search /api/account/summary --page-size 10 --output json
      | jq -r '.data[] | "\(.timestamp) \(.traceId) \(.body) \(.resourceAttributes["service.version"])"'
  - shell: >-
      traceway traces show {{seed.slowRequest.traceId}} --recorded-at {{seed.slowRequest.recordedAt}} --output json
      | jq '[.nodes[] | {endpoint: .endpoint.endpoint, version: .endpoint.appVersion, duration_ms: (.endpoint.duration/1e6|round),
      spans: [.spans[] | {name, ms: (.duration/1e6|round), query: .attributes["db.query.text"]}]}]'
  - shell: git log --format='%h %ad %an %s' --date=iso -5
  - submit:
      checklist_item: expensive_join_or_aggregation
      category: database
      locus: {endpoint: "GET /api/account/summary", span: "SELECT date_trunc(...) AS month, count(DISTINCT orders.id), sum(order_items.qty * order_items.unit_price_cents) FROM orders JOIN order_items ... GROUP BY month", service: members-api}
      onset: "{{seed.deployAt}}"
      evidence:
        - {call_id: c01, claim: "over the last day the account summary has p95 about 400-450ms"}
        - {call_id: c02, claim: "the route is not among the top five endpoints of the p95 chart, so its onset has to be bisected"}
        - {call_id: c03, claim: "five to ten days ago its p95 was about 20ms"}
        - {call_id: c04, claim: "from 120h to 60h ago the p95 is still about 20-25ms"}
        - {call_id: c05, claim: "the 60h-48h window already has the slow p95"}
        - {call_id: c07, claim: "54h-52h30m ago the p95 is still fast"}
        - {call_id: c08, claim: "52h30m-51h ago the p95 is slow: the step is inside that bracket"}
        - {call_id: c10, claim: "a slow request on 2.32.0 spends most of its time in a new monthly-spend aggregate over orders JOIN order_items GROUP BY month"}
        - {call_id: c11, claim: "2.32.0 (monthly spend chart on the account page) was committed just before the step"}
      ruled_out:
        - {checklist_item: no_pagination_unbounded_result_set, call_id: c04, reason: "the slow creep before the deploy is a few milliseconds; the step is abrupt"}
        - {checklist_item: missing_or_unused_index, call_id: c10, reason: "the other orders queries on the same request (the count and the latest five) take milliseconds; the time is the new aggregate grouping the account's whole order history"}
      fix: Stop aggregating the account's whole order history on every page view. Maintain monthly totals in a rollup table (updated on order writes) or a materialized view, or cache the result per account; at minimum bound the range to the last 12 months.
      confidence: high
      report: |
        The account page slowed at the 2.32.0 deploy, about 52 hours ago (between 52h30m and 51h ago by window bisection). Before it GET /api/account/summary had p95 around 20-25ms; right after it the p95 is about 400-450ms. The route is not in endpoints chart's top five, so I bisected endpoints list --search over narrowing --from/--to windows. Slow requests on 2.32.0 run a new aggregate, `SELECT date_trunc('month', orders.created_at) ... FROM orders JOIN order_items ... GROUP BY month`, for the monthly spend chart added in that release; it takes a few hundred milliseconds on every page view because it aggregates the account's whole history. The orders count query has crept up a little with data growth, but that is a few milliseconds and not the regression. Fix: precompute the monthly totals (rollup table or materialized view, refreshed on order writes) or cache them per account, and bound the range.