Created
March 10, 2014 11:30
-
-
Save malanjp/9463385 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
from pymongo import MongoClient | |
from datetime import datetime | |
import time | |
limit = 100000 | |
print('insert count {0}'.format(limit)) | |
class Timer(object): | |
def __init__(self, verbose=False): | |
self.verbose = verbose | |
def __enter__(self): | |
self.start = time.time() | |
return self | |
def __exit__(self, *args): | |
self.end = time.time() | |
self.secs = self.end - self.start | |
self.msecs = self.secs * 1000 # millisecs | |
if self.verbose: | |
print('elapsed time: {0} ms').format(self.msecs) | |
client = MongoClient('localhost', 27017) | |
db = client.test_database | |
collection = db.bench | |
def remove(): | |
for data in collection.find(): | |
collection.remove(data, safe=True) | |
""" | |
insert | |
""" | |
remove() | |
with Timer() as t: | |
for count in range(limit): | |
data = { | |
'text': 'testtext', | |
'date': datetime.utcnow(), | |
} | |
collection.insert(data) | |
print('update =', t.secs) | |
""" | |
update | |
""" | |
remove() | |
with Timer() as t: | |
for count in range(limit): | |
data = { | |
'text': 'testtext', | |
'date': datetime.utcnow(), | |
} | |
collection.save(data) | |
print('insert =', t.secs) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment