Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions newsfragments/71.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add `pytest-benchmark` based performance benchmarking tests to evaluate `Cid.prefix`, `Cid.encode`, and `Prefix.sum` execution speeds.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
42 changes: 42 additions & 0 deletions tests/test_benchmarks.py
Original file line number Diff line number Diff line change
@@ -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)
Loading