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
|
"""Script for testing the performance of pickling/unpickling.
This will pickle/unpickle several real world-representative objects a few
thousand times. The methodology below was chosen for was chosen to be similar
to real-world scenarios which operate on single objects at a time. Note that if
we did something like
pickle.dumps([dict(some_dict) for _ in range(10000)])
this isn't equivalent to dumping the dict 10000 times: pickle uses a
highly-efficient encoding for the n-1 following copies.
"""
import datetime
import random
import sys
import pyperf
from memray_helper import get_tracker
IS_PYPY = pyperf.python_implementation() == "pypy"
__author__ = "collinwinter@google.com (Collin Winter)"
DICT = {
"ads_flags": 0,
"age": 18,
"birthday": datetime.date(1980, 5, 7),
"bulletin_count": 0,
"comment_count": 0,
"country": "BR",
"encrypted_id": "G9urXXAJwjE",
"favorite_count": 9,
"first_name": "",
"flags": 412317970704,
"friend_count": 0,
"gender": "m",
"gender_for_display": "Male",
"id": 302935349,
"is_custom_profile_icon": 0,
"last_name": "",
"locale_preference": "pt_BR",
"member": 0,
"tags": ["a", "b", "c", "d", "e", "f", "g"],
"profile_foo_id": 827119638,
"secure_encrypted_id": "Z_xxx2dYx3t4YAdnmfgyKw",
"session_number": 2,
"signup_id": "201-19225-223",
"status": "A",
"theme": 1,
"time_created": 1225237014,
"time_updated": 1233134493,
"unread_message_count": 0,
"user_group": "0",
"username": "collinwinter",
"play_count": 9,
"view_count": 7,
"zip": "",
}
TUPLE = (
[
265867233,
265868503,
265252341,
265243910,
265879514,
266219766,
266021701,
265843726,
265592821,
265246784,
265853180,
45526486,
265463699,
265848143,
265863062,
265392591,
265877490,
265823665,
265828884,
265753032,
],
60,
)
def mutate_dict(orig_dict, random_source):
new_dict = dict(orig_dict)
for key, value in new_dict.items():
rand_val = random_source.random() * sys.maxsize
if isinstance(key, (int, bytes, str)):
new_dict[key] = type(key)(rand_val)
return new_dict
random_source = random.Random(5) # Fixed seed.
DICT_GROUP = [mutate_dict(DICT, random_source) for _ in range(3)]
def bench_pickle(loops, pickle, options):
range_it = range(loops)
# micro-optimization: use fast local variables
dumps = pickle.dumps
objs = (DICT, TUPLE, DICT_GROUP)
protocol = options.protocol
with get_tracker():
t0 = pyperf.perf_counter()
for _ in range_it:
for obj in objs:
# 20 dumps
dumps(obj, protocol)
dumps(obj, protocol)
dumps(obj, protocol)
dumps(obj, protocol)
dumps(obj, protocol)
dumps(obj, protocol)
dumps(obj, protocol)
dumps(obj, protocol)
dumps(obj, protocol)
dumps(obj, protocol)
dumps(obj, protocol)
dumps(obj, protocol)
dumps(obj, protocol)
dumps(obj, protocol)
dumps(obj, protocol)
dumps(obj, protocol)
dumps(obj, protocol)
dumps(obj, protocol)
dumps(obj, protocol)
dumps(obj, protocol)
return pyperf.perf_counter() - t0
def bench_unpickle(loops, pickle, options):
pickled_dict = pickle.dumps(DICT, options.protocol)
pickled_tuple = pickle.dumps(TUPLE, options.protocol)
pickled_dict_group = pickle.dumps(DICT_GROUP, options.protocol)
range_it = range(loops)
# micro-optimization: use fast local variables
loads = pickle.loads
objs = (pickled_dict, pickled_tuple, pickled_dict_group)
with get_tracker():
t0 = pyperf.perf_counter()
for _ in range_it:
for obj in objs:
# 20 loads dict
loads(obj)
loads(obj)
loads(obj)
loads(obj)
loads(obj)
loads(obj)
loads(obj)
loads(obj)
loads(obj)
loads(obj)
loads(obj)
loads(obj)
loads(obj)
loads(obj)
loads(obj)
loads(obj)
loads(obj)
loads(obj)
loads(obj)
loads(obj)
return pyperf.perf_counter() - t0
LIST = [[list(range(10)), list(range(10))] for _ in range(10)]
def bench_pickle_list(loops, pickle, options):
range_it = range(loops)
# micro-optimization: use fast local variables
dumps = pickle.dumps
obj = LIST
protocol = options.protocol
with get_tracker():
t0 = pyperf.perf_counter()
for _ in range_it:
# 10 dumps list
dumps(obj, protocol)
dumps(obj, protocol)
dumps(obj, protocol)
dumps(obj, protocol)
dumps(obj, protocol)
dumps(obj, protocol)
dumps(obj, protocol)
dumps(obj, protocol)
dumps(obj, protocol)
dumps(obj, protocol)
return pyperf.perf_counter() - t0
def bench_unpickle_list(loops, pickle, options):
pickled_list = pickle.dumps(LIST, options.protocol)
range_it = range(loops)
# micro-optimization: use fast local variables
loads = pickle.loads
with get_tracker():
t0 = pyperf.perf_counter()
for _ in range_it:
# 10 loads list
loads(pickled_list)
loads(pickled_list)
loads(pickled_list)
loads(pickled_list)
loads(pickled_list)
loads(pickled_list)
loads(pickled_list)
loads(pickled_list)
loads(pickled_list)
loads(pickled_list)
return pyperf.perf_counter() - t0
MICRO_DICT = dict((key, dict.fromkeys(range(10))) for key in range(100))
def bench_pickle_dict(loops, pickle, options):
range_it = range(loops)
# micro-optimization: use fast local variables
protocol = options.protocol
obj = MICRO_DICT
with get_tracker():
t0 = pyperf.perf_counter()
for _ in range_it:
# 5 dumps dict
pickle.dumps(obj, protocol)
pickle.dumps(obj, protocol)
pickle.dumps(obj, protocol)
pickle.dumps(obj, protocol)
pickle.dumps(obj, protocol)
return pyperf.perf_counter() - t0
BENCHMARKS = {
# 20 inner-loops: don't count the 3 pickled objects
"pickle": (bench_pickle, 20),
# 20 inner-loops: don't count the 3 unpickled objects
"unpickle": (bench_unpickle, 20),
"pickle_list": (bench_pickle_list, 10),
"unpickle_list": (bench_unpickle_list, 10),
"pickle_dict": (bench_pickle_dict, 5),
}
def is_accelerated_module(module):
return getattr(module.Pickler, "__module__", "<jython>") != "pickle"
def add_cmdline_args(cmd, args):
if args.pure_python:
cmd.append("--pure-python")
cmd.extend(("--protocol", str(args.protocol)))
cmd.append(args.benchmark)
if __name__ == "__main__":
runner = pyperf.Runner(add_cmdline_args=add_cmdline_args)
runner.metadata["description"] = "Test the performance of pickling."
parser = runner.argparser
parser.add_argument(
"--pure-python", action="store_true", help="Use the C version of pickle."
)
parser.add_argument(
"--protocol",
action="store",
default=None,
type=int,
help="Which protocol to use (default: highest protocol).",
)
benchmarks = sorted(BENCHMARKS)
parser.add_argument("benchmark", choices=benchmarks)
options = runner.parse_args()
benchmark, inner_loops = BENCHMARKS[options.benchmark]
name = options.benchmark
if options.pure_python:
name += "_pure_python"
if not (options.pure_python or IS_PYPY):
# C accelerators are enabled by default on 3.x
import pickle
if not is_accelerated_module(pickle):
raise RuntimeError("Missing C accelerators for pickle")
else:
sys.modules["_pickle"] = None
import pickle
if is_accelerated_module(pickle):
raise RuntimeError("Unexpected C accelerators for pickle")
if options.protocol is None:
options.protocol = pickle.HIGHEST_PROTOCOL
runner.metadata["pickle_protocol"] = str(options.protocol)
runner.metadata["pickle_module"] = pickle.__name__
runner.bench_time_func(name, benchmark, pickle, options, inner_loops=inner_loops)
|