Skip to content

Instantly share code, notes, and snippets.

@smerritt
Created January 20, 2014 22:50
Show Gist options
  • Save smerritt/8530982 to your computer and use it in GitHub Desktop.
Save smerritt/8530982 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Benchmark the relative performance of stdlib's json, simplejson, and
# a wrapped simplejson that gives the same API as stdlib's json.
import benchmark
import hashlib
import json as stdjson
import simplejson
from datetime import datetime
# dumped out of MemcacheRing.set(), so it's not just realistic, it's real
# data.
CONTAINER_INFO = {
'bytes': '0',
'cors': {'allow_origin': None,
'expose_headers': None,
'max_age': None},
'meta': {'bert': 'ernie',
'bucky': 'balls',
'holy': 'guacamole',
'pants': 'moderately okayish',
'bit my sister': 'm\xc3\xb8\xc3\xb8se', # møøse
'thing1': 'thing2'},
'object_count': '0',
'read_acl': None,
'status': 204,
'sync_key': None,
'versions': None,
'write_acl': None}
class simple2std(object):
@classmethod
def loads(cls, s, *a, **kw):
return simplejson.loads(unicode(s, "UTF8"), *a, **kw)
# quick test to make sure that this little adapter actually works
adapted = repr(simple2std.loads(stdjson.dumps(CONTAINER_INFO)))
std = repr(stdjson.loads(stdjson.dumps(CONTAINER_INFO)))
if not adapted == std:
print "simplejson\n==========\n%s\n\njson\n====\n%s" % (adapted, std)
exit(1)
class BenchmarkContainerListingGeneration(benchmark.Benchmark):
each = 250
def setUp(self):
self.container_listing = [
("kitten-%5d.jpg" % i,
datetime.utcfromtimestamp(i).isoformat(),
"application/catphoto",
hashlib.md5(str(i)).hexdigest())
for i in xrange(10000)]
def test_simplejson(self):
simplejson.dumps(self.container_listing)
def test_stdjson(self):
stdjson.dumps(self.container_listing)
class BenchmarkSerializeForMemcache(benchmark.Benchmark):
each = 1000
def test_simplejson(self):
simplejson.dumps(CONTAINER_INFO)
def test_stdjson(self):
stdjson.dumps(CONTAINER_INFO)
class BenchmarkDeserializeForMemcache(benchmark.Benchmark):
each = 1000
def setUp(self):
self.serialized_container_info = stdjson.dumps(CONTAINER_INFO)
def test_simplejson(self):
simplejson.loads(self.serialized_container_info)
def test_stdjson(self):
stdjson.loads(self.serialized_container_info)
def test_simple2std(self):
simple2std.loads(self.serialized_container_info)
class BenchmarkInAndOutOfMemcache(benchmark.Benchmark):
"""
Simulate putting things into and pulling things out of memcache.
Takes a wild guess that things get read 10x as often as written.
"""
each = 500
reads_per_write = 10
def test_simplejson(self):
serialized = simplejson.dumps(CONTAINER_INFO)
for _ in xrange(self.reads_per_write):
simplejson.loads(serialized)
def test_stdjson(self):
serialized = stdjson.dumps(CONTAINER_INFO)
for _ in xrange(self.reads_per_write):
stdjson.loads(serialized)
def test_simple2std(self):
serialized = simplejson.dumps(CONTAINER_INFO)
for _ in xrange(self.reads_per_write):
simple2std.loads(serialized)
if __name__ == "__main__":
benchmark.main(format="markdown", numberFormat="%.6g")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment