Skip to content

Commit cbf1789

Browse files
committed
kinto: do the rest of the setup work only once as well
1 parent 4b281c2 commit cbf1789

File tree

1 file changed

+44
-44
lines changed

1 file changed

+44
-44
lines changed

benchmarks/bm_kinto/run_benchmark.py

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from contextlib import nullcontext
1+
from contextlib import ExitStack
22
import os
33
import os.path
44
import requests
@@ -36,43 +36,30 @@ def bench_kinto(loops=5000):
3636

3737

3838
def _bench_kinto(loops=5000, legacy=False):
39-
cmd = [PYTHON, SETUP_PY, "develop"]
40-
proc = subprocess.run(
41-
cmd,
42-
cwd=DATADIR,
43-
stdout=subprocess.DEVNULL,
44-
stderr=subprocess.STDOUT,
45-
)
46-
if proc.returncode != 0:
47-
print(f'# running: {" ".join(cmd)} (in {DATADIR})')
48-
subprocess.run(cmd, cwd=DATADIR, check=True)
49-
50-
cmd_web = [NGINX, "-c", NGINX_CONF, "-p", DATADIR]
51-
with netutils.serving(cmd_web, DATADIR, ADDR, pause=0.010, quiet=False):
52-
if legacy:
53-
print(requests.get("http://localhost:8000/v1").text)
54-
# print(requests.put("http://localhost:8000/v1/accounts/testuser", json={"data": {"password": "password1"}}).text)
55-
56-
start = pyperf.perf_counter()
57-
elapsed = 0
58-
times = []
59-
for i in range(loops):
60-
# This is a macro benchmark for a Python implementation
61-
# so "elapsed" covers more than just how long a request takes.
62-
t0 = pyperf.perf_counter()
63-
# requests.get("http://localhost:8000/v1/").text
64-
urllib.request.urlopen("http://localhost:8000/v1/").read()
65-
t1 = pyperf.perf_counter()
66-
67-
elapsed += t1 - t0
68-
times.append(t0)
69-
if legacy and (i % 100 == 0):
70-
print(i, t0 - start)
71-
times.append(pyperf.perf_counter())
72-
if legacy:
73-
total = times[-1] - start
74-
print("%.2fs (%.3freq/s)" % (total, loops / total))
75-
return elapsed, times
39+
if legacy:
40+
print(requests.get("http://localhost:8000/v1").text)
41+
# print(requests.put("http://localhost:8000/v1/accounts/testuser", json={"data": {"password": "password1"}}).text)
42+
43+
start = pyperf.perf_counter()
44+
elapsed = 0
45+
times = []
46+
for i in range(loops):
47+
# This is a macro benchmark for a Python implementation
48+
# so "elapsed" covers more than just how long a request takes.
49+
t0 = pyperf.perf_counter()
50+
# requests.get("http://localhost:8000/v1/").text
51+
urllib.request.urlopen("http://localhost:8000/v1/").read()
52+
t1 = pyperf.perf_counter()
53+
54+
elapsed += t1 - t0
55+
times.append(t0)
56+
if legacy and (i % 100 == 0):
57+
print(i, t0 - start)
58+
times.append(pyperf.perf_counter())
59+
if legacy:
60+
total = times[-1] - start
61+
print("%.2fs (%.3freq/s)" % (total, loops / total))
62+
return elapsed, times
7663

7764

7865
#############################
@@ -85,13 +72,26 @@ def _bench_kinto(loops=5000, legacy=False):
8572
if NGINX is None:
8673
raise Exception("nginx is not installed")
8774

88-
if "--worker" not in sys.argv:
89-
cmd_app = [UWSGI, PRODUCTION_INI]
90-
context = netutils.serving(cmd_app, DATADIR, SOCK, kill=True)
91-
else:
92-
context = nullcontext()
75+
with ExitStack() as stack:
76+
if "--worker" not in sys.argv:
77+
cmd = [PYTHON, SETUP_PY, "develop"]
78+
proc = subprocess.run(
79+
cmd,
80+
cwd=DATADIR,
81+
stdout=subprocess.DEVNULL,
82+
stderr=subprocess.STDOUT,
83+
)
84+
85+
if proc.returncode != 0:
86+
print(f'# running: {" ".join(cmd)} (in {DATADIR})')
87+
subprocess.run(cmd, cwd=DATADIR, check=True)
88+
89+
cmd_app = [UWSGI, PRODUCTION_INI]
90+
stack.enter_context(netutils.serving(cmd_app, DATADIR, SOCK, kill=True))
91+
92+
cmd_web = [NGINX, "-c", NGINX_CONF, "-p", DATADIR]
93+
stack.enter_context(netutils.serving(cmd_web, DATADIR, ADDR, pause=0.010, quiet=False))
9394

94-
with context:
9595
runner = pyperf.Runner()
9696
runner.metadata['description'] = "Test the performance of kinto"
9797
runner.bench_time_func("kinto", bench_kinto)

0 commit comments

Comments
 (0)