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