Skip to content
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

Setup Continuous Benchmarking workflow with pytest-codspeed #2908

Merged
merged 39 commits into from
Dec 25, 2023

Conversation

weiji14
Copy link
Member

@weiji14 weiji14 commented Dec 23, 2023

Description of proposed changes

Measuring the execution speed of tests to track performance of PyGMT functions over time.

Using pytest-codspeed to do the benchmarking, with the help of https://github.com/CodSpeedHQ/action. Decorated test_basemap with @pytest.mark.benchmark to see if the benchmarking works.

Note: Running the benchmarks on Python 3.12 to enable flame graph generation, available with pytest-codspeed>=2.0.0 - see https://docs.codspeed.io/features/trace-generation.

References:

Relates to #2730 (comment), addresses #2910.

Reminders

  • Run make format and make check to make sure the code follows the style guide.
  • Add tests for new features or tests that would have caught the bug that you're fixing.
  • Add new public functions/methods/classes to doc/api/index.rst.
  • Write detailed docstrings for all functions/methods.
  • If wrapping a new module, open a 'Wrap new GMT module' issue and submit reasonably-sized PRs.
  • If adding new functionality, add an example to docstrings or tutorials.
  • Use underscores (not hyphens) in names of Python files and directories.

Slash Commands

You can write slash commands (/command) in the first line of a comment to perform
specific operations. Supported slash commands are:

  • /format: automatically format and lint the code
  • /test-gmt-dev: run full tests on the latest GMT development version

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 couple of unit tests with @pytest.mark.benchmark to see if the benchmarking works.
@weiji14 weiji14 added the maintenance Boring but important stuff for the core devs label Dec 23, 2023
@weiji14 weiji14 added this to the 0.11.0 milestone Dec 23, 2023
@weiji14 weiji14 self-assigned this Dec 23, 2023
To debug why import pygmt doesn't work.
Might need to launch into the correct shell first. Also running pip list to double check if dependencies are installed ok.
Seeing if it's possible to avoid using conda shell.
Do everything in the same shell, but point to the GMT installation in /home/runner/micromamba/envs/pygmt/lib/
Try to prevent `ERROR: Project file:///home/runner/work/pygmt/pygmt has a 'pyproject.toml' and its build backend is missing the 'build_editable' hook. Since it does not have a 'setup.py' nor a 'setup.cfg', it cannot be installed in editable mode. Consider using a build backend that supports PEP 660`.
Need newer setuptools greater than 64.
Action might not like the single quotes.
@weiji14 weiji14 force-pushed the continuous-benchmarking branch from a19bdf2 to 31a7567 Compare December 23, 2023 08:13
Should be /home/runner/micromamba/envs/pygmt/lib/, but don't know what's happening in the CodSpeedHQ/action shell.
Maybe best to use the bundled conda with GitHub Actions?
New place where GMT is installed by miniconda.
See if libgmt.so is in this folder.
Trying to workaround `Error loading GMT shared library at '/usr/share/miniconda/envs/pygmt/lib/libgmt.so'.
  /lib/x86_64-linux-gnu/libcrypto.so.3: version `OPENSSL_3.2.0' not found (required by /usr/share/miniconda/envs/pygmt/lib/././libssl.so.3)`
Missing conda-forge package for pytest-codspeed.
Instead of setting Python 3.12 explicitly.
Don't use `make test`, which seems to use the system python in `/usr/bin/python` rather than `/usr/share/miniconda/bin/python`.
Also split the PyGMT build step into a separate step, now that we're confident all the packages are installed using conda's python/pip.
Also tidy up some stray bits
Try to avoid collecting tests in the examples/ folder.
@seisman
Copy link
Member

seisman commented Dec 24, 2023

So which tests should we benchmark (and should we mark all those tests with @pytest.mark.benchmark in this PR or a follow-up one)?

I think we should focus on benchmarking low-level functions rather than modules' wrappers, since the low-level functions are heavily used in everywhere and most wrappers have very simple and similar code structures.

For example, most plotting modules have very simple code like the one in Figure.basemap

    kwargs = self._preprocess(**kwargs)                                         
    with Session() as lib:                                                      
        lib.call_module(module="basemap", args=build_arg_string(kwargs))

so we just need to benchmark one basemap test (e.g., test_basemap()) and don't need to benchmark other basemap's tests and other plotting methods (e.g., Figure.coast). Of course, there are few exceptions, for example, Figure.meca, Figure.plot, Figure.plot3d, Figure.text are among the complicated wrappers and should be benchmarked.

Similarly, for table-processing and grid-processing functions, benchmarking pygmt.select and pygmt.grdfill should be enough.

@weiji14
Copy link
Member Author

weiji14 commented Dec 24, 2023

Need to mention the new workflow in:

* `doc/maintenance.md`
* `.github/ISSUE_TEMPLATE/bump_gmt_checklist.md`

Ok, done at c92fcb2 and 7bf09b9

* `.github/ISSUE_TEMPLATE/release_checklist.md` (maybe unnecessary in this file)

Probably not needed in the checklist, but I added a trigger at c8d1965 so that the benchmarks will be run when a release is published.

So which tests should we benchmark (and should we mark all those tests with @pytest.mark.benchmark in this PR or a follow-up one)?

I think we should focus on benchmarking low-level functions rather than modules' wrappers, since the low-level functions are heavily used in everywhere and most wrappers have very simple and similar code structures.

Let me open up a separate issue to discuss this, and also to track which unit tests we should benchmark. Edit: see #2910.

Remove markers for test_blockmean_input_dataframe and test_grd2xyz.
The default Python used now should be the conda one instead of the system one.
Default `make test` requires the use of pytest-cov and pytest-doctestplus
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.
Copy link
Member

@seisman seisman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me.

@weiji14 weiji14 marked this pull request as ready for review December 25, 2023 06:06
@weiji14
Copy link
Member Author

weiji14 commented Dec 25, 2023

Thanks for reviewing @seisman! I'll merge this now and open a follow-up PR for marking more unit tests with @pytest.mark.benchmark. We can discuss more in #2910 too.

@weiji14 weiji14 merged commit 013014b into main Dec 25, 2023
22 of 25 checks passed
@weiji14 weiji14 deleted the continuous-benchmarking branch December 25, 2023 06:33
@seisman
Copy link
Member

seisman commented Dec 25, 2023

The Tests workflow (https://github.com/GenericMappingTools/pygmt/actions/runs/7319763038/job/19937998567) now has the following warnings because pytest-benchmark is not installed in these workflows:

  /home/runner/work/pygmt/pygmt/pygmt/tests/test_basemap.py:8: PytestUnknownMarkWarning: Unknown pytest.mark.benchmark - is this a typo?  You can register custom marks to avoid this warning - for details, see https://docs.pytest.org/en/stable/how-to/mark.html
    @pytest.mark.benchmark

I believe we need to silence these warnings.

@weiji14
Copy link
Member Author

weiji14 commented Dec 25, 2023

The Tests workflow (https://github.com/GenericMappingTools/pygmt/actions/runs/7319763038/job/19937998567) now has the following warnings because pytest-benchmark is not installed in these workflows:

Ok, opened #2912 for this.

@weiji14
Copy link
Member Author

weiji14 commented Dec 25, 2023

By the way, can you change the CodSpeed settings to only make PR comments when there is a performance improvement or regression (still don't have permissions)? This should reduce noise like #2907 (comment) when there's no noticeable change (we can still see the report in the 'Checks' tab if needed).

image

Also, maybe reduce the regression threshold to 5%?

@seisman
Copy link
Member

seisman commented Dec 25, 2023

By the way, can you change the CodSpeed settings to only make PR comments when there is a performance improvement or regression (still don't have permissions)? This should reduce noise like #2907 (comment) when there's no noticeable change (we can still see the report in the 'Checks' tab if needed).

Done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
maintenance Boring but important stuff for the core devs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants