Skip to content
Merged
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
8 changes: 8 additions & 0 deletions docs/contributing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,14 @@ We can run all tests with:

make test

The suite includes codec/Block encode-decode microbenchmarks powered by
``pytest-benchmark`` (marked ``benchmark``). They run with the default suite.
To run only those benches:

.. code:: sh

pytest -m benchmark


Code Style
~~~~~~~~~~
Expand Down
1 change: 1 addition & 0 deletions newsfragments/34.performance.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add pytest-benchmark encode/decode microbenchmarks for DAG-CBOR, DAG-JSON, DAG-PB, raw, and ``Block``.
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ docs = [
]
test = [
"pytest",
"pytest-benchmark",
"pytest-runner",
]
release = [
Expand All @@ -82,6 +83,7 @@ log_date_format = "%m-%d %H:%M:%S"
log_format = "%(levelname)8s %(asctime)s %(filename)20s %(message)s"
markers = [
"slow: mark test as slow",
"benchmark: mark test as a pytest-benchmark microbenchmark",
]

[tool.ruff]
Expand Down
67 changes: 67 additions & 0 deletions tests/test_benchmarks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
"""Codec and Block encode/decode microbenchmarks (pytest-benchmark)."""

from __future__ import annotations

import pytest

from dag import Block
from dag.codecs import dag_cbor, dag_json, dag_pb, raw

BENCH_MAP = {"key": "value", "number": 42, "list": [1, 2, 3]}
BENCH_PB = {"Data": b"bench", "Links": []}
BENCH_RAW = b"benchmark-raw-payload"

BENCH_CBOR_BYTES = dag_cbor.encode(BENCH_MAP)
BENCH_JSON_BYTES = dag_json.encode(BENCH_MAP)
BENCH_PB_BYTES = dag_pb.encode(BENCH_PB)
BENCH_RAW_BYTES = raw.encode(BENCH_RAW)


@pytest.mark.benchmark
def test_bench_dag_cbor_encode(benchmark):
benchmark(dag_cbor.encode, BENCH_MAP)


@pytest.mark.benchmark
def test_bench_dag_cbor_decode(benchmark):
benchmark(dag_cbor.decode, BENCH_CBOR_BYTES)


@pytest.mark.benchmark
def test_bench_dag_json_encode(benchmark):
benchmark(dag_json.encode, BENCH_MAP)


@pytest.mark.benchmark
def test_bench_dag_json_decode(benchmark):
benchmark(dag_json.decode, BENCH_JSON_BYTES)


@pytest.mark.benchmark
def test_bench_dag_pb_encode(benchmark):
benchmark(dag_pb.encode, BENCH_PB)


@pytest.mark.benchmark
def test_bench_dag_pb_decode(benchmark):
benchmark(dag_pb.decode, BENCH_PB_BYTES)


@pytest.mark.benchmark
def test_bench_raw_encode(benchmark):
benchmark(raw.encode, BENCH_RAW)


@pytest.mark.benchmark
def test_bench_raw_decode(benchmark):
benchmark(raw.decode, BENCH_RAW_BYTES)


@pytest.mark.benchmark
def test_bench_block_encode(benchmark):
benchmark(lambda: Block.encode(value=BENCH_MAP, codec=dag_cbor.codec))


@pytest.mark.benchmark
def test_bench_block_decode(benchmark):
benchmark(lambda: Block.decode(data=BENCH_CBOR_BYTES, codec=dag_cbor.codec))
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ commands=
deps =
build
pytest
pytest-benchmark
allowlist_externals =
uv
pytest
Expand Down
Loading