| 1 | | """Benchmark tool for CherryPy.""" |
|---|
| 2 | | |
|---|
| | 1 | """CherryPy Benchmark Tool |
|---|
| | 2 | |
|---|
| | 3 | Usage: |
|---|
| | 4 | benchmark.py --null --notests --help --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 don't run the tests; this allows |
|---|
| | 8 | you to check the tested pages with a browser |
|---|
| | 9 | --help: show this help message |
|---|
| | 10 | --modpython: start up apache on 8080 (with a custom modpython |
|---|
| | 11 | config) and run the tests |
|---|
| | 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 |
|---|
| | 81 | class NullRequest: |
|---|
| | 82 | """A null HTTP request class, returning 204 and an empty body.""" |
|---|
| | 83 | |
|---|
| | 84 | def __init__(self, remoteAddr, remotePort, remoteHost, scheme="http"): |
|---|
| | 85 | pass |
|---|
| | 86 | |
|---|
| | 87 | def close(self): |
|---|
| | 88 | pass |
|---|
| | 89 | |
|---|
| | 90 | def run(self, requestLine, headers, rfile): |
|---|
| | 91 | cherrypy.response.status = "204 No Content" |
|---|
| | 92 | cherrypy.response.header_list = [("Content-Type", 'text/html'), |
|---|
| | 93 | ("Server", "Null CherryPy"), |
|---|
| | 94 | ("Date", httptools.HTTPDate()), |
|---|
| | 95 | ("Content-Length", "0"), |
|---|
| | 96 | ] |
|---|
| | 97 | cherrypy.response.body = [""] |
|---|
| | 98 | return cherrypy.response |
|---|
| | 99 | |
|---|
| | 100 | |
|---|
| | 101 | class NullResponse: |
|---|
| | 102 | pass |
|---|
| | 103 | |
|---|
| | 104 | |
|---|
| 222 | | class NullRequest: |
|---|
| 223 | | """A null HTTP request class, returning 204 and an empty body.""" |
|---|
| 224 | | |
|---|
| 225 | | def __init__(self, remoteAddr, remotePort, remoteHost, scheme="http"): |
|---|
| 226 | | pass |
|---|
| 227 | | |
|---|
| 228 | | def close(self): |
|---|
| 229 | | pass |
|---|
| 230 | | |
|---|
| 231 | | def run(self, requestLine, headers, rfile): |
|---|
| 232 | | cherrypy.response.status = "204 No Content" |
|---|
| 233 | | cherrypy.response.header_list = [("Content-Type", 'text/html'), |
|---|
| 234 | | ("Server", "Null CherryPy"), |
|---|
| 235 | | ("Date", httptools.HTTPDate()), |
|---|
| 236 | | ("Content-Length", "0"), |
|---|
| 237 | | ] |
|---|
| 238 | | cherrypy.response.body = [""] |
|---|
| 239 | | return cherrypy.response |
|---|
| 240 | | |
|---|
| 241 | | |
|---|
| 242 | | class NullResponse: |
|---|
| 243 | | pass |
|---|
| 244 | | |
|---|
| 245 | | |
|---|
| 263 | | return 0 # apache.OK |
|---|
| | 296 | |
|---|
| | 297 | import modpython_gateway |
|---|
| | 298 | return modpython_gateway.handler(req) |
|---|
| | 299 | |
|---|
| | 300 | mp_conf_template = """ |
|---|
| | 301 | # Apache2 server configuration file for benchmarking CherryPy with mod_python. |
|---|
| | 302 | |
|---|
| | 303 | DocumentRoot "/" |
|---|
| | 304 | Listen 8080 |
|---|
| | 305 | LoadModule python_module modules/mod_python.so |
|---|
| | 306 | |
|---|
| | 307 | <Location /> |
|---|
| | 308 | SetHandler python-program |
|---|
| | 309 | PythonHandler cherrypy.test.benchmark::startup_modpython |
|---|
| | 310 | PythonOption application cherrypy._cpwsgi::wsgiApp |
|---|
| | 311 | PythonDebug On |
|---|
| | 312 | %s%s |
|---|
| | 313 | </Location> |
|---|
| | 314 | """ |
|---|
| | 315 | |
|---|
| | 316 | def run_modpython(): |
|---|
| | 317 | # Pass the null and ab=path options through Apache |
|---|
| | 318 | nullreq_opt = "" |
|---|
| | 319 | if "--null" in opts: |
|---|
| | 320 | nullreq_opt = " PythonOption nullreq\n" |
|---|
| | 321 | |
|---|
| | 322 | ab_opt = "" |
|---|
| | 323 | if "--ab" in opts: |
|---|
| | 324 | ab_opt = " PythonOption ab %s\n" % opts["--ab"] |
|---|
| | 325 | |
|---|
| | 326 | conf_data = mp_conf_template % (ab_opt, nullreq_opt) |
|---|
| | 327 | mpconf = os.path.join(curdir, "bench_mp.conf") |
|---|
| | 328 | |
|---|
| | 329 | f = open(mpconf, 'wb') |
|---|
| | 330 | try: |
|---|
| | 331 | f.write(conf_data) |
|---|
| | 332 | finally: |
|---|
| | 333 | f.close() |
|---|
| | 334 | |
|---|
| | 335 | apargs = "-k start -f %s" % mpconf |
|---|
| | 336 | try: |
|---|
| | 337 | read_process(APACHE_PATH or "apache", apargs) |
|---|
| | 338 | run() |
|---|
| | 339 | finally: |
|---|
| | 340 | os.popen("apache -k stop") |
|---|
| | 341 | |
|---|