public
Created

  • Download Gist
jsonperf.py
Python
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
#!/usr/bin/env python
from datetime import datetime
import os, sys
 
import json
import simplejson
import cjson
import jsonlib2
 
 
small_doc_string = '{"type":"counting"}'
large_doc_string = open(os.path.join(os.path.dirname(__file__), 'large_doc.json'), 'r').read()
small_doc_dict = {'type':'counting'}
large_doc_dict = json.loads(large_doc_string)
 
# to_seconds_float from
# http://stackoverflow.com/questions/1083402/missing-datetime-timedelta-toseconds-float-in-python
def to_seconds_float(timedelta):
"""Calculate floating point representation of combined
seconds/microseconds attributes in :param:`timedelta`.
 
:raise ValueError: If :param:`timedelta.days` is truthy.
 
>>> to_seconds_float(datetime.timedelta(seconds=1, milliseconds=500))
1.5
>>> too_big = datetime.timedelta(days=1, seconds=12)
>>> to_seconds_float(too_big) # doctest: +ELLIPSIS
Traceback (most recent call last):
...
ValueError: ('Must not have days', datetime.timedelta(1, 12))
"""
if timedelta.days:
raise ValueError('Must not have days', timedelta)
return timedelta.seconds + timedelta.microseconds / 1E6
 
class ComparisonTimer(object):
def __init__(self, func, assertion=None):
self.func = func
self.timers = []
self.assertion = assertion
def run(self):
starttime = datetime.now()
result = self.func()
endtime = datetime.now()
if self.assertion is None:
assert result
else:
assert result == self.assertion
self.timers.append(endtime - starttime)
def sum(self):
return sum([to_seconds_float(t) for t in self.timers]) / len(self.timers)
def repeat(self, number):
for x in range(number):
self.run()
return sum([to_seconds_float(t) for t in self.timers])
 
def test(func, assertion=None, repeat=100):
sys.stdout.write('.'); sys.stdout.flush();
return ComparisonTimer(func, assertion).repeat(repeat)
 
def run_test(store, func_dict, arg, assertion):
for key, func in func_dict.items():
store[key].append(test(lambda : func(arg), assertion))
 
def run():
parse_times = {'json':[],'jsonlib2':[],'simplejson':[],'cjson':[]}
dump_times = {'json':[],'jsonlib2':[],'simplejson':[],'cjson':[]}
parse_func_dict = {'json':json.loads, 'jsonlib2':jsonlib2.loads,
'simplejson':simplejson.loads, 'cjson':cjson.decode}
dump_func_dict = {'json':json.dumps, 'jsonlib2':jsonlib2.dumps,
'simplejson':simplejson.dumps, 'cjson':cjson.encode}
for x in range(100):
run_test(parse_times, parse_func_dict, large_doc_string, large_doc_dict)
run_test(dump_times, dump_func_dict, large_doc_dict, None)
print ''
for k, v in parse_times.items():
print 'ParseTime:', k, str(sum(v) / len(v) * 1000)+'ms'
for k, v in dump_times.items():
print 'DumpTime:', k, str(sum(v) / len(v) * 1000)+'ms'
 
if __name__ == "__main__":
try:
run()
except Exception, e:
import pdb
pdb.post_mortem()

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.