Skip to content

Commit

Permalink
Add CAR and CID benchmarks (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
MarshalX authored Mar 2, 2024
1 parent 71fa16b commit 6af9137
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 0 deletions.
Binary file added data/torture_cids.dag-cbor
Binary file not shown.
13 changes: 13 additions & 0 deletions pytests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import os
import urllib.request
from typing import Any, List, Tuple


Expand Down Expand Up @@ -32,3 +33,15 @@ def load_cbor_data_fixtures(dir_path: str) -> List[Tuple[str, Any]]:
fixtures.append((os.path.basename(root), fixture))

return fixtures


def load_car_fixture(did: str, path: str) -> bytes:
if os.path.exists(path):
with open(path, 'rb') as f:
return f.read()

contents = urllib.request.urlopen(f'https://bsky.network/xrpc/com.atproto.sync.getRepo?did={did}').read()
with open(path, 'wb') as f:
f.write(contents)

return contents
6 changes: 6 additions & 0 deletions pytests/test_dag_cbor.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
_ROUNDTRIP_DATA_DIR = os.path.join(os.path.dirname(__file__), '..', 'data', 'roundtrip')
_REAL_DATA_DIR = os.path.join(os.path.dirname(__file__), '..', 'data')
_FIXTURES_DATA_DIR = os.path.join(os.path.dirname(__file__), '..', 'data', 'fixtures')
_CIDS_DAG_CBOR_PATH = os.path.join(os.path.dirname(__file__), '..', 'data', 'torture_cids.dag-cbor')


def _dag_cbor_encode(benchmark, data) -> None:
Expand Down Expand Up @@ -103,3 +104,8 @@ def test_dag_cbor_decode_real_data(benchmark, data) -> None:
@pytest.mark.parametrize('data', load_cbor_data_fixtures(_FIXTURES_DATA_DIR), ids=lambda data: data[0])
def test_dag_cbor_decode_fixtures(benchmark, data) -> None:
_dag_cbor_decode(benchmark, data)


def test_dag_cbor_decode_torture_cids(benchmark) -> None:
dag_cbor = open(_CIDS_DAG_CBOR_PATH, 'rb').read()
benchmark(libipld.decode_dag_cbor, dag_cbor)
29 changes: 29 additions & 0 deletions pytests/test_decode_car.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import os

import libipld
import pytest

from conftest import load_car_fixture


_DID = os.environ.get('CAR_REPO_DID', 'did:plc:w4es6sfh43zlht3bgrzi5qzq') # default is public bot in bsky.app
_REPO_CAR_PATH = os.path.join(os.path.dirname(__file__), '..', 'data', 'repo.car')


@pytest.fixture(scope='session')
def car() -> bytes:
return load_car_fixture(_DID, _REPO_CAR_PATH)


def test_decode_car(benchmark, car) -> None:
header, blocks = benchmark(libipld.decode_car, car)

assert 1 == header['version']
assert isinstance(header['roots'], list)
assert 1 == len(header['roots'])

assert isinstance(blocks, dict)
assert all(isinstance(k, str) for k in blocks.keys())
assert all(len(k) == 59 for k in blocks.keys())
assert all(isinstance(v, dict) for v in blocks.values())
assert all(v for v in blocks.values()) # not empty dict

0 comments on commit 6af9137

Please sign in to comment.