Skip to content

Commit c2191a3

Browse files
committed
add benchmarks
1 parent c85d324 commit c2191a3

3 files changed

Lines changed: 47 additions & 0 deletions

File tree

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,13 @@
3838
dev = [
3939
"mypy>=1.16.1",
4040
"pytest>=8",
41+
"pytest-benchmark>=5",
4142
"pytest-cov>=6",
4243
"ruff>=0.12.0",
4344
]
4445
test = [
4546
"pytest>=8",
47+
"pytest-benchmark>=5",
4648
"pytest-cov",
4749
]
4850

tests/test_bench_roundtrip.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
"""Round-trip decompile/recompile benchmarks.
2+
3+
Run with: pytest tests/test_bench_roundtrip.py --benchmark-compare
4+
"""
5+
6+
import types
7+
8+
import pytest
9+
10+
from bytecode import Bytecode
11+
12+
13+
def _collect_code_objects(root: types.CodeType, depth: int = 1) -> list[types.CodeType]:
14+
result = [root]
15+
if depth > 0:
16+
for const in root.co_consts:
17+
if isinstance(const, types.CodeType):
18+
result.extend(_collect_code_objects(const, depth - 1))
19+
return result
20+
21+
22+
def _dis_corpus() -> list[types.CodeType]:
23+
import importlib.util
24+
25+
spec = importlib.util.find_spec("dis")
26+
assert spec and spec.origin
27+
src = open(spec.origin).read()
28+
top = compile(src, spec.origin, "exec")
29+
return _collect_code_objects(top)
30+
31+
32+
_CORPUS = _dis_corpus()
33+
34+
35+
@pytest.fixture(params=_CORPUS, ids=[c.co_name for c in _CORPUS])
36+
def code_object(request):
37+
return request.param
38+
39+
40+
def test_roundtrip(benchmark, code_object):
41+
def roundtrip():
42+
Bytecode.from_code(code_object).to_code()
43+
44+
benchmark(roundtrip)

tox.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ isolated_build = true
66
passenv = BYTECODE_PURE_PYTHON
77
deps=
88
pytest
9+
pytest-benchmark
910
pytest-cov
1011
pytest-subtests
1112
commands = pytest --cov bytecode --cov-report=xml -v tests

0 commit comments

Comments
 (0)