Guest User

MySQLdb vs mysql.connector vs DBUtils.PooledDB

a guest
Sep 25th, 2012
205
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import MySQLdb
  2. import mysql.connector
  3. from DBUtils.PooledDB import PooledDB
  4. from time import time
  5.  
  6. def pool(lib):
  7.         return PooledDB(lib, host="localhost", user="test", passwd="test", db="test")
  8.        
  9. def conn(lib):
  10.         return lib.connect(host="localhost", user="test", passwd="test", db="test")
  11.  
  12. def timeit_pool(steps, pool):
  13.         start = time()
  14.         for a in range(1,steps):
  15.                 conn = pool.connection()
  16.                 cursor = conn.cursor()
  17.                 cursor.execute("Select %s, %s;", ('a',1))
  18.                 result = cursor.fetchone()
  19.                 cursor.close()
  20.                 conn.close()
  21.         return time()-start
  22.        
  23. def timeit_wo_pool(steps, conn):
  24.         start = time()
  25.         for a in range(1,steps):
  26.                 cursor = conn.cursor()
  27.                 cursor.execute("Select %s, %s;", ('a',1))
  28.                 result = cursor.fetchone()
  29.                 cursor.close()
  30.         conn.close()
  31.         return time()-start
  32.  
  33. steps = 10000
  34.  
  35. print timeit_pool(steps, pool(MySQLdb)), 'MySQLdb + pool'
  36. print timeit_pool(steps, pool(mysql.connector)), 'mysql.connector + pool'
  37. print timeit_wo_pool(steps, conn(MySQLdb)), 'MySQLdb'
  38. print timeit_wo_pool(steps, conn(mysql.connector)), 'mysql.connector'
RAW Paste Data