Commits

Rune Halvorsen committed 814f8c2

Added serialization testing, not just parsing

Comments (0)

Files changed (1)

tests/benchmark.py

 
 
 def do_benchmark(impspec, json, runs=10):
-    modulename, funcname = impspec
+    modulename, parsename, emitname = impspec
 
     try:
         __import__(modulename)
         mod = sys.modules[modulename]
-        fun = getattr(mod, funcname)
+        reads = getattr(mod, parsename)
+        dumps = getattr(mod, emitname)
     except:
         return None
 
     start = time.time()
     for n in xrange(runs):
-        data = fun(json)
+        data = reads(json)
 
-    end = time.time()
+    readtime = time.time() - start
 
-    return end-start
+    start = time.time()
+    for n in xrange(runs):
+        data = dumps(data)
 
+    return readtime, time.time() - start # tuple (readtime, writetime)
 
 
-modules = [("json", "loads"),
-           ("simplejson", "loads"),
-           ("yajl", "loads"),
-           ("cjson", "decode"),
-           ("django.utils.simplejson", "loads"),
-           ("jsonpickle", "decode"),
-           ("jsonlib", "read"),
-           ("jsonlib2", "read"),
+modules = [("json", "loads", "dumps"),
+           ("simplejson", "loads", "dumps"),
+           ("yajl", "loads", "dumps"),
+           ("cjson", "decode", "encode"),
+           ("django.utils.simplejson", "loads", "dumps"),
+           ("jsonpickle", "decode", "encode"),
+           #("jsonlib", "read", "write"),
+           ("jsonlib2", "read", "write"),
        #    ("demjson", "decode"), terribly slow. wont include it
            ]
 
-
 if len(sys.argv) > 1 and sys.argv[1] == "--download":
     load_external_json()
 
 
 no_res = set([e for e in res if e[1] is None])
 res = list(set(res) - no_res)
-res.sort(lambda a,b: cmp(sum(a[1:]), sum(b[1:])))
+res = [(e[0], sum(map(lambda x:x[0], e[1:])), sum(map(lambda x:x[1], e[1:]))) for e in res]
 
+res.sort(lambda a,b: cmp((a[1]+a[2]), b[1]+b[2]))
+
+print "Total  Read   Write  Implementation"
+print "-----------------------------------"
 for e in res:
-    print "%.3f %s" % (sum(e[1:]), e[0])
+    print "%.3f  %.3f  %.3f  %s" % (e[1]+e[2], e[1], e[2], e[0])
 for e in no_res:
     print "Not installed:", e[0]