This directory contains the automated performance and memory benchmarking suite for the factorise library.
The suite ensures that modifications to the core arithmetic routines (Miller-Rabin, Pollard-Brent) do not introduce performance regressions or memory accumulation.
The benchmark suite measures:
- Execution Time (Throughput/Latency): Wall-clock time required to factorise given inputs.
- Peak Memory Allocation: Maximum memory used by the algorithms during execution.
- Scalability: Performance trends as input sizes grow exponentially.
- Stress Correctness: Verification of algorithmic stability at scale across multiple cores.
Benchmarking requires development dependencies.
# Install the library with dev/benchmark extras
pip install -e ".[dev]"Key Dependencies:
pytest-benchmark: High-resolution execution timing.tracemalloc: Standard library memory tracking (used inmemory.py).rich: Console UI for concurrent stress testing.
The recommended way to run benchmarks is via the just task runner:
# Run execution timing benchmarks
just benchmark
# Run memory allocation benchmarks
just benchmark-memory
# Run massive concurrent stress tests
just stress-testFor deeper diagnostics, use pytest directly:
Measure execution speed with specific sorting or histograms:
# Sort results by mean execution time
pytest benchmarks/timing.py --benchmark-only --benchmark-sort=mean
# Generate a histogram of execution times
pytest benchmarks/timing.py --benchmark-only --benchmark-histogramMeasure peak allocation with verbose output:
# Run all memory benchmarks and print allocation metrics
pytest benchmarks/memory.py -v -sTo detect regressions against a baseline:
- Save baseline from master:
pytest benchmarks/timing.py --benchmark-only --benchmark-save=baseline - Run on feature branch:
pytest benchmarks/timing.py --benchmark-only --benchmark-compare
Inputs are controlled in inputs.py to ensure consistency:
- Small: Triggers fast-paths (small primes, trivial division).
-
Medium: Exercises core loops (numbers up to
$10^9$ ). -
Large: Stresses modular exponentiation and cycle detection (
$2^{64}$ semiprimes).
- Mean/Min/Max: Captured in microseconds (μs) or milliseconds (ms).
- OPS (Operations Per Second): Throughput measurement; higher is better.
- Peak Bytes: Maximum memory allocated during the benchmarked call.
- Warmup:
pytest-benchmarkperforms automatic warmup rounds; do not add manual sleeps. - Isolation: Run benchmarks on a quiet machine with thermal throttling disabled for stable results.
- Conventions: Keep the actual benchmarked logic inside the
benchmark()call provided by the fixture.
- Hardware Variance: Absolute times depend on the host CPU. Use relative comparisons on the same machine.
- Runtime Variance: Python 3.11+ provides significant speedups over 3.10. Ensure consistent Python versions during comparison.
For detailed contribution guidelines, see CONTRIBUTING.md.