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 <[email protected]> - 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment