Repository navigation
Track benchmarks over time in CI, starting with one #306
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+192
−4
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
82fa0c5
Track benchmarks over time in CI with pytest-benchmark
d33bs de01939
Prep a dispatch-only CodSpeed job alongside pytest-benchmark
d33bs ef30255
Address CodeRabbit review on PR #306
d33bs c200e44
Keep benchmark data out of mike's gh-pages dev/ folder
d33bs 1ff5809
Merge remote-tracking branch 'origin/main' into pr306
alxmrs ff54793
Run benchmarks on every PR, drop CodSpeed, and document benchmarks
alxmrs d03aa58
Merge remote-tracking branch 'origin/main' into pr306
alxmrs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -81,8 +81,10 @@ jobs: | |
| - name: Run unit tests | ||
| env: | ||
| VIRTUAL_ENV: ${{ github.workspace }}/.venv | ||
| # The ML demos run in their own job, below. | ||
| run: uv run --no-project pytest -v . -m "not integration" --ignore=tests/test_ml_demos.py | ||
| # The ML demos run in their own job, below. --benchmark-disable runs | ||
| # each benchmark once, as a test, so a PR that breaks one fails here | ||
| # rather than in the benchmarks job on main. | ||
| run: uv run --no-project pytest -v . -m "not integration" --benchmark-disable --ignore=tests/test_ml_demos.py | ||
|
|
||
| ml-demos: | ||
| # Each ML demo in benchmarks/ml, at its smallest size, checked against | ||
|
|
@@ -121,3 +123,76 @@ jobs: | |
| env: | ||
| VIRTUAL_ENV: ${{ github.workspace }}/.venv | ||
| run: uv run --no-project pytest -v tests/test_ml_demos.py | ||
|
|
||
| benchmarks: | ||
| # tests/test_benchmarks.py, timed with pytest-benchmark (a pytest | ||
| # plugin that runs a function repeatedly and reports its timings) and | ||
| # stored on gh-pages by github-action-benchmark (an action that keeps | ||
| # each run's results as a history, charts it, and comments on a commit | ||
| # that looks like a regression). | ||
| name: "benchmarks" | ||
| runs-on: ubuntu-latest | ||
| # Only on main: a PR from a fork has no permission to push to | ||
| # gh-pages, and a dispatch from another branch shouldn't record a | ||
| # point into history that doesn't belong to main's timeline. | ||
| if: github.ref == 'refs/heads/main' && (github.event_name == 'push' || github.event_name == 'workflow_dispatch') | ||
| permissions: | ||
| contents: write | ||
| deployments: write | ||
| steps: | ||
| - uses: actions/checkout@v4 | ||
|
|
||
| - uses: dtolnay/rust-toolchain@stable | ||
| id: rust-toolchain | ||
|
|
||
| - name: Setup sccache | ||
| uses: mozilla-actions/[email protected] | ||
|
|
||
| - name: Configure sccache | ||
| run: | | ||
| echo "SCCACHE_GHA_ENABLED=true" >> $GITHUB_ENV | ||
| echo "RUSTC_WRAPPER=sccache" >> $GITHUB_ENV | ||
|
|
||
| - name: Cache Cargo | ||
| uses: actions/cache@v4 | ||
| with: | ||
| path: ~/.cargo | ||
| key: cargo-cache-${{ steps.rust-toolchain.outputs.cachekey }}-${{ hashFiles('Cargo.lock') }} | ||
|
|
||
| - uses: astral-sh/setup-uv@v5 | ||
| with: | ||
| python-version: "3.12" | ||
| enable-cache: true | ||
|
|
||
| - name: Install xarray_sql | ||
| run: uv sync --locked --dev --extra benchmark --no-install-package xarray-sql | ||
| - name: build rust | ||
| run: uv run --no-project maturin develop --uv | ||
| - name: Run benchmarks | ||
| env: | ||
| VIRTUAL_ENV: ${{ github.workspace }}/.venv | ||
| run: uv run --no-project pytest tests/test_benchmarks.py -m benchmark --benchmark-only --benchmark-json=benchmark.json | ||
| - name: Store benchmark result | ||
| uses: benchmark-action/github-action-benchmark@v1 | ||
| with: | ||
| name: "Python benchmarks (pytest-benchmark)" | ||
| tool: "pytest" | ||
| output-file-path: benchmark.json | ||
| github-token: ${{ secrets.GITHUB_TOKEN }} | ||
| # The action retries a push that gh-pages rejects (up to 10 times, | ||
| # re-cloning gh-pages each time), so it can share the branch with | ||
| # mike's pushes from docs.yml. A new point goes live when docs.yml | ||
| # next publishes gh-pages to Pages, which may be one push to main | ||
| # later if that publish checked out gh-pages before this push. | ||
| auto-push: true | ||
| # mike (docs.yml) owns dev/, 0.x/, latest/, index.html and | ||
| # versions.json on gh-pages, and replaces dev/ wholesale on | ||
| # every docs deploy; the default dev/bench would collide | ||
| # with that and get wiped out. Use a root-level path mike | ||
| # never touches. | ||
| benchmark-data-dir-path: benchmarks | ||
| # Hosted runners are noisy; keep this loose until there's | ||
| # enough history on gh-pages to pick a tighter threshold. | ||
| alert-threshold: "200%" | ||
| comment-on-alert: true | ||
| fail-on-alert: false | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| """A benchmark tracked over time in CI (see CONTRIBUTING.md). | ||
|
|
||
| It uses the ``benchmark`` fixture of pytest-benchmark | ||
| (https://github.com/ionelmc/pytest-benchmark), a pytest plugin that runs | ||
| a function repeatedly and reports its timings. On every push to main, CI | ||
| stores the timings with github-action-benchmark | ||
| (https://github.com/benchmark-action/github-action-benchmark), which | ||
| keeps them as a history and flags a commit that looks like a regression. | ||
|
|
||
| Starts small: one benchmark, over an in-memory Dataset that needs no | ||
| network, so a regression in xarray-sql, DataFusion or the in-memory | ||
| table adapter shows up in that history. | ||
| """ | ||
|
|
||
| import numpy as np | ||
| import pytest | ||
| import xarray as xr | ||
|
|
||
| from xarray_sql import Context | ||
|
|
||
| pytestmark = pytest.mark.benchmark | ||
|
|
||
|
|
||
| @pytest.fixture(scope="module") | ||
| def grid() -> xr.Dataset: | ||
| """A synthetic (time, lat, lon) grid: 100 x 50 x 50 cells, no I/O.""" | ||
| rng = np.random.default_rng(0) | ||
| time, lat, lon = 100, 50, 50 | ||
| return xr.Dataset( | ||
| { | ||
| "temperature": ( | ||
| ("time", "lat", "lon"), | ||
| rng.normal(size=(time, lat, lon)), | ||
| ), | ||
| }, | ||
| coords={ | ||
| "time": np.arange(time), | ||
| "lat": np.arange(lat), | ||
| "lon": np.arange(lon), | ||
| }, | ||
| ) | ||
|
|
||
|
|
||
| def test_groupby_mean_over_time(benchmark, grid): | ||
| """A climatology query: mean over time, grouped by lat and lon.""" | ||
| ctx = Context() | ||
| ctx.from_dataset("grid", grid) | ||
|
|
||
| def run(): | ||
| return ctx.sql( | ||
| "SELECT lat, lon, AVG(temperature) AS mean_temp " | ||
| 'FROM "grid" GROUP BY lat, lon ORDER BY lat, lon' | ||
| ).to_dataset() | ||
|
|
||
| result = benchmark(run) | ||
| assert result.sizes["lat"] == grid.sizes["lat"] | ||
| assert result.sizes["lon"] == grid.sizes["lon"] | ||
| expected = grid["temperature"].mean("time") | ||
| np.testing.assert_allclose(result["mean_temp"].values, expected.values) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.