Skip to content

Instantly share code, notes, and snippets.

@pablito56
Last active August 29, 2015 13:57

Revisions

  1. pablito56 revised this gist Mar 31, 2014. 2 changed files with 43 additions and 0 deletions.
    26 changes: 26 additions & 0 deletions collections_count.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    #!/usr/bin/env python2.7
    #-*- coding: utf-8 -*-
    #
    # Authors:
    #    Pablo Enfedaque <[email protected]> - 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
    17 changes: 17 additions & 0 deletions outputs.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    $ 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
  2. pablito56 created this gist Mar 31, 2014.
    67 changes: 67 additions & 0 deletions collections_creation_timing.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,67 @@
    #!/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