-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark.py
More file actions
58 lines (49 loc) · 1.85 KB
/
benchmark.py
File metadata and controls
58 lines (49 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import time
from sqlalchemy import text
from attrs import define
import pyodbc
from mssql_python.connection import Connection
@define
class PerformanceTester():
"""Container for benchmark function"""
@classmethod
def benchmark_query(cls,
connection: pyodbc.Connection | Connection,
query: str,
driver_name: str,
iterations: int = 5) -> dict[str, str | float | int]:
"""Runs a query multiple times and returns timing statistics
Args:
connection (pyodbc.Connection | Connection): database connection
query (str): select statement
driver_name (str): driver name
iterations (int, optional): number of times the query will be executed. Defaults to 5.
Returns:
dict[str, str | float | int]: results dictionary containing execution times.
"""
times = []
row_counts = []
for i in range(iterations):
start = time.perf_counter()
if driver_name in ('pyodbc', 'pyodbc + SQLAlchemy'):
cursor = connection.execute(text(query))
result = cursor.fetchall()
cursor.close()
else: # mssql-python
cursor = connection.cursor()
cursor.execute(query)
result = cursor.fetchall()
cursor.close()
end = time.perf_counter()
elapsed = end - start
times.append(elapsed)
row_counts.append(len(result))
return {
'driver': driver_name,
'avg_time': sum(times) / len(times),
'min_time': min(times),
'max_time': max(times),
'total_time': sum(times),
'rows': row_counts[0],
'iterations': iterations
}