Skip to content

Stop /historical/json/ from querying once per result - #125

Open
konradvoelkel wants to merge 1 commit into
python:mainfrom
konradvoelkel:historical-select-related
Open

konradvoelkel wants to merge 1 commit into
python:mainfrom
konradvoelkel:historical-select-related

Conversation

@konradvoelkel

Copy link
Copy Markdown

gethistoricaldata() assembles its response from Result querysets it keeps around, reading res.benchmark as it goes. Without select_related('benchmark') that costs one extra query per result.

Measured by rendering /historical/json/ under CaptureQueriesContext with one executable, 12 revisions and a growing number of legacy benchmarks:

legacy benchmarks before after
60 197 queries 17
200 617 queries 17

The response body is byte for byte identical before and after (same sha256, 14644 bytes at 200 benchmarks); select_related only changes how the rows are fetched.

The test

TestHistoricalDataQueries pins the invariant rather than a query count, so it will not need adjusting whenever an unrelated query is added or dropped: the number of queries must not grow when the number of benchmarks does. Before this change it fails with

AssertionError: 144 != 44 : 144 queries for 25 benchmarks against 44 for 5: the view queries per result

Notes for a reviewer

  • The endpoint is cached for 30 minutes (HISTORICAL_CACHE_SECONDS), so this is about cache misses — the first request after a deploy or an upload, which is also the slow one.
  • Three call sites get the same treatment: the baseline results, the tagged revisions, and the "latest" loop. The remaining Result.objects.filter(...) calls in views.py either use values_list() or already have select_related.
  • While measuring I noticed something separate and did not touch it here: /historical/json/ raises AttributeError on any site that does not define DEF_BASELINES/DEF_EXECUTABLES, because those are read without the hasattr guard the rest of views.py uses, and codespeed/settings.py only ships the long-renamed singular DEF_BASELINE. Happy to open an issue if it is not already known.

🤖 Generated with Claude Code

gethistoricaldata() holds on to Result querysets and reads res.benchmark
when it assembles the response, so each result costs an extra query. With
200 legacy benchmarks over 12 revisions the endpoint issued 617 queries;
with select_related('benchmark') it issues 17, and the response is byte for
byte identical (same sha256, 14644 bytes).

The test pins the invariant rather than a number: the query count must not
grow when the number of benchmarks does. Before the change it fails with
144 queries for 25 benchmarks against 44 for 5.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant