1
- from contextlib import nullcontext
1
+ from contextlib import ExitStack
2
2
import os
3
3
import os .path
4
4
import requests
@@ -36,43 +36,30 @@ def bench_kinto(loops=5000):
36
36
37
37
38
38
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
76
63
77
64
78
65
#############################
@@ -85,13 +72,26 @@ def _bench_kinto(loops=5000, legacy=False):
85
72
if NGINX is None :
86
73
raise Exception ("nginx is not installed" )
87
74
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 ))
93
94
94
- with context :
95
95
runner = pyperf .Runner ()
96
96
runner .metadata ['description' ] = "Test the performance of kinto"
97
97
runner .bench_time_func ("kinto" , bench_kinto )
0 commit comments