2014-01-07 22:20:29 +01:00
|
|
|
from __future__ import absolute_import
|
|
|
|
|
|
|
|
import time
|
|
|
|
from psycopg2.extensions import cursor, connection
|
|
|
|
|
2016-06-05 04:20:00 +02:00
|
|
|
from typing import Callable, Optional, Iterable, Any, Dict, Union, TypeVar, \
|
|
|
|
Mapping, Sequence
|
|
|
|
from six import text_type
|
2016-06-28 07:10:38 +02:00
|
|
|
from zerver.lib.str_utils import NonBinaryStr
|
2016-06-05 04:20:00 +02:00
|
|
|
|
|
|
|
CursorObj = TypeVar('CursorObj', bound=cursor)
|
2016-06-27 21:34:51 +02:00
|
|
|
ParamsT = Union[Iterable[Any], Mapping[text_type, Any]]
|
2016-06-05 04:20:00 +02:00
|
|
|
|
2014-01-07 22:20:29 +01:00
|
|
|
# Similar to the tracking done in Django's CursorDebugWrapper, but done at the
|
|
|
|
# psycopg2 cursor level so it works with SQLAlchemy.
|
|
|
|
def wrapper_execute(self, action, sql, params=()):
|
2016-06-28 07:10:38 +02:00
|
|
|
# type: (CursorObj, Callable[[NonBinaryStr, Optional[ParamsT]], CursorObj], NonBinaryStr, ParamsT) -> CursorObj
|
2014-01-07 22:20:29 +01:00
|
|
|
start = time.time()
|
|
|
|
try:
|
|
|
|
return action(sql, params)
|
|
|
|
finally:
|
|
|
|
stop = time.time()
|
|
|
|
duration = stop - start
|
|
|
|
self.connection.queries.append({
|
|
|
|
'time': "%.3f" % duration,
|
|
|
|
})
|
|
|
|
|
|
|
|
class TimeTrackingCursor(cursor):
|
|
|
|
"""A psycopg2 cursor class that tracks the time spent executing queries."""
|
|
|
|
|
|
|
|
def execute(self, query, vars=None):
|
2016-06-28 07:10:38 +02:00
|
|
|
# type: (NonBinaryStr, Optional[ParamsT]) -> TimeTrackingCursor
|
2014-01-07 22:20:29 +01:00
|
|
|
return wrapper_execute(self, super(TimeTrackingCursor, self).execute, query, vars)
|
|
|
|
|
|
|
|
def executemany(self, query, vars):
|
2016-06-28 07:10:38 +02:00
|
|
|
# type: (NonBinaryStr, Iterable[Any]) -> TimeTrackingCursor
|
2014-01-07 22:20:29 +01:00
|
|
|
return wrapper_execute(self, super(TimeTrackingCursor, self).executemany, query, vars)
|
|
|
|
|
|
|
|
class TimeTrackingConnection(connection):
|
|
|
|
"""A psycopg2 connection class that uses TimeTrackingCursors."""
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
2016-06-05 04:20:00 +02:00
|
|
|
# type: (Sequence[Any], Mapping[text_type, Any]) -> None
|
2016-01-25 23:42:16 +01:00
|
|
|
self.queries = [] # type: List[Dict[str, str]]
|
2014-01-07 22:20:29 +01:00
|
|
|
super(TimeTrackingConnection, self).__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
def cursor(self, name=None):
|
2016-06-05 04:20:00 +02:00
|
|
|
# type: (Optional[text_type]) -> TimeTrackingCursor
|
2014-01-07 22:20:29 +01:00
|
|
|
if name is None:
|
|
|
|
return super(TimeTrackingConnection, self).cursor(cursor_factory=TimeTrackingCursor)
|
|
|
|
else:
|
|
|
|
return super(TimeTrackingConnection, self).cursor(name, cursor_factory=TimeTrackingCursor)
|
|
|
|
|
|
|
|
def reset_queries():
|
2016-06-05 04:20:00 +02:00
|
|
|
# type: () -> None
|
2014-01-07 22:20:29 +01:00
|
|
|
from django.db import connections
|
|
|
|
for conn in connections.all():
|
2015-11-24 07:01:35 +01:00
|
|
|
if conn.connection is not None:
|
|
|
|
conn.connection.queries = []
|