Skip to content

Update dbtools.py #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions app/dbtools.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ def run_latency_test(
database="",
url="",
interval=1.0,
period=10
period=10,
custom_sql=""
):
query_times = []
result_info = {"success": False, "error": None, "latency_stats": {}, "details": []}
custom_sql = (custom_sql or "").strip()
try:
end_time = time.perf_counter() + period
while time.perf_counter() < end_time:
Expand All @@ -36,29 +38,33 @@ def run_latency_test(
if dbtype == "oracle":
conn = oracledb.connect(user=username, password=password, dsn=host)
cursor = conn.cursor()
cursor.execute("select 1 from dual")
sql = custom_sql if custom_sql else "select 1 from dual"
cursor.execute(sql)
cursor.fetchall()
cursor.close()
conn.close()
elif dbtype == "postgresql":
conn = psycopg2.connect(host=host, port=port, dbname=database, user=username, password=password)
cursor = conn.cursor()
cursor.execute("SELECT 1")
sql = custom_sql if custom_sql else "SELECT 1"
cursor.execute(sql)
cursor.fetchall()
cursor.close()
conn.close()
elif dbtype == "mysql":
conn = pymysql.connect(host=host, port=int(port), user=username, password=password, db=database)
cursor = conn.cursor()
cursor.execute("SELECT 1")
sql = custom_sql if custom_sql else "SELECT 1"
cursor.execute(sql)
cursor.fetchall()
cursor.close()
conn.close()
elif dbtype == "sqlserver" and mssql_ok:
conn_str = f"DRIVER={{ODBC Driver 17 for SQL Server}};SERVER={host},{port};DATABASE={database};UID={username};PWD={password}"
conn = pyodbc.connect(conn_str)
cursor = conn.cursor()
cursor.execute("SELECT 1")
sql = custom_sql if custom_sql else "SELECT 1"
cursor.execute(sql)
cursor.fetchall()
cursor.close()
conn.close()
Expand All @@ -81,7 +87,6 @@ def run_latency_test(
"error": error
})
time.sleep(interval)
# Compute p99, p90, avg, stddev, mean on all query_times
arr = np.array(query_times)
result_info["latency_stats"] = {
"p99": float(np.percentile(arr, 99)) if len(arr) else None,
Expand Down