/
Depends on
- dh-python (dh-sequence-python3, pybuild-plugin-pyproject)
- freezegun (python3-freezegun)
- git (git)
- mercurial (mercurial)
- pytest (python3-pytest)
- pytest-xdist (python3-pytest-xdist)
- python3-defaults (python3-all)
- setuptools (python3-setuptools)
- debhelper-compat, python3-cpuinfo, python3-elasticsearch, python3-pygal
Used by
- bidict 0.23.1-2
- borgbackup 1.4.4-3
- borgbackup2 2.0.0b21-1
- construct 2.10.68+dfsg1-3
- cyarray 1.2-1
- dacite 1.9.2-4
- deepdiff 9.0.0-1
- geoalchemy2 0.18.4-1
- graphql-core 3.2.11-1
- guessit 3.8.0-4
- isort 8.0.1-2
- labgrid 26.0-1
- lazy-object-proxy 1.12.0-1
- libcloud 3.8.0+repack-6
- markdown-it-py 4.2.0-3
- mkdocs-glightbox 0.5.2-1
- numpy-rms 0.7.0-3
- python-cryptography 46.0.7-1
- pylint 4.0.6-1
- pytest-codspeed 5.0.3-1
Show 18 more
- python-atom 0.12.1-2
- python-cattrs 26.1.0-1
- python-ical 12.1.0-1
- python-jaeger-client 4.8.0-5
- python-multipledispatch 1.0.0-2
- python-mutf8 1.0.6-4
- python-pixelmatch 0.3.0+ds-2
- python-pint 0.25.3-2
- python-promise 2.3.0-4
- python-pyppmd 1.3.1+ds1-2
- python-syrupy 5.5.3-1
- python-tblib 3.2.2-1
- borgbackup 1.4.5-1
- borgbackup2 2.0.0b23-4
- eodag 4.7.2+ds-1
- borgbackup2 2.0.0b23-5
- isort 9.0.1-1
- labgrid 26.0-2
README (full)
Overview
:target: https://readthedocs.org/projects/pytest-benchmark/ :alt: Documentation Status
:alt: GitHub Actions Build Status :target: https://github.com/ionelmc/pytest-benchmark/actions
:alt: Join the chat at https://gitter.im/ionelmc/pytest-benchmark :target: https://gitter.im/ionelmc/pytest-benchmark
:alt: Coverage Status :target: https://coveralls.io/github/ionelmc/pytest-benchmark?branch=master
:alt: Coverage Status :target: https://app.codecov.io/github/ionelmc/pytest-benchmark
:alt: PyPI Package latest release :target: https://pypi.org/project/pytest-benchmark
:alt: PyPI Wheel :target: https://pypi.org/project/pytest-benchmark
:alt: Supported versions :target: https://pypi.org/project/pytest-benchmark
:alt: Supported implementations :target: https://pypi.org/project/pytest-benchmark
:alt: Commits since latest release :target: https://github.com/ionelmc/pytest-benchmark/compare/v5.2.3...master
A pytest fixture for benchmarking code. It will group the tests into rounds that are calibrated to the chosen timer.
See calibration and FAQ.
Free software: BSD 2-Clause License
Installation
pip install pytest-benchmark
Documentation
For latest release: pytest-benchmark.readthedocs.org/en/stable.
For master branch (may include documentation fixes): pytest-benchmark.readthedocs.io/en/latest.
Examples
But first, a prologue:
This plugin tightly integrates into pytest. To use this effectively you should know a thing or two about pytest first. Take a look at the introductory material or watch talks.
Few notes:
This plugin benchmarks functions and only that. If you want to measure block of code
or whole programs you will need to write a wrapper function.
In a test you can only benchmark one function. If you want to benchmark many functions write more tests or
use parametrization.
To run the benchmarks you simply use `pytest` to run your "tests". The plugin will automatically do the
benchmarking and generate a result table. Run
pytest --helpfor more details.
This plugin provides a `benchmark` fixture. This fixture is a callable object that will benchmark any function passed to it.
Example:
def something(duration=0.000001):
"""
Function that needs some serious benchmarking.
"""
time.sleep(duration)
# You may return anything you want, like the result of a computation
return 123
def test_my_stuff(benchmark):
# benchmark something
result = benchmark(something)
# Extra code, to verify that the run completed correctly.
# Sometimes you may want to check the result, fast functions
# are no good if they return incorrect results :-)
assert result == 123
You can also pass extra arguments:
def test_my_stuff(benchmark):
benchmark(time.sleep, 0.02)
Or even keyword arguments:
def test_my_stuff(benchmark):
benchmark(time.sleep, duration=0.02)
Another pattern seen in the wild, that is not recommended for micro-benchmarks (very fast code) but may be convenient:
def test_my_stuff(benchmark):
@benchmark
def something(): # unnecessary function call
time.sleep(0.000001)
A better way is to just benchmark the final function:
def test_my_stuff(benchmark):
benchmark(time.sleep, 0.000001) # way more accurate results!
If you need to do fine control over how the benchmark is run (like a `setup` function, exact control of `iterations` and `rounds`) there's a special mode - pedantic:
def my_special_setup():
...
def test_with_setup(benchmark):
benchmark.pedantic(something, setup=my_special_setup, args=(1, 2, 3), kwargs={'foo': 'bar'}, iterations=10, rounds=100)
Screenshots
Normal run:
:alt: Screenshot of pytest summary
Compare mode (--benchmark-compare):
:alt: Screenshot of pytest summary in compare mode
Histogram (--benchmark-histogram):
:alt: Histogram sample
Development
To run the all tests run::
tox
Credits
Timing code and ideas taken from: https://github.com/vstinner/misc/blob/34d3128468e450dad15b6581af96a790f8bd58ce/python/benchmark.py
| dir | .github/ |
| dir | ci/ |
| dir | debian/ |
| dir | docs/ |
| dir | src/ |
| dir | tests/ |
| file | .bumpversion.cfg |
| file | .cookiecutterrc |
| file | .coveragerc |
| file | .editorconfig |
| file | .gitignore |
| file | .pre-commit-config.yaml |
| file | .readthedocs.yml |
| file | .taplo.toml |
| file | AUTHORS.rst |
| file | CHANGELOG.rst |
| file | CONTRIBUTING.rst |
| file | LICENSE |
| file | MANIFEST.in |
| file | README.rst |
| file | pyproject.toml |
| file | pytest.ini |
| file | tox.ini |