Performance testing is handled via pytest-benchmark.
Benchmarking:
Wrap a function call using the benchmark fixture. You can pass the function directly, pass arguments, or use a lambda for complex calls.
def test_benchmark(benchmark):
def sleep_s(s):
import time
time.sleep(s)
return True
assert benchmark(lambda: sleep_s(1))
Profiling:
To evaluate the call stack in detail, run tests with the --benchmark-cprofile flag. You can specify the sorting column (e.g., --benchmark-cprofile="tottime").
Notes:
- Benchmarking is automatically disabled when running tests in parallel (
pytest-xdist). - Use
--benchmark-disable to turn it off manually. - Use
--benchmark-autosave to save results to a .benchmarks folder in the current working directory.