2022-07-21 11:56:41 +02:00
|
|
|
import time
|
|
|
|
|
import traceback
|
2022-07-21 12:24:35 +02:00
|
|
|
from typing import Union
|
2022-07-21 11:56:41 +02:00
|
|
|
|
2022-07-21 12:24:35 +02:00
|
|
|
import mysql.connector.connection
|
2022-07-21 11:56:41 +02:00
|
|
|
import trino.dbapi
|
|
|
|
|
|
2022-07-21 18:31:46 +02:00
|
|
|
from connections import get_connection, clean_up_connection
|
2022-07-26 14:22:48 +02:00
|
|
|
from _version import __version__
|
2022-07-21 11:56:41 +02:00
|
|
|
|
2022-07-21 12:24:35 +02:00
|
|
|
|
|
|
|
|
def run_measuring_session(config: dict) -> None:
|
|
|
|
|
"""
|
|
|
|
|
Complete session flow. Connect, test all queries.
|
|
|
|
|
|
|
|
|
|
:param config: the full config for the measuring session.
|
|
|
|
|
:return: None
|
|
|
|
|
"""
|
2022-07-26 14:22:48 +02:00
|
|
|
print(f"Query Performance Gauge - Version: {__version__}")
|
2022-07-21 11:56:41 +02:00
|
|
|
print("Starting the measuring session.")
|
|
|
|
|
|
2022-07-21 12:24:35 +02:00
|
|
|
connection = get_connection(config["connection_details"])
|
2022-07-21 11:56:41 +02:00
|
|
|
|
2022-08-22 13:45:46 +02:00
|
|
|
measure_queries(config["queries_to_measure"], connection)
|
|
|
|
|
|
|
|
|
|
print("Finished the measuring session.")
|
|
|
|
|
|
|
|
|
|
clean_up_connection(config["connection_details"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def measure_queries(
|
|
|
|
|
queries_config: dict,
|
|
|
|
|
connection: Union[trino.dbapi.Connection, mysql.connector.MySQLConnection],
|
|
|
|
|
) -> None:
|
|
|
|
|
"""
|
|
|
|
|
Measure several queries through a connection.
|
|
|
|
|
|
|
|
|
|
:param queries_config: the configuration for the queries to measure.
|
|
|
|
|
:param connection: the connection to the queriable server.
|
|
|
|
|
:return: None
|
|
|
|
|
"""
|
|
|
|
|
for query_config in queries_config:
|
2022-07-21 11:56:41 +02:00
|
|
|
try:
|
|
|
|
|
query = TestableQuery(
|
|
|
|
|
name=query_config["name"], query_string=query_config["query_string"]
|
|
|
|
|
)
|
|
|
|
|
measure_query_runtime(connection, query)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
print(f"""Something went wrong with query {query_config["name"]}.""")
|
|
|
|
|
print(f"{traceback.format_exc()}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestableQuery:
|
2022-07-21 12:24:35 +02:00
|
|
|
"""
|
|
|
|
|
Simple object to hold the details of a query that will be measured.
|
|
|
|
|
"""
|
|
|
|
|
|
2022-07-21 11:56:41 +02:00
|
|
|
def __init__(self, name: str, query_string: str):
|
|
|
|
|
self.name = name
|
|
|
|
|
self.query_string = query_string
|
|
|
|
|
|
|
|
|
|
|
2022-07-21 12:24:35 +02:00
|
|
|
def measure_query_runtime(
|
|
|
|
|
connection: Union[trino.dbapi.Connection, mysql.connector.MySQLConnection],
|
|
|
|
|
query_to_measure: TestableQuery,
|
|
|
|
|
) -> None:
|
|
|
|
|
"""
|
|
|
|
|
Execute a query against the given connection and print the time it took.
|
|
|
|
|
|
|
|
|
|
:param connection: a connection object capable of generating cursors.
|
|
|
|
|
:param query_to_measure: the query that will be measured.
|
|
|
|
|
:return: None
|
|
|
|
|
"""
|
2022-07-21 11:56:41 +02:00
|
|
|
start_time = time.time()
|
|
|
|
|
cur = connection.cursor()
|
2022-07-21 12:24:35 +02:00
|
|
|
cur.execute(query_to_measure.query_string)
|
2022-07-21 11:56:41 +02:00
|
|
|
rows = cur.fetchall()
|
2022-07-26 14:13:39 +02:00
|
|
|
obtained_rows = len(rows)
|
2022-07-21 12:24:35 +02:00
|
|
|
print(
|
2022-07-26 14:13:39 +02:00
|
|
|
f"Query '{query_to_measure.name}' took {int(time.time() - start_time)} seconds to run and returned {obtained_rows} rows."
|
2022-07-21 11:56:41 +02:00
|
|
|
)
|