From af85448f9a7df8e7107f25cf6b5d512e8c9a0cbc Mon Sep 17 00:00:00 2001 From: sumanjeet0012 Date: Mon, 13 Jul 2026 23:30:35 +0530 Subject: [PATCH] Add benchmark tests --- newsfragments/71.feature.rst | 1 + pyproject.toml | 1 + tests/test_benchmarks.py | 42 ++++++++++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+) create mode 100644 newsfragments/71.feature.rst create mode 100644 tests/test_benchmarks.py diff --git a/newsfragments/71.feature.rst b/newsfragments/71.feature.rst new file mode 100644 index 0000000..eb52cdd --- /dev/null +++ b/newsfragments/71.feature.rst @@ -0,0 +1 @@ +Add `pytest-benchmark` based performance benchmarking tests to evaluate `Cid.prefix`, `Cid.encode`, and `Prefix.sum` execution speeds. diff --git a/pyproject.toml b/pyproject.toml index 121c5a1..c503b93 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -64,6 +64,7 @@ dev = [ "pre-commit>=3.0.0", "towncrier>=24,<25", "pyrefly>=0.17.1,<0.18.0", + "pytest-benchmark>=4.0.0", ] [tool.setuptools] diff --git a/tests/test_benchmarks.py b/tests/test_benchmarks.py new file mode 100644 index 0000000..d8b9731 --- /dev/null +++ b/tests/test_benchmarks.py @@ -0,0 +1,42 @@ +import hashlib + +import pytest +import multihash + +from cid import CIDv1, Prefix, make_cid + + +@pytest.fixture +def bench_cid(): + data = b"benchmark data" * 100 + digest = hashlib.sha256(data).digest() + mh_bytes = multihash.encode(digest, "sha2-256") + return CIDv1("raw", mh_bytes) + + +@pytest.mark.benchmark +def test_bench_cid_buffer(benchmark, bench_cid): + benchmark(lambda: bench_cid.buffer) + + +@pytest.mark.benchmark +def test_bench_cid_encode(benchmark, bench_cid): + benchmark(bench_cid.encode) + + +@pytest.mark.benchmark +def test_bench_cid_prefix(benchmark, bench_cid): + benchmark(bench_cid.prefix) + + +@pytest.mark.benchmark +def test_bench_prefix_sum(benchmark): + prefix = Prefix.v1(codec="raw", mh_type="sha2-256") + data = b"benchmark data" * 100 + benchmark(prefix.sum, data) + + +@pytest.mark.benchmark +def test_bench_make_cid(benchmark, bench_cid): + cid_str = str(bench_cid) + benchmark(make_cid, cid_str)