Created
September 8, 2016 08:20
-
-
Save adieu/c8724f6e44bf12f5cc7e03bb5ac3b265 to your computer and use it in GitHub Desktop.
python db profiling
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
HOST = '' | |
USER = '' | |
PASSWORD = '' | |
DB = '' | |
WORKER = 20 | |
COUNT = 10000 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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