Use time.perf_counter() instead of time.time(), closes #1157

This commit is contained in:
Simon Willison 2020-12-21 13:49:14 -08:00
commit 810853c5f2
3 changed files with 8 additions and 8 deletions

View file

@ -37,9 +37,9 @@ def trace(type, **kwargs):
if tracer is None: if tracer is None:
yield yield
return return
start = time.time() start = time.perf_counter()
yield yield
end = time.time() end = time.perf_counter()
trace_info = { trace_info = {
"type": type, "type": type,
"start": start, "start": start,
@ -74,7 +74,7 @@ class AsgiTracer:
if b"_trace=1" not in scope.get("query_string", b"").split(b"&"): if b"_trace=1" not in scope.get("query_string", b"").split(b"&"):
await self.app(scope, receive, send) await self.app(scope, receive, send)
return return
trace_start = time.time() trace_start = time.perf_counter()
traces = [] traces = []
accumulated_body = b"" accumulated_body = b""
@ -109,7 +109,7 @@ class AsgiTracer:
# We have all the body - modify it and send the result # We have all the body - modify it and send the result
# TODO: What to do about Content-Type or other cases? # TODO: What to do about Content-Type or other cases?
trace_info = { trace_info = {
"request_duration_ms": 1000 * (time.time() - trace_start), "request_duration_ms": 1000 * (time.perf_counter() - trace_start),
"sum_trace_duration_ms": sum(t["duration_ms"] for t in traces), "sum_trace_duration_ms": sum(t["duration_ms"] for t in traces),
"num_traces": len(traces), "num_traces": len(traces),
"traces": traces, "traces": traces,

View file

@ -138,7 +138,7 @@ class CustomJSONEncoder(json.JSONEncoder):
@contextmanager @contextmanager
def sqlite_timelimit(conn, ms): def sqlite_timelimit(conn, ms):
deadline = time.time() + (ms / 1000) deadline = time.perf_counter() + (ms / 1000)
# n is the number of SQLite virtual machine instructions that will be # n is the number of SQLite virtual machine instructions that will be
# executed between each check. It's hard to know what to pick here. # executed between each check. It's hard to know what to pick here.
# After some experimentation, I've decided to go with 1000 by default and # After some experimentation, I've decided to go with 1000 by default and
@ -148,7 +148,7 @@ def sqlite_timelimit(conn, ms):
n = 1 n = 1
def handler(): def handler():
if time.time() >= deadline: if time.perf_counter() >= deadline:
return 1 return 1
conn.set_progress_handler(handler, n) conn.set_progress_handler(handler, n)

View file

@ -425,7 +425,7 @@ class DataView(BaseView):
kwargs["default_labels"] = True kwargs["default_labels"] = True
extra_template_data = {} extra_template_data = {}
start = time.time() start = time.perf_counter()
status_code = 200 status_code = 200
templates = [] templates = []
try: try:
@ -457,7 +457,7 @@ class DataView(BaseView):
except DatasetteError: except DatasetteError:
raise raise
end = time.time() end = time.perf_counter()
data["query_ms"] = (end - start) * 1000 data["query_ms"] = (end - start) * 1000
for key in ("source", "source_url", "license", "license_url"): for key in ("source", "source_url", "license", "license_url"):
value = self.ds.metadata(key) value = self.ds.metadata(key)