Skip to content

Commit 6477ad3

Browse files
committed
add benchmarks for catch all
1 parent 1b5f8fe commit 6477ad3

File tree

1 file changed

+122
-0
lines changed

1 file changed

+122
-0
lines changed

benchmarks/catch_all.py

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
import logging
2+
from dataclasses import dataclass
3+
from timeit import timeit
4+
from typing import Any
5+
6+
import pytest
7+
8+
from dataclasses_json import (dataclass_json,
9+
Undefined,
10+
CatchAll as CatchAllDJ)
11+
12+
from dataclass_wizard import (JSONWizard,
13+
CatchAll as CatchAllWizard)
14+
15+
16+
log = logging.getLogger(__name__)
17+
18+
19+
@dataclass()
20+
class DontCareAPIDump:
21+
endpoint: str
22+
data: dict[str, Any]
23+
24+
25+
@dataclass_json(undefined=Undefined.INCLUDE)
26+
@dataclass()
27+
class DontCareAPIDumpDJ(DontCareAPIDump):
28+
unknown_things: CatchAllDJ
29+
30+
31+
@dataclass()
32+
class DontCareAPIDumpWizard(DontCareAPIDump, JSONWizard):
33+
34+
class _(JSONWizard.Meta):
35+
v1 = True
36+
37+
unknown_things: CatchAllWizard
38+
39+
40+
@pytest.fixture(scope='session')
41+
def data():
42+
return {"endpoint": "some_api_endpoint",
43+
"data": {"foo": 1, "bar": "2"},
44+
"undefined_field_name": [1, 2, 3]}
45+
46+
47+
@pytest.fixture(scope='session')
48+
def data_no_extras():
49+
return {"endpoint": "some_api_endpoint",
50+
"data": {"foo": 1, "bar": "2"}}
51+
52+
53+
def test_load(data, n):
54+
"""
55+
[ RESULTS ON MAC OS X ]
56+
57+
benchmarks.catch_all.catch_all - [INFO] dataclass-wizard 0.060889
58+
benchmarks.catch_all.catch_all - [INFO] dataclasses-json 11.469157
59+
60+
"""
61+
g = globals().copy()
62+
g.update(locals())
63+
64+
log.info('dataclass-wizard %f',
65+
timeit('DontCareAPIDumpWizard.from_dict(data)', globals=g, number=n))
66+
log.info('dataclasses-json %f',
67+
timeit('DontCareAPIDumpDJ.from_dict(data)', globals=g, number=n))
68+
69+
dump1 = DontCareAPIDumpDJ.from_dict(data) # DontCareAPIDump(endpoint='some_api_endpoint', data={'foo': 1, 'bar': '2'})
70+
dump2 = DontCareAPIDumpWizard.from_dict(data) # DontCareAPIDump(endpoint='some_api_endpoint', data={'foo': 1, 'bar': '2'})
71+
72+
assert dump1.endpoint == dump2.endpoint
73+
assert dump1.data == dump2.data
74+
assert dump1.unknown_things == dump2.unknown_things
75+
76+
77+
def test_load_with_no_extra_data(data_no_extras, n):
78+
"""
79+
[ RESULTS ON MAC OS X ]
80+
81+
benchmarks.catch_all.catch_all - [INFO] dataclass-wizard 0.045790
82+
benchmarks.catch_all.catch_all - [INFO] dataclasses-json 11.031206
83+
84+
"""
85+
g = globals().copy()
86+
g.update(locals())
87+
88+
log.info('dataclass-wizard %f',
89+
timeit('DontCareAPIDumpWizard.from_dict(data_no_extras)', globals=g, number=n))
90+
log.info('dataclasses-json %f',
91+
timeit('DontCareAPIDumpDJ.from_dict(data_no_extras)', globals=g, number=n))
92+
93+
dump1 = DontCareAPIDumpDJ.from_dict(data_no_extras) # DontCareAPIDump(endpoint='some_api_endpoint', data={'foo': 1, 'bar': '2'})
94+
dump2 = DontCareAPIDumpWizard.from_dict(data_no_extras) # DontCareAPIDump(endpoint='some_api_endpoint', data={'foo': 1, 'bar': '2'})
95+
96+
assert dump1.endpoint == dump2.endpoint
97+
assert dump1.data == dump2.data
98+
assert dump1.unknown_things == dump2.unknown_things == {}
99+
100+
101+
def test_dump(data):
102+
"""
103+
[ RESULTS ON MAC OS X ]
104+
105+
benchmarks.catch_all.catch_all - [INFO] dataclass-wizard 0.317555
106+
benchmarks.catch_all.catch_all - [INFO] dataclasses-json 3.970232
107+
108+
"""
109+
dump1 = DontCareAPIDumpWizard.from_dict(data)
110+
dump2 = DontCareAPIDumpDJ.from_dict(data)
111+
112+
g = globals().copy()
113+
g.update(locals())
114+
115+
log.info('dataclass-wizard %f',
116+
timeit('dump1.to_dict()', globals=g, number=n))
117+
log.info('dataclasses-json %f',
118+
timeit('dump2.to_dict()', globals=g, number=n))
119+
120+
expected = {'endpoint': 'some_api_endpoint', 'data': {'foo': 1, 'bar': '2'}, 'undefined_field_name': [1, 2, 3]}
121+
122+
assert dump1.to_dict() == dump2.to_dict() == expected

0 commit comments

Comments
 (0)