Skip to content

Commit

Permalink
Replace time.time() with time.perf_counter() (#1777)
Browse files Browse the repository at this point in the history
time.perf_counter() is guaranteed to use the highest-resolution clock
available, and is monotonically increasing, neither of which are true
for time.time().  Thanks @matthiask for the suggestion!
  • Loading branch information
living180 committed May 15, 2023
1 parent e7575e8 commit 43a87f7
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 12 deletions.
6 changes: 3 additions & 3 deletions debug_toolbar/management/commands/debugsqlshell.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from time import time
from time import perf_counter

import sqlparse
from django.core.management.commands.shell import Command
Expand All @@ -19,12 +19,12 @@

class PrintQueryWrapper(base_module.CursorDebugWrapper):
def execute(self, sql, params=()):
start_time = time()
start_time = perf_counter()
try:
return self.cursor.execute(sql, params)
finally:
raw_sql = self.db.ops.last_executed_query(self.cursor, sql, params)
end_time = time()
end_time = perf_counter()
duration = (end_time - start_time) * 1000
formatted_sql = sqlparse.format(raw_sql, reindent=True)
print(f"{formatted_sql} [{duration:.2f}ms]")
Expand Down
6 changes: 3 additions & 3 deletions debug_toolbar/panels/cache.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import functools
import time
from time import perf_counter

from asgiref.local import Local
from django.conf import settings
Expand Down Expand Up @@ -148,9 +148,9 @@ def _record_call(self, cache, name, original_method, args, kwargs):
# the course of this call, and then reset it back afterward.
cache._djdt_panel = None
try:
t = time.time()
start_time = perf_counter()
value = original_method(*args, **kwargs)
t = time.time() - t
t = perf_counter() - start_time
finally:
cache._djdt_panel = self

Expand Down
6 changes: 3 additions & 3 deletions debug_toolbar/panels/sql/tracking.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import contextvars
import datetime
import json
from time import time
from time import perf_counter

import django.test.testcases
from django.db.backends.utils import CursorWrapper
Expand Down Expand Up @@ -162,11 +162,11 @@ def _record(self, method, sql, params):
conn = self.db.connection
initial_conn_status = conn.info.transaction_status

start_time = time()
start_time = perf_counter()
try:
return method(sql, params)
finally:
stop_time = time()
stop_time = perf_counter()
duration = (stop_time - start_time) * 1000
_params = ""
try:
Expand Down
6 changes: 3 additions & 3 deletions debug_toolbar/panels/timer.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import time
from time import perf_counter

from django.template.loader import render_to_string
from django.templatetags.static import static
Expand Down Expand Up @@ -59,15 +59,15 @@ def scripts(self):
return scripts

def process_request(self, request):
self._start_time = time.time()
self._start_time = perf_counter()
if self.has_content:
self._start_rusage = resource.getrusage(resource.RUSAGE_SELF)
return super().process_request(request)

def generate_stats(self, request, response):
stats = {}
if hasattr(self, "_start_time"):
stats["total_time"] = (time.time() - self._start_time) * 1000
stats["total_time"] = (perf_counter() - self._start_time) * 1000
if hasattr(self, "_start_rusage"):
self._end_rusage = resource.getrusage(resource.RUSAGE_SELF)
stats["utime"] = 1000 * self._elapsed_ru("ru_utime")
Expand Down
2 changes: 2 additions & 0 deletions docs/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ Pending
* Reworked the cache panel instrumentation code to no longer attempt to undo
monkey patching of cache methods, as that turned out to be fragile in the
presence of other code which also monkey patches those methods.
* Update all timing code that used :py:func:`time.time()` to use
:py:func:`time.perf_counter()` instead.

4.0.0 (2023-04-03)
------------------
Expand Down

0 comments on commit 43a87f7

Please sign in to comment.