Skip to content

Instantly share code, notes, and snippets.

@pablito56
Last active August 29, 2015 13:57
Show Gist options
  • Save pablito56/9899563 to your computer and use it in GitHub Desktop.
Save pablito56/9899563 to your computer and use it in GitHub Desktop.
MongoDB collections creation timing
#!/usr/bin/env python2.7
#-*- coding: utf-8 -*-
#
# Authors:
#    Pablo Enfedaque <pev@tid.es> - 2014
#
"""
Performance comparison of MongoDB operations
"""
from datetime import datetime
from time import sleep
from pymongo import MongoClient
db = MongoClient()["mongotimes"]
try:
while True:
colls = db.collection_names(False)
print datetime.now(), "NUM COLLECTIONS:", len(colls)
sleep(1.0)
except KeyboardInterrupt:
pass
#!/usr/bin/env python2.7
#-*- coding: utf-8 -*-
#
# Authors:
#    Pablo Enfedaque <pev@tid.es> - 2014
#
"""
Performance comparison of MongoDB operations
"""
from sys import argv
from timeit import timeit
from random import choice, seed
from string import ascii_letters
from datetime import datetime
from pymongo import MongoClient
seed()
NAMES_LEN = 10
NUM_COLLS = 50
DB_NAME = "mongotimes"
conn = MongoClient()
db = conn[DB_NAME]
def explicit_collection_creation():
coll_names = ["".join(choice(ascii_letters) for _ in xrange(NAMES_LEN)) for _ in xrange(NUM_COLLS)]
for name in coll_names:
db.create_collection(name)
db[name].insert({"user_id": name, "date": datetime.utcnow()})
def implicit_collection_creation():
coll_names = ["".join(choice(ascii_letters) for _ in xrange(NAMES_LEN)) for _ in xrange(NUM_COLLS)]
for name in coll_names:
db[name].insert({"user_id": name, "date": datetime.utcnow()})
def no_collection_creation():
coll_names = ["".join(choice(ascii_letters) for _ in xrange(NAMES_LEN)) for _ in xrange(NUM_COLLS)]
for name in coll_names:
db["dbname"].insert({"user_id": name, "date": datetime.utcnow()})
number = 10 if len(argv) < 2 else int(argv[1])
conn.drop_database(DB_NAME)
print datetime.now(), "--- TEST NOT DROPPING DB ---"
t = timeit(explicit_collection_creation, number=number)
print datetime.now(), "EXPLICIT CREATION:", t
t = timeit(implicit_collection_creation, number=number)
print datetime.now(), "IMPLICIT CREATION:", t
t = timeit(no_collection_creation, number=number)
print datetime.now(), "NO CREATION:", t
conn.drop_database(DB_NAME)
print datetime.now(), "--- TEST DROPPING DB ---"
t = timeit(explicit_collection_creation, number=number)
print datetime.now(), "EXPLICIT CREATION:", t
conn.drop_database(DB_NAME)
t = timeit(implicit_collection_creation, number=number)
print datetime.now(), "IMPLICIT CREATION:", t
conn.drop_database(DB_NAME)
t = timeit(no_collection_creation, number=number)
print datetime.now(), "NO CREATION:", t
$ python collections_creation_timing.py 100
2014-03-31 19:17:34.495816 --- TEST NOT DROPPING DB ---
2014-03-31 19:20:07.772564 EXPLICIT CREATION: 153.276600838
2014-03-31 19:20:13.638951 IMPLICIT CREATION: 5.86628293991
2014-03-31 19:20:15.246171 NO CREATION: 1.60711812973
2014-03-31 19:20:15.906324 --- TEST DROPPING DB ---
2014-03-31 19:22:48.116410 EXPLICIT CREATION: 152.209965944
2014-03-31 19:22:54.014151 IMPLICIT CREATION: 5.56894993782
2014-03-31 19:22:57.769845 NO CREATION: 2.46466207504
$ python collections_creation_timing.py 100 # Changed NUM_COLLS to 1000
2014-03-31 19:59:30.187550 --- TEST NOT DROPPING DB ---
2014-03-31 19:59:41.347007 IMPLICIT CREATION: 11.1593368053
2014-03-31 19:59:43.878950 NO CREATION: 2.53181004524
2014-03-31 19:59:45.345161 --- TEST DROPPING DB ---
2014-03-31 19:59:56.902443 IMPLICIT CREATION: 11.5558490753
2014-03-31 20:00:01.826150 NO CREATION: 3.3363199234
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment