This file is indexed.

/usr/share/pyshared/cherrypy/test/benchmark.py is in python-cherrypy 2.3.0-3build1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
"""CherryPy Benchmark Tool

    Usage:
        benchmark.py --null --notests --help --modpython --ab=path --apache=path
    
    --null:        use a null Request object (to bench the HTTP server only)
    --notests:     start the server but don't run the tests; this allows
                   you to check the tested pages with a browser
    --help:        show this help message
    --modpython:   start up apache on 8080 (with a custom modpython
                   config) and run the tests
    --ab=path:     Use the ab script/executable at 'path' (see below)
    --apache=path: Use the apache script/exe at 'path' (see below)
    
    To run the benchmarks, the Apache Benchmark tool "ab" must either be on
    your system path, or specified via the --ab=path option.
    
    To run the modpython tests, the "apache" executable or script must be
    on your system path, or provided via the --apache=path option. On some
    platforms, "apache" may be called "apachectl" or "apache2ctl"--create
    a symlink to them if needed.
"""

import getopt
import os
curdir = os.path.join(os.getcwd(), os.path.dirname(__file__))

import re
import sys
import time
import traceback

import cherrypy
from cherrypy.lib import httptools


AB_PATH = ""
APACHE_PATH = ""
MOUNT_POINT = "/cpbench/users/rdelon/apps/blog"

__all__ = ['ABSession', 'Root', 'print_report', 'read_process',
           'run_standard_benchmarks', 'safe_threads',
           'size_report', 'startup', 'thread_report',
           ]

size_cache = {}

class Root:
    def index(self):
        return "Hello, world\r\n"
    index.exposed = True
    
    def sizer(self, size):
        resp = size_cache.get(size, None)
        if resp is None:
            size_cache[size] = resp = "X" * int(size)
        return resp
    sizer.exposed = True


conf = {
    'global': {
        'server.log_to_screen': False,
##        'server.log_file': os.path.join(curdir, "bench.log"),
        'server.environment': 'production',
        'server.socket_host': 'localhost',
        'server.socket_port': 8080,
        'server.max_request_header_size': 0,
        'server.max_request_body_size': 0,
        },
    '/static': {
        'static_filter.on': True,
        'static_filter.dir': 'static',
        'static_filter.root': curdir,
        },
    }
cherrypy.tree.mount(Root(), MOUNT_POINT, conf)
cherrypy.lowercase_api = True


class NullRequest:
    """A null HTTP request class, returning 204 and an empty body."""
    
    def __init__(self, remoteAddr, remotePort, remoteHost, scheme="http"):
        pass
    
    def close(self):
        pass
    
    def run(self, requestLine, headers, rfile):
        cherrypy.response.status = "204 No Content"
        cherrypy.response.header_list = [("Content-Type", 'text/html'),
                                         ("Server", "Null CherryPy"),
                                         ("Date", httptools.HTTPDate()),
                                         ("Content-Length", "0"),
                                         ]
        cherrypy.response.body = [""]
        return cherrypy.response


class NullResponse:
    pass


def read_process(cmd, args=""):
    pipein, pipeout = os.popen4("%s %s" % (cmd, args))
    try:
        firstline = pipeout.readline()
        if (re.search(r"(not recognized|No such file|not found)", firstline,
                      re.IGNORECASE)):
            raise IOError('%s must be on your system path.' % cmd)
        output = firstline + pipeout.read()
    finally:
        pipeout.close()
    return output


class ABSession:
    """A session of 'ab', the Apache HTTP server benchmarking tool.

Example output from ab:

This is ApacheBench, Version 2.0.40-dev <$Revision: 1.121.2.1 $> apache-2.0
Copyright (c) 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Copyright (c) 1998-2002 The Apache Software Foundation, http://www.apache.org/

Benchmarking localhost (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests


Server Software:        CherryPy/2.2.0beta
Server Hostname:        localhost
Server Port:            8080

Document Path:          /static/index.html
Document Length:        14 bytes

Concurrency Level:      10
Time taken for tests:   9.643867 seconds
Complete requests:      1000
Failed requests:        0
Write errors:           0
Total transferred:      189000 bytes
HTML transferred:       14000 bytes
Requests per second:    103.69 [#/sec] (mean)
Time per request:       96.439 [ms] (mean)
Time per request:       9.644 [ms] (mean, across all concurrent requests)
Transfer rate:          19.08 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   2.9      0      10
Processing:    20   94   7.3     90     130
Waiting:        0   43  28.1     40     100
Total:         20   95   7.3    100     130

Percentage of the requests served within a certain time (ms)
  50%    100
  66%    100
  75%    100
  80%    100
  90%    100
  95%    100
  98%    100
  99%    110
 100%    130 (longest request)
Finished 1000 requests
"""
    
    parse_patterns = [('complete_requests', 'Completed',
                       r'^Complete requests:\s*(\d+)'),
                      ('failed_requests', 'Failed',
                       r'^Failed requests:\s*(\d+)'),
                      ('requests_per_second', 'req/sec',
                       r'^Requests per second:\s*([0-9.]+)'),
                      ('time_per_request_concurrent', 'msec/req',
                       r'^Time per request:\s*([0-9.]+).*concurrent requests\)$'),
                      ('transfer_rate', 'KB/sec',
                       r'^Transfer rate:\s*([0-9.]+)'),
                      ]
    
    def __init__(self, path=MOUNT_POINT + "/", requests=1000, concurrency=10):
        self.path = path
        self.requests = requests
        self.concurrency = concurrency
    
    def args(self):
        port = cherrypy.config.get('server.socket_port')
        assert self.concurrency > 0
        assert self.requests > 0
        return ("-n %s -c %s http://localhost:%s%s" %
                (self.requests, self.concurrency, port, self.path))
    
    def run(self):
        # Parse output of ab, setting attributes on self
        self.output = read_process(AB_PATH or "ab", self.args())
        for attr, name, pattern in self.parse_patterns:
            val = re.search(pattern, self.output, re.MULTILINE)
            if val:
                val = val.group(1)
                setattr(self, attr, val)
            else:
                setattr(self, attr, None)


safe_threads = (25, 50, 100, 200, 400)
if sys.platform in ("win32",):
    # For some reason, ab crashes with > 50 threads on my Win2k laptop.
    safe_threads = (10, 20, 30, 40, 50)


def thread_report(path=MOUNT_POINT + "/", concurrency=safe_threads):
    sess = ABSession(path)
    attrs, names, patterns = zip(*sess.parse_patterns)
    rows = [('threads',) + names]
    for c in concurrency:
        sess.concurrency = c
        sess.run()
        rows.append([c] + [getattr(sess, attr) for attr in attrs])
    return rows

def size_report(sizes=(1, 10, 50, 100, 100000, 100000000),
               concurrency=50):
    sess = ABSession(concurrency=concurrency)
    attrs, names, patterns = zip(*sess.parse_patterns)
    rows = [('bytes',) + names]
    for sz in sizes:
        sess.path = "%s/sizer?size=%s" % (MOUNT_POINT, sz)
        sess.run()
        rows.append([sz] + [getattr(sess, attr) for attr in attrs])
    return rows

def print_report(rows):
    widths = []
    for i in range(len(rows[0])):
        lengths = [len(str(row[i])) for row in rows]
        widths.append(max(lengths))
    for row in rows:
        print
        for i, val in enumerate(row):
            print str(val).rjust(widths[i]), "|",
    print


def run_standard_benchmarks():
    print
    print ("Client Thread Report (1000 requests, 14 byte response body, "
           "%s server threads):" % cherrypy.config.get('server.thread_pool'))
    print_report(thread_report())
    
    print
    print ("Client Thread Report (1000 requests, 14 bytes via static_filter, "
           "%s server threads):" % cherrypy.config.get('server.thread_pool'))
    print_report(thread_report("%s/static/index.html" % MOUNT_POINT))
    
    print
    print ("Size Report (1000 requests, 50 client threads, "
           "%s server threads):" % cherrypy.config.get('server.thread_pool'))
    print_report(size_report())


started = False
def startup(req=None):
    """Start the CherryPy app server in 'serverless' mode (for WSGI)."""
    global started
    if not started:
        started = True
        cherrypy.server.start(init_only=True, server_class=None)
    return 0 # apache.OK



#                         modpython and other WSGI                         #

def startup_modpython(req=None):
    """Start the CherryPy app server in 'serverless' mode (for WSGI)."""
    global started
    if not started:
        started = True
        if req.get_options().has_key("nullreq"):
            cherrypy.server.request_class = NullRequest
            cherrypy.server.response_class = NullResponse
        ab_opt = req.get_options().get("ab", "")
        if ab_opt:
            global AB_PATH
            AB_PATH = ab_opt
        cherrypy.server.start(init_only=True, server_class=None)
    
    import modpython_gateway
    return modpython_gateway.handler(req)

mp_conf_template = """
# Apache2 server configuration file for benchmarking CherryPy with mod_python.

DocumentRoot "/"
Listen 8080
LoadModule python_module modules/mod_python.so

<Location />
    SetHandler python-program
    PythonHandler cherrypy.test.benchmark::startup_modpython
    PythonOption application cherrypy._cpwsgi::wsgiApp
    PythonDebug On
%s%s
</Location>
"""

def run_modpython():
    # Pass the null and ab=path options through Apache
    nullreq_opt = ""
    if "--null" in opts:
        nullreq_opt = "    PythonOption nullreq\n"
    
    ab_opt = ""
    if "--ab" in opts:
        ab_opt = "    PythonOption ab %s\n" % opts["--ab"]
    
    conf_data = mp_conf_template % (ab_opt, nullreq_opt)
    mpconf = os.path.join(curdir, "bench_mp.conf")
    
    f = open(mpconf, 'wb')
    try:
        f.write(conf_data)
    finally:
        f.close()
    
    apargs = "-k start -f %s" % mpconf
    try:
        read_process(APACHE_PATH or "apache", apargs)
        run()
    finally:
        os.popen("apache -k stop")



if __name__ == '__main__':
    longopts = ['modpython', 'null', 'notests', 'help', 'ab=', 'apache=']
    try:
        switches, args = getopt.getopt(sys.argv[1:], "", longopts)
        opts = dict(switches)
    except getopt.GetoptError:
        print __doc__
        sys.exit(2)
    
    if "--help" in opts:
        print __doc__
        sys.exit(0)
    
    if "--ab" in opts:
        AB_PATH = opts['--ab']
    
    if "--notests" in opts:
        # Return without stopping the server, so that the pages
        # can be tested from a standard web browser.
        def run():
            if "--null" in opts:
                print "Using null Request object"
    else:
        def run():
            end = time.time() - start
            print "Started in %s seconds" % end
            if "--null" in opts:
                print "\nUsing null Request object"
            try:
                run_standard_benchmarks()
            finally:
                cherrypy.server.stop()
    
    print "Starting CherryPy app server..."
    start = time.time()
    
    if "--modpython" in opts:
        run_modpython()
    else:
        if "--null" in opts:
            cherrypy.server.request_class = NullRequest
            cherrypy.server.response_class = NullResponse
        
        # This will block
        cherrypy.server.start_with_callback(run)