1
+ from contextlib import nullcontext
1
2
import os .path
2
3
import requests
3
4
import sys
@@ -38,23 +39,24 @@ def _bench_aiohttp_requests(loops=3000, legacy=False):
38
39
start = pyperf .perf_counter ()
39
40
elapsed = 0
40
41
times = []
41
- with netutils .serving (ARGV , DATADIR , "127.0.0.1:8080" ):
42
- requests_get = requests .get
43
- for i in range (loops ):
44
- # This is a macro benchmark for a Python implementation
45
- # so "elapsed" covers more than just how long a request takes.
46
- t0 = pyperf .perf_counter ()
47
- requests_get ("http://localhost:8080/blog/" ).text
48
- t1 = pyperf .perf_counter ()
49
-
50
- elapsed += t1 - t0
51
- times .append (t0 )
52
- if legacy and (i % 100 == 0 ):
53
- print (i , t0 - start )
54
- times .append (pyperf .perf_counter ())
55
- if legacy :
56
- total = times [- 1 ] - start
57
- print ("%.2fs (%.3freq/s)" % (total , loops / total ))
42
+
43
+ requests_get = requests .get
44
+ for i in range (loops ):
45
+ # This is a macro benchmark for a Python implementation
46
+ # so "elapsed" covers more than just how long a request takes.
47
+ t0 = pyperf .perf_counter ()
48
+ requests_get ("http://localhost:8080/blog/" ).text
49
+ t1 = pyperf .perf_counter ()
50
+
51
+ elapsed += t1 - t0
52
+ times .append (t0 )
53
+ if legacy and (i % 100 == 0 ):
54
+ print (i , t0 - start )
55
+ times .append (pyperf .perf_counter ())
56
+ if legacy :
57
+ total = times [- 1 ] - start
58
+ print ("%.2fs (%.3freq/s)" % (total , loops / total ))
59
+ print (loops , elapsed / loops )
58
60
return elapsed , times
59
61
60
62
@@ -65,6 +67,12 @@ def _bench_aiohttp_requests(loops=3000, legacy=False):
65
67
from legacyutils import maybe_handle_legacy
66
68
maybe_handle_legacy (_bench_aiohttp_requests , legacyarg = 'legacy' )
67
69
68
- runner = pyperf .Runner ()
69
- runner .metadata ['description' ] = "Test the performance of aiohttp"
70
- runner .bench_time_func ("aiohttp" , bench_aiohttp_requests )
70
+ if "--worker" not in sys .argv :
71
+ context = netutils .serving (ARGV , DATADIR , "127.0.0.1:8080" )
72
+ else :
73
+ context = nullcontext ()
74
+
75
+ with context :
76
+ runner = pyperf .Runner ()
77
+ runner .metadata ['description' ] = "Test the performance of aiohttp"
78
+ runner .bench_time_func ("aiohttp" , bench_aiohttp_requests )
0 commit comments