Skip to content

Commit d91a522

Browse files
committed
port benchmarks to pytest-benchmark
1 parent 5686bb3 commit d91a522

12 files changed

Lines changed: 162 additions & 282 deletions

asv.conf.json

Lines changed: 0 additions & 21 deletions
This file was deleted.

benchmarks/_utils.py

Lines changed: 0 additions & 52 deletions
This file was deleted.

benchmarks/asdf.py

Lines changed: 0 additions & 120 deletions
This file was deleted.

benchmarks/conftest.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import io
2+
import itertools
3+
import string
4+
5+
import numpy as np
6+
import pytest
7+
8+
import asdf
9+
10+
11+
SMALL_SET = string.ascii_lowercase[:3]
12+
LARGE_SET = string.ascii_lowercase[:26]
13+
14+
DATA_GENS = ["no_data", "small_data", "large_data"]
15+
TREE_GENS = ["small_tree", "flat_tree", "deep_tree", "large_tree"]
16+
17+
18+
def no_data(i):
19+
return i
20+
21+
22+
def small_data(i):
23+
return np.full((3, 3), i)
24+
25+
26+
def large_data(i):
27+
return np.full((128, 128), i)
28+
29+
30+
def small_tree(data_gen):
31+
return {k: data_gen(ord(k)) for k in SMALL_SET}
32+
33+
34+
def flat_tree(data_gen):
35+
return {k: data_gen(ord(k)) for k in LARGE_SET}
36+
37+
38+
def deep_tree(data_gen):
39+
tree = {}
40+
for k in LARGE_SET:
41+
tree[k] = {"value": data_gen(k)}
42+
tree = tree[k]
43+
return tree
44+
45+
46+
def large_tree(data_gen):
47+
tree = {}
48+
for k in LARGE_SET:
49+
tree["value"] = {k: data_gen(ord(k)) for k in LARGE_SET}
50+
tree[k] = {}
51+
tree = tree[k]
52+
return tree
53+
54+
55+
@pytest.fixture(params=itertools.product(DATA_GENS, TREE_GENS))
56+
def tree(request):
57+
data_gen_name, tree_gen_name = request.param
58+
# some gymnastics to work around pytest not allowing
59+
# getfixturevalue to use parametrized fixtures
60+
return globals()[tree_gen_name](globals()[data_gen_name])
61+
62+
63+
@pytest.fixture
64+
def tree_bytes(tree):
65+
return asdf.dumps(tree)
66+
67+
68+
@pytest.fixture
69+
def asdf_file(tree):
70+
return asdf.AsdfFile(tree)

benchmarks/schema.py

Lines changed: 0 additions & 35 deletions
This file was deleted.

benchmarks/test_asdf.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import io
2+
3+
import asdf
4+
5+
6+
def test_init(tree, benchmark):
7+
benchmark(asdf.AsdfFile, tree)
8+
9+
10+
def test_validate(asdf_file, benchmark):
11+
benchmark(asdf_file.validate)
12+
13+
14+
def test_write_to(asdf_file, benchmark):
15+
bs = io.BytesIO()
16+
benchmark(asdf_file.write_to, bs)
17+
18+
19+
def test_open(tree_bytes, benchmark):
20+
bs = io.BytesIO(tree_bytes)
21+
benchmark(asdf.open, bs)
22+
23+
24+
def test_update(tree_bytes, benchmark):
25+
bs = io.BytesIO(tree_bytes)
26+
af = asdf.open(bs)
27+
benchmark(af.update)
28+
29+
30+
def test_dump(tree, benchmark):
31+
benchmark(asdf.dumps, tree)
32+
33+
34+
def test_load(tree_bytes, benchmark):
35+
benchmark(asdf.loads, tree_bytes)

benchmarks/test_schema.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import pytest
2+
import numpy as np
3+
4+
import asdf
5+
6+
7+
@pytest.fixture
8+
def software_asdf_file():
9+
return asdf.AsdfFile({"obj": asdf.tags.core.Software(name="foo", version="0.0.0")})
10+
11+
12+
@pytest.fixture
13+
def ndarray_asdf_file():
14+
return asdf.AsdfFile({"obj": np.ndarray([1])})
15+
16+
17+
@pytest.fixture(params=["software_asdf_file", "ndarray_asdf_file"])
18+
def asdf_file(request):
19+
return request.getfixturevalue(request.param)
20+
21+
22+
def test_validate(asdf_file, benchmark):
23+
# first validate outside the benchmark to incur
24+
# extension loading, schema caching and other one-time costs
25+
asdf_file.validate()
26+
benchmark(asdf_file.validate)

benchmarks/test_treeutil.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import asdf
2+
3+
4+
def test_walk(tree, benchmark):
5+
benchmark(asdf.treeutil.walk, tree, lambda x: None)

benchmarks/test_yamlutil.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import pytest
2+
3+
import asdf
4+
5+
6+
@pytest.fixture(scope="module")
7+
def ctx_asdf_file():
8+
return asdf.AsdfFile()
9+
10+
11+
@pytest.fixture()
12+
def tagged_tree(tree, ctx_asdf_file):
13+
return asdf.yamlutil.custom_tree_to_tagged_tree(tree, ctx_asdf_file)
14+
15+
16+
def test_custom_tree_to_tagged_tree(tree, ctx_asdf_file, benchmark):
17+
benchmark(asdf.yamlutil.custom_tree_to_tagged_tree, tree, ctx_asdf_file)
18+
19+
20+
def test_tagged_tree_to_tagged_tree(tagged_tree, ctx_asdf_file, benchmark):
21+
benchmark(asdf.yamlutil.tagged_tree_to_custom_tree, tagged_tree, ctx_asdf_file)

0 commit comments

Comments
 (0)