Instantly share code, notes, and snippets.

Embed
What would you like to do?
# -*- coding: utf-8 -*-
# Set up the connection once to avoid the overhead having to do it for every test
setup = """\
from pymongo import MongoClient
client = MongoClient('mongodb://mongo-a1:27017/')
db = client['test']
collection = db['test']
from __main__ import test
"""
def test(collection, w, j):
# Define a simple test doc
doc = {"field1" : 5, "field2" : True, "field3" : "hat"}
# Do the insert
try:
insert = collection.insert(doc, w = w, j = j)
except Exception, e:
print e
if __name__ == '__main__':
def time(calls, w, j):
import timeit
if isinstance(w, basestring):
timer = timeit.Timer("test(collection, w = 'majority', j = %d)" % (j), setup = setup)
else:
timer = timeit.Timer("test(collection, w = %d, j = %d)" % (w, j), setup = setup)
result = timer.repeat(repeat = 3, number = calls)
return min(result) # mean/stddev not useful as per http://docs.python.org/2/library/timeit.html#timeit.Timer.repeat
calls = 1000
# Test 1
w = 0
j = 0
totalTime = time(calls, w = w, j = j)
print "test(w = %d, j = %d): %s (%s per call)" % (w, j, round(totalTime, 6), round(totalTime / calls, 6))
# Test 2
w = 1
j = 0
totalTime = time(calls, w = w, j = j)
print "test(w = %d, j = %d): %s (%s per call)" % (w, j, round(totalTime, 6), round(totalTime / calls, 6))
# Test 3
w = 1
j = 1
totalTime = time(calls, w = w, j = j)
print "test(w = %d, j = %d): %s (%s per call)" % (w, j, round(totalTime, 6), round(totalTime / calls, 6))
# Test 4
w = 2
j = 0
totalTime = time(calls, w = w, j = j)
print "test(w = %d, j = %d): %s (%s per call)" % (w, j, round(totalTime, 6), round(totalTime / calls, 6))
# Test 5
w = 2
j = 1
totalTime = time(calls, w = w, j = j)
print "test(w = %d, j = %d): %s (%s per call)" % (w, j, round(totalTime, 6), round(totalTime / calls, 6))
# Test 6
w = 'majority'
j = 0
totalTime = time(calls, w = w, j = j)
print "test(w = %s, j = %d): %s (%s per call)" % (w, j, round(totalTime, 6), round(totalTime / calls, 6))
# Test 7
w = 'majority'
j = 1
totalTime = time(calls, w = w, j = j)
print "test(w = %s, j = %d): %s (%s per call)" % (w, j, round(totalTime, 6), round(totalTime / calls, 6))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment