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, \
|
2016-12-21 13:17:53 +01:00
|
|
|
Mapping, Sequence, Text
|
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-12-21 13:17:53 +01:00
|
|
|
ParamsT = Union[Iterable[Any], Mapping[Text, 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-12-21 13:17:53 +01:00
|
|
|
# type: (Sequence[Any], Mapping[Text, 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)
|
|
|
|
|
2016-10-14 09:36:27 +02:00
|
|
|
def cursor(self, *args, **kwargs):
|
|
|
|
# type: (*Any, **Any) -> TimeTrackingCursor
|
|
|
|
kwargs.setdefault('cursor_factory', TimeTrackingCursor)
|
|
|
|
return connection.cursor(self, *args, **kwargs)
|
2014-01-07 22:20:29 +01:00
|
|
|
|
|
|
|
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 = []
|