Skip to content

Commit 013014b

Browse files
weiji14seisman
andauthored
Setup Continuous Benchmarking workflow with pytest-codspeed (#2908)
Measuring the execution speed of tests to track performance of PyGMT functions over time. Using pytest-codspeed, see https://docs.codspeed.io/benchmarks/python#running-the-benchmarks-in-your-ci. Decorated a unit test with @pytest.mark.benchmark to see if the benchmarking works. * Pin to Python 3.12 * Add shields.io badge for CodSpeed * Document benchmarks.yml workflow in docs/maintenance.md * Run benchmarks when a release is published * Add benchmarks.yml to bump_gmt_checklist.md * Only benchmark test_basemap for now * Only run when non-test PyGMT source files and benchmarks.yml is modified Trigger the benchmark run when files in `pygmt/clib`, `pygmt/datasets`, `pygmt/helpers`, `pygmt/src` and `pygmt/*.py` are modified (i.e. except `pygmt/tests/**`), and also when `.github/workflows/benchmarks.yml` is modified. --------- Co-authored-by: Dongdong Tian <[email protected]>
1 parent 06ae818 commit 013014b

File tree

5 files changed

+86
-0
lines changed

5 files changed

+86
-0
lines changed

.github/ISSUE_TEMPLATE/bump_gmt_checklist.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ assignees: ''
1919
- [ ] Bump the GMT version in CI (1 PR)
2020
- [ ] Update `environment.yml`
2121
- [ ] Update `ci/requirements/docs.yml`
22+
- [ ] Update `.github/workflows/benchmarks.yml`
2223
- [ ] Update `.github/workflows/cache_data.yaml`
2324
- [ ] Update `.github/workflows/ci_doctests.yaml`
2425
- [ ] Update `.github/workflows/ci_docs.yml`

.github/workflows/benchmarks.yml

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# Run performance benchmarks
2+
#
3+
# Continuous benchmarking using pytest-codspeed. Measures the execution speed
4+
# of tests marked with @pytest.mark.benchmark decorator.
5+
6+
name: Benchmarks
7+
8+
on:
9+
# Run on pushes to the main branch
10+
push:
11+
branches: [ main ]
12+
paths:
13+
- 'pygmt/**/*.py'
14+
- '!pygmt/tests/**'
15+
- '.github/workflows/benchmarks.yml'
16+
pull_request:
17+
paths:
18+
- 'pygmt/**/*.py'
19+
- '!pygmt/tests/**'
20+
- '.github/workflows/benchmarks.yml'
21+
# `workflow_dispatch` allows CodSpeed to trigger backtest
22+
# performance analysis in order to generate initial data.
23+
workflow_dispatch:
24+
release:
25+
types:
26+
- published
27+
28+
concurrency:
29+
group: ${{ github.workflow }}-${{ github.ref }}
30+
cancel-in-progress: ${{ github.ref != 'refs/heads/main' }}
31+
32+
jobs:
33+
benchmarks:
34+
runs-on: ubuntu-22.04
35+
defaults:
36+
run:
37+
shell: bash -l {0}
38+
39+
steps:
40+
# Checkout current git repository
41+
- name: Checkout
42+
uses: actions/[email protected]
43+
with:
44+
# fetch all history so that setuptools-scm works
45+
fetch-depth: 0
46+
47+
# Install Miniconda with conda-forge dependencies
48+
- name: Setup Miniconda
49+
uses: conda-incubator/[email protected]
50+
with:
51+
auto-activate-base: true
52+
activate-environment: "" # base environment
53+
channels: conda-forge,nodefaults
54+
channel-priority: strict
55+
56+
# Install GMT and dependencies from conda-forge
57+
- name: Install dependencies
58+
run: |
59+
# $CONDA is an environment variable pointing to the root of the miniconda directory
60+
# Preprend $CONDA/bin to $PATH so that conda's python is used over system python
61+
echo $CONDA/bin >> $GITHUB_PATH
62+
conda install --solver=libmamba gmt=6.4.0 python=3.12 \
63+
numpy pandas xarray netCDF4 packaging \
64+
pytest pytest-benchmark pytest-mpl
65+
python -m pip install -U pytest-codspeed setuptools
66+
67+
# Install the package that we want to test
68+
- name: Install the package
69+
run: make install
70+
71+
# Run the benchmark tests
72+
- name: Run benchmarks
73+
uses: CodSpeedHQ/[email protected]
74+
with:
75+
run: |
76+
python -c "import pygmt; pygmt.show_versions()"
77+
PYGMT_USE_EXTERNAL_DISPLAY="false" python -m pytest -r P --pyargs pygmt --codspeed
78+
env:
79+
GMT_LIBRARY_PATH: /usr/share/miniconda/lib/

README.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ PyGMT
2222
.. image:: https://codecov.io/gh/GenericMappingTools/pygmt/branch/main/graph/badge.svg?token=78Fu4EWstx
2323
:alt: Test coverage status
2424
:target: https://app.codecov.io/gh/GenericMappingTools/pygmt
25+
.. image:: https://img.shields.io/endpoint?url=https://codspeed.io/badge.json
26+
:alt: CodSpeed Performance Benchmarks
27+
:target: https://codspeed.io/GenericMappingTools/pygmt
2528
.. image:: https://img.shields.io/pypi/pyversions/pygmt.svg?style=flat-square
2629
:alt: Compatible Python versions.
2730
:target: https://pypi.python.org/pypi/pygmt

doc/maintenance.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ workflow files for more details.
104104
12. `format-command.yml`: Format the codes using slash command
105105
13. `dvc-diff.yml`: Report changes in test images
106106
14. `slash-command-dispatch.yml`: Support slash commands in pull requests
107+
15. `benchmarks.yml`: Benchmarks the execution speed of tests to track performance of PyGMT functions
108+
107109

108110
## Continuous Documentation
109111

pygmt/tests/test_basemap.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from pygmt import Figure
66

77

8+
@pytest.mark.benchmark
89
@pytest.mark.mpl_image_compare
910
def test_basemap():
1011
"""

0 commit comments

Comments
 (0)