Skip to content

Instantly share code, notes, and snippets.

@adieu
Created September 8, 2016 08:20
Show Gist options
  • Save adieu/c8724f6e44bf12f5cc7e03bb5ac3b265 to your computer and use it in GitHub Desktop.
Save adieu/c8724f6e44bf12f5cc7e03bb5ac3b265 to your computer and use it in GitHub Desktop.
python db profiling
HOST = ''
USER = ''
PASSWORD = ''
DB = ''
WORKER = 20
COUNT = 10000
from pymysql_case import task
import GreenletProfiler
GreenletProfiler.set_clock_type('cpu')
GreenletProfiler.start()
task()
GreenletProfiler.stop()
stats = GreenletProfiler.get_func_stats()
stats.print_all()
stats.save('profile.callgrind', type='callgrind')
from gevent import monkey; monkey.patch_all()
from gevent.pool import Pool
import conf
import pymysql
import pymysql.cursors
def worker(n):
# Connect to the database
connection = pymysql.connect(host=conf.HOST,
user=conf.USER,
password=conf.PASSWORD,
db=conf.DB,
)
try:
with connection.cursor() as cursor:
cursor.execute(conf.SQL)
result = cursor.fetchall()
finally:
connection.close()
def task():
pool = Pool(conf.WORKER)
pool.map(worker, xrange(conf.COUNT))
if __name__ == '__main__':
task()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment