Skip to content

Commit 2f69455

Browse files
committed
swap monotonic to perf_counter timer
1 parent 9a9134e commit 2f69455

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

src/capture_db_queries/timers.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
from __future__ import annotations
22

3-
import time
43
from typing import TYPE_CHECKING
54

65
from typing_extensions import Self # noqa: UP035
76

87
if TYPE_CHECKING:
8+
from collections.abc import Callable
99
from types import TracebackType
1010

1111

1212
class ContextTimer:
13-
def __init__(self) -> None:
13+
def __init__(self, measure_func: Callable[..., float]) -> None:
14+
self.measure_func = measure_func
1415
self.execution_time = 0.0
1516
self.all_execution_times: list[float] = []
1617
self.exec_times_per_iter: list[float] = []
@@ -24,7 +25,7 @@ def queries_count_per_iter(self) -> int:
2425
return len(self.exec_times_per_iter)
2526

2627
def __enter__(self) -> Self:
27-
self._start = time.monotonic()
28+
self._start = self.measure_func()
2829
return self
2930

3031
def __exit__(
@@ -36,7 +37,7 @@ def __exit__(
3637
if exc_type is not None:
3738
return
3839

39-
self._end = time.monotonic()
40+
self._end = self.measure_func()
4041
self.execution_time = self._end - self._start
4142
self.all_execution_times.append(self.execution_time)
4243
self.exec_times_per_iter.append(self.execution_time)

src/capture_db_queries/wrappers.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import json
4+
import time
45
from typing import TYPE_CHECKING, Any, NamedTuple
56

67
from django.utils.regex_helper import _lazy_re_compile
@@ -19,7 +20,7 @@
1920

2021
class BaseExecutionWrapper:
2122
def __init__(self, queries_log: QueriesLog, *args: Any, **kwargs: Any) -> None:
22-
self.timer = ContextTimer()
23+
self.timer = ContextTimer(time.perf_counter)
2324
self.queries_log = queries_log
2425

2526
def __call__(

0 commit comments

Comments
 (0)