Source

txMysql / benchmarks / connect.py

Full commit
"""
connection benchmark

This simple script just attempts to connect to the database
many times serially.

This script simply requires a running server and database.

TODO: modify this or create a script that uses threads
      to open multiple connections at once to see how the
      driver handles that.

"""
import sys
import timeit


from txmysql import settings


def pymysql():
    import pymysql as db
    startTime = timeit.default_timer()
    for x in xrange(settings.ITERATIONS):
        con = db.connect(host=settings.HOSTNAME,
                         user=settings.USERNAME,
                         db=settings.DATABASE,
                         passwd="")
        con.close()
    endTime = timeit.default_timer()
    elapsedTime = endTime - startTime
    print "Pymysql connected %s times in %s"%(settings.ITERATIONS, elapsedTime)
    consPerSecond = settings.ITERATIONS / elapsedTime
    print "Pymysql connected at a rate of %s/s"%(consPerSecond,)
    avgConTime = elapsedTime / settings.ITERATIONS
    print "Pymysql average connection time %s"%(avgConTime,)


def mysqldb():
    import MySQLdb as db
    startTime = timeit.default_timer()
    for x in xrange(settings.ITERATIONS):
        con = db.connect(host=settings.HOSTNAME,
                         user=settings.USERNAME,
                         db=settings.DATABASE,
                         use_unicode=settings.USE_UNICODE)
        con.close()
    endTime = timeit.default_timer()
    elapsedTime = endTime - startTime
    print "MySQLdb connected %s times in %s"%(settings.ITERATIONS, elapsedTime)
    consPerSecond = settings.ITERATIONS / elapsedTime
    print "MySQLdb connected at a rate of %s/s"%(consPerSecond,)
    avgConTime = elapsedTime / settings.ITERATIONS
    print "MySQLdb average connection time %s"%(avgConTime,)


def oursql():
    import oursql as db
    startTime = timeit.default_timer()
    for x in xrange(settings.ITERATIONS):
        con = db.connect(host=settings.HOSTNAME,
                         user=settings.USERNAME,
                         db=settings.DATABASE,
                         use_unicode=settings.USE_UNICODE)
        con.close()
    endTime = timeit.default_timer()
    elapsedTime = endTime - startTime
    print "Oursql connected %s times in %s"%(settings.ITERATIONS, elapsedTime)
    consPerSecond = settings.ITERATIONS / elapsedTime
    print "Oursql connected at a rate of %s/s"%(consPerSecond,)
    avgConTime = elapsedTime / settings.ITERATIONS
    print "Oursql average connection time %s"%(avgConTime,)


def pymongo():
    import pymongo
    startTime = timeit.default_timer()
    for x in xrange(settings.ITERATIONS):
        con = pymongo.Connection(settings.HOSTNAME)
        db = con[settings.DATABASE]
        con.disconnect()
    endTime = timeit.default_timer()
    elapsedTime = endTime - startTime
    print "Pymongo connected %s times in %s"%(settings.ITERATIONS, elapsedTime)
    consPerSecond = settings.ITERATIONS / elapsedTime
    print "Pymongo connected at a rate of %s/s"%(consPerSecond,)
    avgConTime = elapsedTime / settings.ITERATIONS
    print "Pymongo average connection time %s"%(avgConTime,)


if __name__ == "__main__":
    if "mysqldb" in sys.argv:
        mysqldb()
    if "oursql" in sys.argv:
        oursql()
    if "pymongo" in sys.argv:
        pymongo()
    if "pymysql" in sys.argv:
        pymysql()