Skip to content

Commit f64a8ea

Browse files
committed
Wrap timing in a context manager
1 parent 8f8671a commit f64a8ea

File tree

2 files changed

+41
-18
lines changed

2 files changed

+41
-18
lines changed

asyncpg/connection.py

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -339,10 +339,9 @@ async def execute(self, query: str, *args, timeout: float=None) -> str:
339339
self._check_open()
340340

341341
if not args:
342-
start = time.monotonic()
343-
result = await self._protocol.query(query, timeout)
344-
elapsed = time.monotonic() - start
345-
self._log_query(query, args, timeout, elapsed)
342+
with utils.timer() as t:
343+
result = await self._protocol.query(query, timeout)
344+
self._log_query(query, args, timeout, t.elapsed)
346345
return result
347346

348347
_, status, _ = await self._execute(
@@ -1724,27 +1723,25 @@ async def __execute(
17241723
executor = lambda stmt, timeout: self._protocol.bind_execute(
17251724
stmt, args, '', limit, return_status, timeout)
17261725
timeout = self._protocol._get_timeout(timeout)
1727-
start = time.monotonic()
1728-
result, stmt = await self._do_execute(
1729-
query,
1730-
executor,
1731-
timeout,
1732-
record_class=record_class,
1733-
ignore_custom_codec=ignore_custom_codec,
1734-
)
1735-
elapsed = time.monotonic() - start
1736-
self._log_query(query, args, timeout, elapsed)
1726+
with utils.timer() as t:
1727+
result, stmt = await self._do_execute(
1728+
query,
1729+
executor,
1730+
timeout,
1731+
record_class=record_class,
1732+
ignore_custom_codec=ignore_custom_codec,
1733+
)
1734+
self._log_query(query, args, timeout, t.elapsed)
17371735
return result, stmt
17381736

17391737
async def _executemany(self, query, args, timeout):
17401738
executor = lambda stmt, timeout: self._protocol.bind_execute_many(
17411739
stmt, args, '', timeout)
17421740
timeout = self._protocol._get_timeout(timeout)
17431741
with self._stmt_exclusive_section:
1744-
start = time.monotonic()
1745-
result, _ = await self._do_execute(query, executor, timeout)
1746-
elapsed = time.monotonic() - start
1747-
self._log_query(query, args, timeout, elapsed)
1742+
with utils.timer() as t:
1743+
result, _ = await self._do_execute(query, executor, timeout)
1744+
self._log_query(query, args, timeout, t.elapsed)
17481745
return result
17491746

17501747
async def _do_execute(

asyncpg/utils.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77

88
import re
9+
import time
910

1011

1112
def _quote_ident(ident):
@@ -43,3 +44,28 @@ async def _mogrify(conn, query, args):
4344
# Finally, replace $n references with text values.
4445
return re.sub(
4546
r'\$(\d+)\b', lambda m: textified[int(m.group(1)) - 1], query)
47+
48+
49+
class timer:
50+
__slots__ = ('start', 'elapsed')
51+
52+
def __init__(self):
53+
self.start = time.monotonic()
54+
self.elapsed = None
55+
56+
@property
57+
def current(self):
58+
return time.monotonic() - self.start
59+
60+
def restart(self):
61+
self.start = time.monotonic()
62+
63+
def stop(self):
64+
self.elapsed = self.current
65+
66+
def __enter__(self):
67+
self.restart()
68+
return self
69+
70+
def __exit__(self, *exc):
71+
self.stop()

0 commit comments

Comments
 (0)