Skip to content

Instantly share code, notes, and snippets.

@srkiNZ84
Created January 6, 2020 20:08
Show Gist options
  • Save srkiNZ84/2d0c2332e1eb6f4ebe1a464c68e31d5f to your computer and use it in GitHub Desktop.
Save srkiNZ84/2d0c2332e1eb6f4ebe1a464c68e31d5f to your computer and use it in GitHub Desktop.
Simple Python script to test DB connection latency
#!/usr/bin/python3
# NOTE: If running on Ubuntu you need to install the "libmysqlclient-dev" package from apt and the "mysql" package through pip
# apt install libmysqlclient-dev
# pip3 install mysql
import MySQLdb
import time
NUM_ITERATIONS = 100
db = MySQLdb.connect(host="remote.url.example.com", # your host, usually localhost
user="user", # your username
passwd="secret", # your password
db="somedb") # name of the data base
cur = db.cursor()
latencyTimes = [None] * NUM_ITERATIONS
for iteration in range(NUM_ITERATIONS):
start = time.time()
cur.execute("SELECT 1")
end = time.time()
print("iteration " + str(iteration) + " time was " + str(end - start))
latencyTimes[iteration] = end - start
average = sum(latencyTimes)/len(latencyTimes)
print("Average was " + str(average) + " seconds")
db.close()
#!/usr/bin/python3
# NOTE: If running on Ubuntu you need to first install the "libpq-dev" package from apt and then the "psycopg2" pacakge from pip
# apt install libpq-dev
# pip3 install psycopg2
import psycopg2
import time
NUM_ITERATIONS = 100
db = psycopg2.connect(host="localhost", # your host, usually localhost
user="user", # your username
password="secret", # your password
dbname="ffdb") # name of the data base
cur = db.cursor()
latencyTimes = [None] * NUM_ITERATIONS
for iteration in range(NUM_ITERATIONS):
start = time.time()
cur.execute("SELECT 1")
end = time.time()
print("iteration " + str(iteration) + " time was " + str(end - start))
latencyTimes[iteration] = end - start
average = sum(latencyTimes)/len(latencyTimes)
print("Average was " + str(average) + " milliseconds")
db.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment