Debian Codegraph @

jsonpickle_benchmarks.py

raw
1 """
2 Run "make benchmark" in the directory that this file is in.
3 This will test the version of jsonpickle inside the local directory,
4 NOT the pip installed version.
5 To test the pip installed version, run this outside the jsonpickle directory:
6 py.test --benchmark-only --benchmark-histogram=./jsonpickle/images/benchmark-$(date +%Y-%m-%dT%T%z) ./jsonpickle/jsonpickle_benchmarks.py
7 MAKE SURE you have pytest-benchmark pip installed.
8 """
9
10 import itertools
11
12 import jsonpickle
13
14 # DEFINTIIONS:
15 # HOMOGENOUS - Except for sets, each container may only have integers, floats, strings,
16 # and itself inside it
17 # HETEROGENOUS - Except for sets, each container must have at least one of every other
18 # container type being tested
19 # SIMPLE - Each container must have two each of integers, floats, and strings
20 # COMPLEX - Each container must have four each of integers, floats, and strings
21
22 # SETUP
23 ###################################################################################
24
25
26 class SlotPickleMixin(object):
27 def __getstate__(self):
28 all_slots = itertools.chain.from_iterable(
29 getattr(cls, "__slots__", []) for cls in self.__class__.__mro__
30 )
31 return dict(
32 (slot, getattr(self, slot)) for slot in all_slots if hasattr(self, slot)
33 )
34
35 def __setstate__(self, state):
36 for slot, value in dict(state).items():
37 setattr(self, slot, value)
38
39
40 class MyClass(object):
41 __slots__ = ["idk", "idk2"]
42
43 def __init__(self):
44 self.idk = {
45 "a": 0,
46 "b": [0.5783, 1, 2.5789],
47 "c": {0, 1.87559, 2},
48 "d": ["0", "1", "2"],
49 "e": {"0", "1", "2"},
50 "f": {"a": 0, "b": {"a": 1}},
51 }
52 self.idk2 = [
53 "a",
54 "b",
55 "c",
56 0,
57 0.18752,
58 2,
59 ["a", "b", "c", 0.8579, 1, 2.59757],
60 (self.idk, self.idk),
61 ]
62
63
64 class MyClassGetState(SlotPickleMixin):
65 __slots__ = ["idk", "idk2"]
66
67 def __init__(self):
68 self.idk = {
69 "a": 0,
70 "b": [0.5783, 1, 2.5789],
71 "c": {0, 1.87559, 2},
72 "d": ["0", "1", "2"],
73 "e": {"0", "1", "2"},
74 "f": {"a": 0, "b": {"a": 1}},
75 }
76 self.idk2 = [
77 "a",
78 "b",
79 "c",
80 0,
81 0.18752,
82 2,
83 ["a", "b", "c", 0.8579, 1, 2.59757],
84 (self.idk, self.idk),
85 ]
86
87 def __getstate__(self):
88 return SlotPickleMixin.__getstate__(self)
89
90 def __setstate__(self, object_state):
91 SlotPickleMixin.__setstate__(self, object_state)
92
93
94 class MyClassSimple(object):
95 def __init__(self):
96 self.idk = {"a": 0}
97 self.idk2 = ["a", 0]
98
99
100 HOMOGENOUS_COMPLEX_DICT = {
101 "a": 0,
102 "b": 4.2,
103 "c": {6.9: "d"},
104 -1: {5: 5},
105 6: {8.42: -2.5},
106 }
107 HOMOGENOUS_COMPLEX_LIST = [
108 "a",
109 0,
110 "b",
111 4.2,
112 "c",
113 [6.9, "d"],
114 -1,
115 [5, 5],
116 6,
117 [8.42, -2.5],
118 ]
119 HOMOGENOUS_COMPLEX_TUPLE = (
120 "a",
121 0,
122 "b",
123 4.2,
124 "c",
125 (6.9, "d"),
126 -1,
127 (5, 5),
128 6,
129 (8.42, -2.5),
130 )
131 # can't put a set inside a set :(
132 HOMOGENOUS_COMPLEX_SET = {
133 "a",
134 0,
135 "b",
136 4.2,
137 "c",
138 (6.9, "d"),
139 -1,
140 (5, 5),
141 6,
142 (8.42, -2.5),
143 }
144
145 HETEROGENOUS_COMPLEX_DICT = {
146 "a": (0, "1"),
147 2.53: [3, 1],
148 "c": {4.2, 6.9},
149 "d": {1: 5.432},
150 }
151 HETEROGENOUS_COMPLEX_LIST = [
152 "a",
153 (0, "1"),
154 2.53,
155 [3, 1],
156 "c",
157 {4.2, 6.9},
158 "d",
159 {1: 5.432},
160 ]
161 HETEROGENOUS_COMPLEX_TUPLE = (
162 "a",
163 (0, "1"),
164 2.53,
165 [3, 1],
166 "c",
167 {4.2, 6.9},
168 "d",
169 {1: 5.432},
170 )
171 HETEROGENOUS_COMPLEX_SET = {
172 "a",
173 (0, "1"),
174 2.53,
175 (3, 1),
176 "c",
177 (4.2, 6.9),
178 "d",
179 (1, 5.432),
180 }
181
182 simple_dict_encoded = jsonpickle.encode({"a": 0, "b": 4.2, 3: 6.9})
183 simple_list_encoded = jsonpickle.encode(["a", 0, "b", 4.2, 3, 6.9])
184 simple_tuple_encoded = jsonpickle.encode(("a", 0, "b", 4.2, 3, 6.9))
185 simple_set_encoded = jsonpickle.encode({"a", 0, "b", 4.2, 3, 6.9})
186
187 homogenous_dict_encoded = jsonpickle.encode(HOMOGENOUS_COMPLEX_DICT)
188 homogenous_list_encoded = jsonpickle.encode(HOMOGENOUS_COMPLEX_LIST)
189 homogenous_tuple_encoded = jsonpickle.encode(HOMOGENOUS_COMPLEX_TUPLE)
190 homogenous_set_encoded = jsonpickle.encode(HOMOGENOUS_COMPLEX_SET)
191
192 heterogenous_dict_encoded = jsonpickle.encode(HETEROGENOUS_COMPLEX_DICT)
193 heterogenous_list_encoded = jsonpickle.encode(HETEROGENOUS_COMPLEX_LIST)
194 heterogenous_tuple_encoded = jsonpickle.encode(HETEROGENOUS_COMPLEX_TUPLE)
195 heterogenous_set_encoded = jsonpickle.encode(HETEROGENOUS_COMPLEX_SET)
196
197 simple_class_encoded = jsonpickle.encode(MyClassSimple())
198 class_encoded = jsonpickle.encode(MyClass())
199 state_class_encoded = jsonpickle.encode(MyClassGetState())
200 ###################################################################################
201
202 # SIMPLE PRIMITIVE ENCODE/DECODE
203
204
205 def simple_dict_encode(benchmark):
206 benchmark(jsonpickle.encode, {"a": 0})
207
208
209 def simple_dict_decode(benchmark):
210 benchmark(jsonpickle.decode, simple_dict_encoded)
211
212
213 def simple_list_encode(benchmark):
214 benchmark(jsonpickle.encode, ["a", 0])
215
216
217 def simple_list_decode(benchmark):
218 benchmark(jsonpickle.decode, simple_list_encoded)
219
220
221 def simple_tuple_encode(benchmark):
222 benchmark(jsonpickle.encode, ("a", 0))
223
224
225 def simple_tuple_decode(benchmark):
226 benchmark(jsonpickle.decode, simple_tuple_encoded)
227
228
229 def simple_set_encode(benchmark):
230 benchmark(jsonpickle.encode, {"a", 0})
231
232
233 def simple_set_decode(benchmark):
234 benchmark(jsonpickle.decode, simple_set_encoded)
235
236
237 # COMPLEX HOMOGENOUS PRIMITIVE ENCODE/DECODE
238 def complex_homogenous_dict_encode(benchmark):
239 benchmark(jsonpickle.encode, HOMOGENOUS_COMPLEX_DICT)
240
241
242 def complex_homogenous_dict_decode(benchmark):
243 benchmark(jsonpickle.decode, homogenous_dict_encoded)
244
245
246 def complex_homogenous_list_encode(benchmark):
247 benchmark(jsonpickle.encode, HOMOGENOUS_COMPLEX_LIST)
248
249
250 def complex_homogenous_list_decode(benchmark):
251 benchmark(jsonpickle.decode, homogenous_list_encoded)
252
253
254 def complex_homogenous_tuple_encode(benchmark):
255 benchmark(jsonpickle.encode, HOMOGENOUS_COMPLEX_TUPLE)
256
257
258 def complex_homogenous_tuple_decode(benchmark):
259 benchmark(jsonpickle.decode, homogenous_tuple_encoded)
260
261
262 def complex_homogenous_set_encode(benchmark):
263 benchmark(jsonpickle.encode, HOMOGENOUS_COMPLEX_SET)
264
265
266 def complex_homogenous_set_decode(benchmark):
267 benchmark(jsonpickle.decode, homogenous_set_encoded)
268
269
270 # COMPLEX HETEROGENOUS PRIMITIVE ENCODE/DECODE
271 def complex_heterogenous_dict_encode(benchmark):
272 benchmark(jsonpickle.encode, HETEROGENOUS_COMPLEX_DICT)
273
274
275 def complex_heterogenous_dict_decode(benchmark):
276 benchmark(jsonpickle.decode, heterogenous_dict_encoded)
277
278
279 def complex_heterogenous_list_encode(benchmark):
280 benchmark(jsonpickle.encode, HETEROGENOUS_COMPLEX_LIST)
281
282
283 def complex_heterogenous_list_decode(benchmark):
284 benchmark(jsonpickle.decode, heterogenous_list_encoded)
285
286
287 def complex_heterogenous_tuple_encode(benchmark):
288 benchmark(jsonpickle.encode, HETEROGENOUS_COMPLEX_TUPLE)
289
290
291 def complex_heterogenous_tuple_decode(benchmark):
292 benchmark(jsonpickle.decode, heterogenous_tuple_encoded)
293
294
295 def complex_heterogenous_set_encode(benchmark):
296 benchmark(jsonpickle.encode, HETEROGENOUS_COMPLEX_SET)
297
298
299 def complex_heterogenous_set_decode(benchmark):
300 benchmark(jsonpickle.decode, heterogenous_set_encoded)
301
302
303 # CUSTOM CLASS ENCODE/DECODE
304 def simple_class_encode(benchmark):
305 benchmark(jsonpickle.encode, MyClassSimple())
306
307
308 def simple_class_decode(benchmark):
309 benchmark(jsonpickle.decode, simple_class_encoded)
310
311
312 def complex_class_encode(benchmark):
313 benchmark(jsonpickle.encode, MyClass())
314
315
316 def complex_class_decode(benchmark):
317 benchmark(jsonpickle.decode, class_encoded)
318
319
320 def state_class_encode(benchmark):
321 benchmark(jsonpickle.encode, MyClassGetState())
322
323
324 def state_class_decode(benchmark):
325 benchmark(jsonpickle.decode, state_class_encoded)