Download Install Tutorial Docs FAQ Tools WikiLicense Team IRC Planet Involvement Shop Book

Changeset 988

Show
Ignore:
Timestamp:
03/02/06 00:49:55
Author:
fumanchu
Message:

Improvements to benchmark.py. New "-modpython" switch which popens apache, runs the tests, then stops apache.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/cherrypy/test/benchmark.py

    r987 r988  
    1212 
    1313 
     14MOUNT_POINT = "/cpbench/users/rdelon/apps/blog" 
     15 
     16__all__ = ['ABSession', 'Root', 'print_chart', 'read_process', 
     17           'run_standard_benchmarks', 'safe_threads', 
     18           'size_chart', 'startup', 'thread_chart', 
     19           ] 
     20 
    1421size_cache = {} 
    1522 
     
    2229        resp = size_cache.get(size, None) 
    2330        if resp is None: 
    24             size_cache[size] = resp = "X" * size 
     31            size_cache[size] = resp = "X" * int(size) 
    2532        return resp 
    2633    sizer.exposed = True 
    2734 
    2835 
    29 cherrypy.tree.mount(Root()) 
    30 cherrypy.lowercase_api = True 
    31 cherrypy.config.update({ 
     36conf = { 
    3237    'global': { 
    3338        'server.log_to_screen': False, 
     39##        'server.log_file': os.path.join(curdir, "bench.log"), 
    3440        'server.environment': 'production', 
     41        'server.socket_host': 'localhost', 
     42        'server.socket_port': 8080, 
    3543        }, 
    3644    '/static': { 
     
    3947        'static_filter.root': curdir, 
    4048        }, 
    41     }) 
     49    } 
     50cherrypy.tree.mount(Root(), MOUNT_POINT, conf) 
     51cherrypy.lowercase_api = True 
     52 
     53 
     54def read_process(cmd, args=""): 
     55    pipein, pipeout = os.popen4("%s %s" % (cmd, args)) 
     56    output = pipeout.read() 
     57    if (# Windows 
     58        output.startswith("'%s' is not recognized" % cmd) 
     59        # bash 
     60        or re.match(r"bash: .*: No such file", output) 
     61        ): 
     62        raise IOError('%s must be on your system path.' % cmd) 
     63    pipeout.close() 
     64    return output 
    4265 
    4366 
     
    114137                      ] 
    115138     
    116     def __init__(self, path="/", requests=1000, concurrency=10): 
     139    def __init__(self, path=MOUNT_POINT + "/", requests=1000, concurrency=10): 
    117140        self.path = path 
    118141        self.requests = requests 
    119142        self.concurrency = concurrency 
    120143     
    121     def cmd(self): 
     144    def args(self): 
    122145        port = cherrypy.config.get('server.socket_port') 
    123146        assert self.concurrency > 0 
    124147        assert self.requests > 0 
    125         return ("ab -n %s -c %s http://localhost:%s%s" % 
     148        return ("-n %s -c %s http://localhost:%s%s" % 
    126149                (self.requests, self.concurrency, port, self.path)) 
    127150     
    128151    def run(self): 
    129         pipein, pipeout = os.popen4(self.cmd()) 
    130         self.output = pipeout.read() 
    131         if (# Windows 
    132             self.output.startswith("'ab' is not recognized") 
    133             # bash 
    134             or re.match(r"bash: .*: No such file", self.output) 
    135             ): 
    136             raise IOError('The Apache benchmark tool "ab" must be ' 
    137                           'on your system path.') 
    138         pipeout.close() 
    139          
    140         # Parse output, setting attribute on self 
     152        # Parse output of ab, setting attributes on self 
     153        self.output = read_process("ab", self.args()) 
    141154        for attr, name, pattern in self.parse_patterns: 
    142155            val = re.search(pattern, self.output, re.MULTILINE) 
     
    152165 
    153166 
    154 def thread_chart(path="/", concurrency=safe_threads): 
     167def thread_chart(path=MOUNT_POINT + "/", concurrency=safe_threads): 
    155168    sess = ABSession(path) 
    156169    attrs, names, patterns = zip(*sess.parse_patterns) 
     
    168181    rows = [('bytes',) + names] 
    169182    for sz in sizes: 
    170         sess.path = "/sizer?size=%s" % sz 
     183        sess.path = "%s/sizer?size=%s" % (MOUNT_POINT, sz) 
    171184        sess.run() 
    172185        rows.append([sz] + [getattr(sess, attr) for attr in attrs]) 
     
    185198 
    186199 
     200def run_standard_benchmarks(): 
     201    print 
     202    print "Thread Chart (1000 requests, 14 byte response body):" 
     203    print_chart(thread_chart()) 
     204     
     205    print 
     206    print "Thread Chart (1000 requests, 14 bytes via static_filter):" 
     207    print_chart(thread_chart("%s/static/index.html" % MOUNT_POINT)) 
     208     
     209    print 
     210    print "Size Chart (1000 requests, 50 threads):" 
     211    print_chart(size_chart()) 
     212 
     213 
     214started = False 
     215def startup(req=None): 
     216    """Start the CherryPy app server in 'serverless' mode (for WSGI).""" 
     217    global started 
     218    if not started: 
     219        started = True 
     220        cherrypy.server.start(init_only=True, server_class=None) 
     221    return 0 # apache.OK 
     222 
     223 
    187224if __name__ == '__main__': 
    188     def run_standard_benchmarks(): 
    189         end = time.time() - start 
    190         print "Started in %s seconds" % end 
     225    if "-notests" in sys.argv: 
     226        # Return without stopping the server, so that the pages 
     227        # can be tested from a standard web browser. 
     228        run = lambda x: x 
     229    else: 
     230        def run(): 
     231            end = time.time() - start 
     232            print "Started in %s seconds" % end 
     233            try: 
     234                run_standard_benchmarks() 
     235            finally: 
     236                cherrypy.server.stop() 
     237     
     238    print "Starting CherryPy app server..." 
     239    start = time.time() 
     240     
     241    if "-modpython" in sys.argv: 
    191242        try: 
    192             print 
    193             print "Thread Chart (1000 requests, 14 byte response body):" 
    194             print_chart(thread_chart()) 
    195              
    196             print 
    197             print "Thread Chart (1000 requests, 14 bytes via static_filter):" 
    198             print_chart(thread_chart("/static/index.html")) 
    199              
    200             print 
    201             print "Size Chart (1000 requests, 50 threads):" 
    202             print_chart(size_chart()) 
     243            mpconf = os.path.join(curdir, "bench_mp.conf") 
     244            read_process("apache", "-k start -f %s" % mpconf) 
     245            run() 
    203246        finally: 
    204             cherrypy.server.stop() 
    205      
    206     print "Starting CherryPy HTTP server..." 
    207     start = time.time() 
    208      
    209     # This will block 
    210     cherrypy.server.start_with_callback(run_standard_benchmarks) 
     247            os.popen("apache -k stop") 
     248    else: 
     249        # This will block 
     250        cherrypy.server.start_with_callback(run) 

Hosted by WebFaction

Log in as guest/cpguest to create tickets