Created
March 5, 2012 17:51
-
-
Save codeb2cc/1979843 to your computer and use it in GitHub Desktop.
MongoDB Insert VS Save
This file contains hidden or 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 -*- | |
# MongoDB Benmarking with Mongoengine | |
import os, time, multiprocessing | |
import pymongo | |
import mongoengine | |
DB_HOST = '127.0.0.1' | |
DB_PORT = 27017 | |
class DocA(mongoengine.Document): | |
field_a = mongoengine.IntField() | |
field_b = mongoengine.StringField() | |
field_c = mongoengine.ListField(mongoengine.StringField()) | |
field_d = mongoengine.DictField() | |
def _conect_database(): | |
mongoengine.connect('TestDB', host=DB_HOST, port=DB_PORT) | |
def _create_database(): | |
connection = pymongo.Connection(DB_HOST, DB_PORT) | |
db = connection['TestDB'] | |
connection.close() | |
_conect_database() | |
def _drop_database(): | |
connection = pymongo.Connection(DB_HOST, DB_PORT) | |
connection.drop_database('TestDB') | |
def _create_by_insert(num): | |
print '>>> Insert:' | |
insert_begin = time.time() | |
new_docs = [] | |
for i in xrange(num): | |
doc = DocA( | |
field_a = i, | |
field_b = 'Doc #%d' % i, | |
field_c = ['Hello', 'XingCloud', ], | |
field_d = { | |
'TBBT': 'The Big Bang Theory', | |
}, | |
) | |
new_docs.append(doc) | |
DocA.objects.insert(new_docs, load_bulk=False) | |
insert_end = time.time() | |
print ' Eclipse: %f' % (insert_end - insert_begin) | |
def _create_by_save(num): | |
print 'PID: %s >>> Save:' % os.getpid() | |
save_begin = time.time() | |
for i in xrange(num): | |
doc = DocA( | |
field_a = i, | |
field_b = 'Doc #%d' % i, | |
field_c = ['Hello', 'XingCloud', ], | |
field_d = { | |
'TBBT': 'The Big Bang Theory', | |
}, | |
) | |
doc.save() | |
save_end = time.time() | |
print 'PID: %s Eclipse: %f' % (os.getpid(), save_end - save_begin) | |
if __name__ == '__main__': | |
SAMPLE_SIZE = 16000 | |
PROCESS_NUM = 2 | |
_create_database() | |
print 'Database Created ...' | |
print '=== Benchmark Begin (Sample Size: %d) ===' % SAMPLE_SIZE | |
_create_database() | |
print 'Database Created ...' | |
_create_by_insert(SAMPLE_SIZE) | |
print 'Database Count: %d' % DocA.objects.count() | |
_drop_database() | |
print 'Test Database Droped...' | |
_create_database() | |
print 'Database Created ...' | |
for i in xrange(PROCESS_NUM): | |
multiprocessing.Process( | |
target = _create_by_save, | |
args = (SAMPLE_SIZE / PROCESS_NUM, ), | |
).start() | |
time.sleep(10) | |
print 'Database Count: %d' % DocA.objects.count() | |
_drop_database() | |
print 'Test Database Droped...' | |
print '=== Benchmark End ===' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment