]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
test: Use time.perf_counter instead of time.time for performance tests
authorBen Darnell <ben@bendarnell.com>
Thu, 11 Dec 2025 03:00:03 +0000 (22:00 -0500)
committerBen Darnell <ben@bendarnell.com>
Thu, 11 Dec 2025 03:00:03 +0000 (22:00 -0500)
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.

tornado/test/httputil_test.py

index ed06beaee3d96893dd574cf5df5b6289ea38e97d..7fa528cdcb39e847151ff973084dc7ae7883668d 100644 (file)
@@ -284,7 +284,7 @@ Foo
         # to the content-disposition header, specifically for semicolons within
         # quoted strings.
         def f(n):
-            start = time.time()
+            start = time.perf_counter()
             message = (
                 b"--1234\r\nContent-Disposition: form-data; "
                 + b'x="'
@@ -295,7 +295,7 @@ Foo
             args: dict[str, list[bytes]] = {}
             files: dict[str, list[HTTPFile]] = {}
             parse_multipart_form_data(b"1234", message, args, files)
-            return time.time() - start
+            return time.perf_counter() - start
 
         d1 = f(1_000)
         d2 = f(10_000)
@@ -496,11 +496,11 @@ Foo: even
 
     def test_linear_performance(self):
         def f(n):
-            start = time.time()
+            start = time.perf_counter()
             headers = HTTPHeaders()
             for i in range(n):
                 headers.add("X-Foo", "bar")
-            return time.time() - start
+            return time.perf_counter() - start
 
         # This runs under 50ms on my laptop as of 2025-12-09.
         d1 = f(10_000)