Skip to content

Commit 31431c9

Browse files
committed
test: Use time.perf_counter instead of time.time for performance tests
On windows, time.time has low resolution (about 15ms), which makes performance tests flaky. time.perf_counter has much higher resolution and is the recommended way to measure elapsed time.
1 parent 07b5924 commit 31431c9

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

tornado/test/httputil_test.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ def test_disposition_param_linear_performance(self):
284284
# to the content-disposition header, specifically for semicolons within
285285
# quoted strings.
286286
def f(n):
287-
start = time.time()
287+
start = time.perf_counter()
288288
message = (
289289
b"--1234\r\nContent-Disposition: form-data; "
290290
+ b'x="'
@@ -295,7 +295,7 @@ def f(n):
295295
args: dict[str, list[bytes]] = {}
296296
files: dict[str, list[HTTPFile]] = {}
297297
parse_multipart_form_data(b"1234", message, args, files)
298-
return time.time() - start
298+
return time.perf_counter() - start
299299

300300
d1 = f(1_000)
301301
d2 = f(10_000)
@@ -496,11 +496,11 @@ def test_invalid_header_names(self):
496496

497497
def test_linear_performance(self):
498498
def f(n):
499-
start = time.time()
499+
start = time.perf_counter()
500500
headers = HTTPHeaders()
501501
for i in range(n):
502502
headers.add("X-Foo", "bar")
503-
return time.time() - start
503+
return time.perf_counter() - start
504504

505505
# This runs under 50ms on my laptop as of 2025-12-09.
506506
d1 = f(10_000)

0 commit comments

Comments
 (0)