Skip to content

Commit 4b281c2

Browse files
committed
Migrate aiohttp and gunicorn to single-webserver style
1 parent c7dbe45 commit 4b281c2

File tree

2 files changed

+55
-40
lines changed

2 files changed

+55
-40
lines changed

benchmarks/bm_aiohttp/run_benchmark.py

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from contextlib import nullcontext
12
import os.path
23
import requests
34
import sys
@@ -38,23 +39,24 @@ def _bench_aiohttp_requests(loops=3000, legacy=False):
3839
start = pyperf.perf_counter()
3940
elapsed = 0
4041
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)
5860
return elapsed, times
5961

6062

@@ -65,6 +67,12 @@ def _bench_aiohttp_requests(loops=3000, legacy=False):
6567
from legacyutils import maybe_handle_legacy
6668
maybe_handle_legacy(_bench_aiohttp_requests, legacyarg='legacy')
6769

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)

benchmarks/bm_gunicorn/run_benchmark.py

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from contextlib import nullcontext
12
import os.path
23
import requests
34
import sys
@@ -48,23 +49,23 @@ def _bench_gunicorn(loops=3000, legacy=False):
4849
start = pyperf.perf_counter()
4950
elapsed = 0
5051
times = []
51-
with netutils.serving(ARGV, DATADIR, ADDR):
52-
requests_get = requests.get
53-
for i in range(loops):
54-
# This is a macro benchmark for a Python implementation
55-
# so "elapsed" covers more than just how long a request takes.
56-
t0 = pyperf.perf_counter()
57-
requests_get("http://localhost:8000/blog/").text
58-
t1 = pyperf.perf_counter()
59-
60-
elapsed += t1 - t0
61-
times.append(t0)
62-
if legacy and (i % 100 == 0):
63-
print(i, t0 - start)
64-
times.append(pyperf.perf_counter())
65-
if legacy:
66-
total = times[-1] - start
67-
print("%.2fs (%.3freq/s)" % (total, loops / total))
52+
53+
requests_get = requests.get
54+
for i in range(loops):
55+
# This is a macro benchmark for a Python implementation
56+
# so "elapsed" covers more than just how long a request takes.
57+
t0 = pyperf.perf_counter()
58+
requests_get("http://localhost:8000/blog/").text
59+
t1 = pyperf.perf_counter()
60+
61+
elapsed += t1 - t0
62+
times.append(t0)
63+
if legacy and (i % 100 == 0):
64+
print(i, t0 - start)
65+
times.append(pyperf.perf_counter())
66+
if legacy:
67+
total = times[-1] - start
68+
print("%.2fs (%.3freq/s)" % (total, loops / total))
6869
return elapsed, times
6970

7071

@@ -75,6 +76,12 @@ def _bench_gunicorn(loops=3000, legacy=False):
7576
from legacyutils import maybe_handle_legacy
7677
maybe_handle_legacy(_bench_gunicorn, legacyarg='legacy')
7778

78-
runner = pyperf.Runner()
79-
runner.metadata['description'] = "Test the performance of gunicorn"
80-
runner.bench_time_func("gunicorn", bench_gunicorn)
79+
if "--worker" not in sys.argv:
80+
context = netutils.serving(ARGV, DATADIR, ADDR)
81+
else:
82+
context = nullcontext()
83+
84+
with context:
85+
runner = pyperf.Runner()
86+
runner.metadata['description'] = "Test the performance of gunicorn"
87+
runner.bench_time_func("gunicorn", bench_gunicorn)

0 commit comments

Comments
 (0)