Skip to content

Instantly share code, notes, and snippets.

@weldpua2008
Created June 27, 2018 05:02
Show Gist options
  • Select an option

  • Save weldpua2008/140f711218a756d0dddf7e319100b633 to your computer and use it in GitHub Desktop.

Select an option

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
#!/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