stocks / docs /performance.md
user
python version
ee446e8
|
Raw History Blame Contribute Delete
6.73 kB
# Performance
LT tracks wall-clock performance through a serial benchmark suite (`tests/perf/`,
run with `make perf-benchmark`) plus a baseline comparison gate (`make perf-check`).
Frontend timing on `/discover` comes from instrumentation already in the page.
This document records where each number lives, how to re-check it, and what the
gates decide on.
## 1. Frontend: /discover page timings
`static/js/features/discover/discover-page.ts` instruments every Discover fetch
with `emitDiscoverPerformance(stage, startedAt, requestSeq)`. Stages:
- `fetch:start` β€” request begins, AbortController created
- `flags` β€” symbol flags loaded (cached after the first call)
- `watchlist` β€” watchlist state refreshed (skipped on background polls)
- `screener` β€” `/api/screener` response committed
- `primary-render` β€” table rendered after `applyDiscoverWave`
Each stage is pushed onto `window.__discoverPerformance` (capped at 100 entries),
dispatched as a `discover-performance` CustomEvent, and recorded with
`performance.measure("discover:<stage>", {start, end, detail})`.
Re-check in DevTools on `/discover`:
```js
window.__discoverPerformance; // [{stage, durationMs, requestSeq}]
performance.getEntriesByName("discover:screener");
[...document.querySelectorAll(".stock-row--list")].length; // rows painted
```
Charts-mode request counts: open the page, wait for
`#table-container .stock-row`, click
`#discover-view-toggle .view-btn[data-mode="charts"]`, wait, then read
`performance.getEntriesByType("resource")` and count by path. A scratch copy in
`/tmp` is enough; it does not belong in the repo.
Backend bounds for one Discover warm:
- `/api/screener` posts with `limit: 50`; one warm is at most 60 Finviz
`/stock` requests (`GROUP_WARM_LIMIT = 60`, also the backend's default warm
limit in `DiscoverDescriptionWarmRequest` in `core/domain/schemas.py`). Rows
past 60 are described by the next group run, not by this press.
- `/api/discover/patterns` groups the universe rows (`min_support` from
`GROUP_MIN_SUPPORT`). The response cache key hashes only the stable row fields
(`_PATTERN_ROW_FIELDS`, `modules/discover/routes.py`) plus a digest of the
described descriptions; TTL is `_PATTERN_CACHE_TTL = 300` seconds. Volatile
quote/timestamp fields are intentionally excluded so an unrelated quote
change does not fragment the cache.
- `/api/discover/descriptions/warm` populates the description cache: one Finviz
request per symbol, bounded by `min(req.limit, DISCOVER_DESCRIPTIONS.max_symbols)`,
run under a semaphore so a hundred concurrent scrapes cannot rate-limit the
whole provider. Symbols already cached are counted as cached, not refetched.
The warm is keyed on session `|` sorted symbol list, so re-running the same
universe does not spend the provider twice.
- Webull fallback cache key is `webull_fallback_{session}_{direction}.json`;
session/direction are validated with `plain_token`, so they can never reach
the filesystem outside the cache directory.
## 2. Backend benchmark suite
`tests/perf/` holds the serial perf suite, every file marked `pytest.mark.perf`.
`make test` deselects it (`-m "not e2e and not perf"`) because a wall-clock
number under `pytest -n auto` measures the scheduler as much as the code.
Commands:
```sh
make perf-benchmark # serial run, writes tests/perf/latest.json
make perf-check # compares latest.json against tests/perf/baseline.json
```
`make perf-benchmark` runs `pytest tests/perf/ --benchmark-json=tests/perf/latest.json`.
`make perf-check` runs that suite, then compares each benchmark's mean against
`tests/perf/baseline.json` with `LIMIT = 1.20` (20% over baseline fails).
Gate rules (`scripts/perf_check.py`):
- Decides on the **mean**. Percentiles move with round count and are only for
reporting and for the p95 target (a measured figure, not pass/fail).
- A baseline entry with no benchmark behind it is reported as stale and does not
fail. A benchmark with no baseline entry is unbaselined: its numbers print
without a ratio.
Baseline (`tests/perf/baseline.json`, committed):
| benchmark | mean |
| --- | --- |
| `test_get_candidates_benchmark[gap-volume]` | 0.4546 s |
| `test_get_candidates_benchmark[relative-volume]` | 0.3510 s |
| `test_compile_benchmark` | 4.55e-05 s |
Other serial budget files:
- `tests/perf/test_scan_stage_budget.py` β€” scan-stage gate (`core/dsl`, `ScanService`,
`enrich_session_frame`); wall-clock, belongs here not in the parallel suite.
- `tests/perf/test_live_budget.py` β€” P3 live budget: 500 symbols within one
second (moved from `tests/dsl/test_dsl_detector_series.py` for the same xdist
reason).
- `tests/perf/test_series_eval_budget.py` β€” per-symbol whole-frame gate for
`core/dsl/series_eval.py` (`ENRICH_BUDGET_MS`); absolute, uncalibrated budget
(the Β§6 target from the removed performance plan is not reachable without
changing what the function returns).
- `tests/perf/test_calendar_export_budget.py` β€” PERF-P2 calendar-export
measurements over real cached minute frames; asserts a perf run rewrites or
deletes nothing in the cache.
Concurrency guards (`tests/infra/`):
- `test_perf_batch_ohlc_singleflight.py` β€” concurrent cold OHLC requests for one
symbol/timeframe share one upstream fetch (singleflight keyed on the route's
cache key).
- `test_perf_batch_cached_stampede.py`, `test_perf_batch_news_feed.py`,
`test_perf_batch_symbol_search_cache.py` β€” batch-cache behavior under repeated
cold requests.
## 3. Removed performance plan
A performance plan (PERF-P1 p95 target, PERF-P2 calendar export, PERF-P3 live
budget) was removed on 2026-10-17; see git history. Its targets survive as the
committed baselines and serial budget files above, not as a separate document.
## 4. Local gates only
Performance checks are local, never remote CI. Full gate: `make ci` (includes
`js`, `lint`, `test`, `test-js`, `smoke-fast`); fast iteration loop:
`make check-fast` then `make verify-diff` (alias `make ci-agent`). Serial perf
runs separately with `make perf-benchmark` / `make perf-check`.
## 5. How to verify a change
1. Reproduce on a quiet machine, serially (`make perf-benchmark`), never under
`pytest -n auto`.
2. Compare against the committed baseline (`make perf-check`).
3. For `/discover`, re-check the DevTools stages above before and after the
change.
4. Confirm no cache file under the cached frame directory is rewritten or
deleted by a perf run (see `test_calendar_export_budget.py`).
## 6. Dates
Dates in this document use `YYYY-MM-DD`. The performance plan was removed
2026-10-17; the serial budget files were moved into `tests/perf/` at the same
time (see git history).