1 """CherryPy Benchmark Tool
2
3 Usage:
4 benchmark.py --null --notests --help --cpmodpy --modpython --ab=path --apache=path
5
6 --null: use a null Request object (to bench the HTTP server only)
7 --notests: start the server but do not run the tests; this allows
8 you to check the tested pages with a browser
9 --help: show this help message
10 --cpmodpy: run tests via apache on 54583 (with the builtin _cpmodpy)
11 --modpython: run tests via apache on 54583 (with modpython_gateway)
12 --ab=path: Use the ab script/executable at 'path' (see below)
13 --apache=path: Use the apache script/exe at 'path' (see below)
14
15 To run the benchmarks, the Apache Benchmark tool "ab" must either be on
16 your system path, or specified via the --ab=path option.
17
18 To run the modpython tests, the "apache" executable or script must be
19 on your system path, or provided via the --apache=path option. On some
20 platforms, "apache" may be called "apachectl" or "apache2ctl"--create
21 a symlink to them if needed.
22 """
23
24 import getopt
25 import os
26 curdir = os.path.join(os.getcwd(), os.path.dirname(__file__))
27
28 import re
29 import sys
30 import time
31 import traceback
32
33 import cherrypy
34 from cherrypy._cpcompat import ntob
35 from cherrypy import _cperror, _cpmodpy
36 from cherrypy.lib import httputil
37
38
39 AB_PATH = ""
40 APACHE_PATH = "apache"
41 SCRIPT_NAME = "/cpbench/users/rdelon/apps/blog"
42
43 __all__ = ['ABSession', 'Root', 'print_report',
44 'run_standard_benchmarks', 'safe_threads',
45 'size_report', 'startup', 'thread_report',
46 ]
47
48 size_cache = {}
49
51
53 return """<html>
54 <head>
55 <title>CherryPy Benchmark</title>
56 </head>
57 <body>
58 <ul>
59 <li><a href="hello">Hello, world! (14 byte dynamic)</a></li>
60 <li><a href="static/index.html">Static file (14 bytes static)</a></li>
61 <li><form action="sizer">Response of length:
62 <input type='text' name='size' value='10' /></form>
63 </li>
64 </ul>
65 </body>
66 </html>"""
67 index.exposed = True
68
70 return "Hello, world\r\n"
71 hello.exposed = True
72
74 resp = size_cache.get(size, None)
75 if resp is None:
76 size_cache[size] = resp = "X" * int(size)
77 return resp
78 sizer.exposed = True
79
80
81 cherrypy.config.update({
82 'log.error.file': '',
83 'environment': 'production',
84 'server.socket_host': '127.0.0.1',
85 'server.socket_port': 54583,
86 'server.max_request_header_size': 0,
87 'server.max_request_body_size': 0,
88 'engine.deadlock_poll_freq': 0,
89 })
90
91
92 del cherrypy.config['tools.log_tracebacks.on']
93 del cherrypy.config['tools.log_headers.on']
94 del cherrypy.config['tools.trailing_slash.on']
95
96 appconf = {
97 '/static': {
98 'tools.staticdir.on': True,
99 'tools.staticdir.dir': 'static',
100 'tools.staticdir.root': curdir,
101 },
102 }
103 app = cherrypy.tree.mount(Root(), SCRIPT_NAME, appconf)
104
105
107 """A null HTTP request class, returning 200 and an empty body."""
108
109 - def __init__(self, local, remote, scheme="http"):
111
114
115 - def run(self, method, path, query_string, protocol, headers, rfile):
124
125
128
129
131 """A session of 'ab', the Apache HTTP server benchmarking tool.
132
133 Example output from ab:
134
135 This is ApacheBench, Version 2.0.40-dev <$Revision: 1.121.2.1 $> apache-2.0
136 Copyright (c) 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
137 Copyright (c) 1998-2002 The Apache Software Foundation, http://www.apache.org/
138
139 Benchmarking 127.0.0.1 (be patient)
140 Completed 100 requests
141 Completed 200 requests
142 Completed 300 requests
143 Completed 400 requests
144 Completed 500 requests
145 Completed 600 requests
146 Completed 700 requests
147 Completed 800 requests
148 Completed 900 requests
149
150
151 Server Software: CherryPy/3.1beta
152 Server Hostname: 127.0.0.1
153 Server Port: 54583
154
155 Document Path: /static/index.html
156 Document Length: 14 bytes
157
158 Concurrency Level: 10
159 Time taken for tests: 9.643867 seconds
160 Complete requests: 1000
161 Failed requests: 0
162 Write errors: 0
163 Total transferred: 189000 bytes
164 HTML transferred: 14000 bytes
165 Requests per second: 103.69 [#/sec] (mean)
166 Time per request: 96.439 [ms] (mean)
167 Time per request: 9.644 [ms] (mean, across all concurrent requests)
168 Transfer rate: 19.08 [Kbytes/sec] received
169
170 Connection Times (ms)
171 min mean[+/-sd] median max
172 Connect: 0 0 2.9 0 10
173 Processing: 20 94 7.3 90 130
174 Waiting: 0 43 28.1 40 100
175 Total: 20 95 7.3 100 130
176
177 Percentage of the requests served within a certain time (ms)
178 50% 100
179 66% 100
180 75% 100
181 80% 100
182 90% 100
183 95% 100
184 98% 100
185 99% 110
186 100% 130 (longest request)
187 Finished 1000 requests
188 """
189
190 parse_patterns = [('complete_requests', 'Completed',
191 ntob(r'^Complete requests:\s*(\d+)')),
192 ('failed_requests', 'Failed',
193 ntob(r'^Failed requests:\s*(\d+)')),
194 ('requests_per_second', 'req/sec',
195 ntob(r'^Requests per second:\s*([0-9.]+)')),
196 ('time_per_request_concurrent', 'msec/req',
197 ntob(r'^Time per request:\s*([0-9.]+).*concurrent requests\)$')),
198 ('transfer_rate', 'KB/sec',
199 ntob(r'^Transfer rate:\s*([0-9.]+)')),
200 ]
201
203 self.path = path
204 self.requests = requests
205 self.concurrency = concurrency
206
208 port = cherrypy.server.socket_port
209 assert self.concurrency > 0
210 assert self.requests > 0
211
212
213 return ("-k -n %s -c %s http://127.0.0.1:%s%s" %
214 (self.requests, self.concurrency, port, self.path))
215
231
232
233 safe_threads = (25, 50, 100, 200, 400)
234 if sys.platform in ("win32",):
235
236 safe_threads = (10, 20, 30, 40, 50)
237
238
240 sess = ABSession(path)
241 attrs, names, patterns = list(zip(*sess.parse_patterns))
242 avg = dict.fromkeys(attrs, 0.0)
243
244 yield ('threads',) + names
245 for c in concurrency:
246 sess.concurrency = c
247 sess.run()
248 row = [c]
249 for attr in attrs:
250 val = getattr(sess, attr)
251 if val is None:
252 print(sess.output)
253 row = None
254 break
255 val = float(val)
256 avg[attr] += float(val)
257 row.append(val)
258 if row:
259 yield row
260
261
262 yield ["Average"] + [str(avg[attr] / len(concurrency)) for attr in attrs]
263
264 -def size_report(sizes=(10, 100, 1000, 10000, 100000, 100000000),
265 concurrency=50):
266 sess = ABSession(concurrency=concurrency)
267 attrs, names, patterns = list(zip(*sess.parse_patterns))
268 yield ('bytes',) + names
269 for sz in sizes:
270 sess.path = "%s/sizer?size=%s" % (SCRIPT_NAME, sz)
271 sess.run()
272 yield [sz] + [getattr(sess, attr) for attr in attrs]
273
275 for row in rows:
276 print("")
277 for i, val in enumerate(row):
278 sys.stdout.write(str(val).rjust(10) + " | ")
279 print("")
280
281
297
298
299
300
316
317
319 print("Starting mod_python...")
320 pyopts = []
321
322
323 if "--null" in opts:
324 pyopts.append(("nullreq", ""))
325
326 if "--ab" in opts:
327 pyopts.append(("ab", opts["--ab"]))
328
329 s = _cpmodpy.ModPythonServer
330 if use_wsgi:
331 pyopts.append(("wsgi.application", "cherrypy::tree"))
332 pyopts.append(("wsgi.startup", "cherrypy.test.benchmark::startup_modpython"))
333 handler = "modpython_gateway::handler"
334 s = s(port=54583, opts=pyopts, apache_path=APACHE_PATH, handler=handler)
335 else:
336 pyopts.append(("cherrypy.setup", "cherrypy.test.benchmark::startup_modpython"))
337 s = s(port=54583, opts=pyopts, apache_path=APACHE_PATH)
338
339 try:
340 s.start()
341 run()
342 finally:
343 s.stop()
344
345
346
347 if __name__ == '__main__':
348 longopts = ['cpmodpy', 'modpython', 'null', 'notests',
349 'help', 'ab=', 'apache=']
350 try:
351 switches, args = getopt.getopt(sys.argv[1:], "", longopts)
352 opts = dict(switches)
353 except getopt.GetoptError:
354 print(__doc__)
355 sys.exit(2)
356
357 if "--help" in opts:
358 print(__doc__)
359 sys.exit(0)
360
361 if "--ab" in opts:
362 AB_PATH = opts['--ab']
363
364 if "--notests" in opts:
365
366
374 else:
388
389 print("Starting CherryPy app server...")
390
392 """Suppresses the printing of socket errors."""
395 sys.stderr = NullWriter()
396
397 start = time.time()
398
399 if "--cpmodpy" in opts:
400 run_modpython()
401 elif "--modpython" in opts:
402 run_modpython(use_wsgi=True)
403 else:
404 if "--null" in opts:
405 cherrypy.server.request_class = NullRequest
406 cherrypy.server.response_class = NullResponse
407
408 cherrypy.engine.start_with_callback(run)
409 cherrypy.engine.block()
410