boltons/misc/bench_omd.py

123 lines
3.2 KiB
Python
Raw Normal View History

2013-08-25 18:34:10 +00:00
# -*- coding: utf-8 -*-
2013-09-08 01:14:19 +00:00
2013-08-28 05:20:04 +00:00
import string
2013-08-25 18:34:10 +00:00
import sys
sys.path.append('/home/mahmoud/projects/lithoxyl/')
import time
import lithoxyl
from lithoxyl import sinks, logger
from dictutils import OMD, FastIterOrderedMultiDict
2013-08-25 18:34:10 +00:00
from collections import OrderedDict as OD
q_sink = lithoxyl.sinks.QuantileSink()
log = lithoxyl.logger.BaseLogger('bench_stats', sinks=[q_sink])
try:
profile
except NameError:
2013-08-27 02:59:27 +00:00
times = 10
size = 10000
redun = 2
2013-08-25 18:34:10 +00:00
else:
times = 1
size = 10000
redun = 10
_rng = range(size / redun) * redun
_unique_keys = set(_rng)
_bad_rng = range(size, size + size)
2013-08-25 18:34:10 +00:00
_pairs = zip(_rng, _rng)
2013-09-08 01:10:34 +00:00
# order matters because 'pop' mutates
2013-09-01 06:58:44 +00:00
_shared_actions = ('setitem', 'iteritems', 'iterkeys', 'getitem', 'keyerror', 'pop')
_multi_actions = ('multi_iteritems',)
_all_actions = ('init',) + _multi_actions + _shared_actions
2013-09-08 01:10:34 +00:00
MULTI_IMPLS = (FastIterOrderedMultiDict, OMD)
try:
from werkzeug.datastructures import MultiDict, OrderedMultiDict as WOMD
MULTI_IMPLS += (WOMD, MultiDict)
except ImportError:
print '(installing werkzeug is recommended for full comparison)'
2013-09-01 06:58:44 +00:00
ALL_IMPLS = MULTI_IMPLS + (OD, dict)
2013-08-27 02:59:27 +00:00
2013-08-25 18:34:10 +00:00
def bench():
2013-09-01 06:58:44 +00:00
for impl in ALL_IMPLS:
q_sink = lithoxyl.sinks.QuantileSink()
2013-08-27 02:59:27 +00:00
impl_name = '.'.join([impl.__module__, impl.__name__])
log = lithoxyl.logger.BaseLogger(impl_name, sinks=[q_sink])
print
2013-08-27 02:59:27 +00:00
print '+ %s' % impl_name
for _ in range(times):
2013-08-28 05:20:04 +00:00
with log.info('total'):
2013-08-27 02:59:27 +00:00
for _ in range(times):
with log.info('init'):
target_dict = impl(_pairs)
2013-09-01 06:58:44 +00:00
if impl in MULTI_IMPLS:
_actions = _multi_actions + _shared_actions
else:
_actions = _shared_actions
2013-08-27 02:59:27 +00:00
for action in _actions:
action_func = globals()['_do_' + action]
with log.info(action):
action_func(target_dict)
2013-08-28 05:20:04 +00:00
for action in _all_actions:
2013-09-01 06:58:44 +00:00
try:
best_msecs = q_sink.qas[impl_name][action].min * 1000
print ' - %s - %g ms' % (action, best_msecs)
except KeyError:
pass
2013-08-27 02:59:27 +00:00
best_msecs = q_sink.qas[impl_name]['total'].min * 1000
median_msecs = q_sink.qas[impl_name]['total'].median * 1000
print ' > ran %d loops of %d items each, best time: %g ms, median time: %g ms' % (times, size, best_msecs, median_msecs)
2013-08-25 18:53:05 +00:00
print
return
2013-08-28 05:20:04 +00:00
def _do_setitem(target_dict):
for k, i in enumerate(string.lowercase):
target_dict[k] = i
2013-08-27 02:59:27 +00:00
def _do_iteritems(target_dict):
[_ for _ in target_dict.iteritems()]
def _do_iterkeys(target_dict):
[_ for _ in target_dict.iterkeys()]
2013-09-01 06:58:44 +00:00
def _do_multi_iteritems(target_dict):
[_ for _ in target_dict.iteritems(multi=True)]
def _do_multi_iterkeys(target_dict):
[_ for _ in target_dict.iterkeys(multi=True)]
2013-08-27 02:59:27 +00:00
def _do_getitem(target_dict):
for k in _rng:
target_dict[k]
def _do_keyerror(target_dict):
for k in _bad_rng:
try:
target_dict[k]
except KeyError:
pass
def _do_pop(target_dict):
for k in _unique_keys:
target_dict.pop(k)
assert not target_dict
if __name__ == '__main__':
bench()