Created
June 27, 2018 05:02
-
-
Save weldpua2008/140f711218a756d0dddf7e319100b633 to your computer and use it in GitHub Desktop.
Detecting max/min/avg response time to MySQL server with (SELECT 1) query
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
| #!/bin/env python | |
| ############################## | |
| # Dependency: | |
| # yum install MySQL-python -y | |
| ############################## | |
| import timeit | |
| import time | |
| import gc | |
| loops = 10 | |
| def _timer(): | |
| return int(round(time.time() * 1000)) | |
| import MySQLdb | |
| db = MySQLdb.connect(host="host",user="user",passwd="passwd",db="db") | |
| c = db.cursor() | |
| gcold = gc.isenabled() | |
| gc.disable() | |
| r = [] | |
| try: | |
| i=0 | |
| while i < loops: | |
| _t0 = _timer() | |
| c.execute("SELECT 1") | |
| _t1 = _timer() | |
| total_time = _t1 - _t0 | |
| r.append(total_time) | |
| i = i+1 | |
| print "Total loops: {0:.2f}ms".format(loops) | |
| print "Total time: {0:.2f}ms".format(sum(r)) | |
| print "Min time per loop: {0:.2f}ms".format(min(r)) | |
| print "Avg time per loop: {0:.2f}ms".format(sum(r) / loops) | |
| print "Max time per loop: {0:.2f}ms".format(max(r)) | |
| finally: | |
| if gcold: | |
| gc.enable() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment