-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathquery_recorder.py
176 lines (139 loc) · 6.37 KB
/
query_recorder.py
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
from collections import defaultdict
from collections.abc import Callable, Iterable
from pprint import saferepr
from django.db import connections
from bx_django_utils.dbperf.cursor import RecordingCursorWrapper
class Logger:
def __init__(self):
self._queries = [] # list of (dbname, metrics) tuples for each query that was run
self._databases = {} # short summary
self._sql_time = 0 # total execution time of all queries in milliseconds
self._num_queries = 0 # total count of queries executed
def record(self, alias, **kwargs):
self._queries.append((alias, kwargs))
if alias not in self._databases:
self._databases[alias] = {
"time_spent": kwargs["duration"],
"num_queries": 1,
}
else:
self._databases[alias]["time_spent"] += kwargs["duration"]
self._databases[alias]["num_queries"] += 1
self._sql_time += kwargs['duration']
self._num_queries += 1
@staticmethod
def _similar_key(query):
return query['raw_sql']
@staticmethod
def _duplicate_key(query):
raw_params = () if query['raw_params'] is None else tuple(query['raw_params'])
# saferepr() avoids problems because of unhashable types
# (e.g. lists) when used as dictionary keys.
return query['raw_sql'], saferepr(raw_params)
def _aggregate(self, results):
aliases = set()
# todo: defaultdicts handle very awkwardly (e.g. with Django Templates),
# is collections.Counter a good replacement?
queries_similar = defaultdict(lambda: defaultdict(int))
queries_duplicated = defaultdict(lambda: defaultdict(int))
num_queries_similar = 0
num_queries_duplicated = 0
# put every query into similar/duplicated dicts to aggregate their execution count
for alias, query in self._queries:
aliases.add(alias)
queries_similar[alias][self._similar_key(query)] += 1
queries_duplicated[alias][self._duplicate_key(query)] += 1
# now that the aggregation is done we must strip all queries in the
# aggregation dicts that were executed only once.
for agg_dict in (queries_similar, queries_duplicated):
for queries in agg_dict.values():
del_keys = []
for query, count in queries.items():
if count == 1:
del_keys.append(query)
for key in del_keys:
del queries[key]
# for convenience, make a total for each aggregation across all databases and queries
for db in queries_similar:
num_queries_similar += len(queries_similar[db])
for db in queries_duplicated:
num_queries_duplicated += len(queries_duplicated[db])
results['queries_similar'] = queries_similar
results['queries_duplicated'] = queries_duplicated
results['num_queries_similar'] = num_queries_similar
results['num_queries_duplicated'] = num_queries_duplicated
def dump(self, aggregate_queries=True):
results = {
'queries': self._queries,
'databases': self._databases,
'sql_time': self._sql_time,
'num_queries': self._num_queries,
}
if aggregate_queries:
self._aggregate(results)
return results
class SQLQueryRecorder:
"""
A context manager that allows recording SQL queries executed during its lifetime.
Results of the recording can be read from the recorder in various formats after
recording has stopped.
In environments where not all databases in settings.DATABASES are available (e.g. unittests)
you should pass the databases you want to observe to the constructor.
Example usage:
with SQLQueryRecorder(query_explain=True) as rec:
func_that_makes_queries()
print(rec.results(aggregate_results=True))
"""
running = None
def __init__(
self,
databases: Iterable[str] | None = None,
collect_stacktrace: Callable | None = None,
query_explain: bool = False, # Capture EXPLAIN SQL information?
):
self.logger = Logger()
self.query_explain = query_explain
if databases:
self.databases = [db for db in connections.all() if db.alias in databases]
else:
self.databases = connections.all()
self.collect_stacktrace = collect_stacktrace
def __enter__(self):
for connection in self.databases:
assert not hasattr(connection, '_recording_cursor'), \
'the connection\'s cursor has not been unwrapped properly in a previous run'
# wrap Django's default cursors in RecordingCursorWrapper
connection._recording_cursor = connection.cursor
connection._recording_chunked_cursor = connection.chunked_cursor
def cursor():
return RecordingCursorWrapper(
connection._recording_cursor(), # noqa:B023
connection, # noqa:B023
self.logger,
collect_stacktrace=self.collect_stacktrace,
query_explain=self.query_explain,
)
def chunked_cursor():
return RecordingCursorWrapper(
connection._recording_chunked_cursor(), # noqa:B023
connection, # noqa:B023
self.logger,
collect_stacktrace=self.collect_stacktrace,
query_explain=self.query_explain,
)
connection.cursor = cursor
connection.chunked_cursor = chunked_cursor
self.running = True
return self
def __exit__(self, exc_type, exc_val, exc_tb):
for connection in self.databases:
assert hasattr(connection, '_recording_cursor'), \
'the connection has already been unwrapped, this should not have happened'
# undo the cursor wrapping so the connection is 'clean' again
del connection._recording_cursor
del connection.cursor
del connection.chunked_cursor
self.running = False
def results(self, **dump_kwargs):
assert not self.running, 'results can only be accessed after recording has stopped'
return self.logger.dump(**dump_kwargs)