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

Changeset 997

Show
Ignore:
Timestamp:
03/03/06 16:15:02
Author:
fumanchu
Message:

Even more improvements to benchmark.py. New getopt options --ab=path and --apache=path. The modpython conf is now generated automatically.

Files:

Legend:

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

    r996 r997  
    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 
     24import getopt 
    325import os 
    426curdir = os.path.join(os.getcwd(), os.path.dirname(__file__)) 
     
    1335 
    1436 
     37AB_PATH = "" 
     38APACHE_PATH = "" 
    1539MOUNT_POINT = "/cpbench/users/rdelon/apps/blog" 
    1640 
     
    5579 
    5680 
     81class 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 
     101class NullResponse: 
     102    pass 
     103 
     104 
    57105def read_process(cmd, args=""): 
    58106    pipein, pipeout = os.popen4("%s %s" % (cmd, args)) 
     
    154202    def run(self): 
    155203        # Parse output of ab, setting attributes on self 
    156         self.output = read_process("ab", self.args()) 
     204        self.output = read_process(AB_PATH or "ab", self.args()) 
    157205        for attr, name, pattern in self.parse_patterns: 
    158206            val = re.search(pattern, self.output, re.MULTILINE) 
     
    220268 
    221269 
    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  
    246270started = False 
    247271def startup(req=None): 
     
    253277    return 0 # apache.OK 
    254278 
    255 def startup_null(req=None): 
    256     """Start the CherryPy app server in NULL 'serverless' mode (for WSGI).""" 
     279 
     280 
     281#                         modpython and other WSGI                         # 
     282 
     283def startup_modpython(req=None): 
     284    """Start the CherryPy app server in 'serverless' mode (for WSGI).""" 
    257285    global started 
    258286    if not started: 
    259287        started = True 
    260         cherrypy.server.request_class = NullRequest 
    261         cherrypy.server.response_class = NullResponse 
     288        if req.get_options().has_key("nullreq"): 
     289            cherrypy.server.request_class = NullRequest 
     290            cherrypy.server.response_class = NullResponse 
     291        ab_opt = req.get_options().get("ab", "") 
     292        if ab_opt: 
     293            global AB_PATH 
     294            AB_PATH = ab_opt 
    262295        cherrypy.server.start(init_only=True, server_class=None) 
    263     return 0 # apache.OK 
     296     
     297    import modpython_gateway 
     298    return modpython_gateway.handler(req) 
     299 
     300mp_conf_template = """ 
     301# Apache2 server configuration file for benchmarking CherryPy with mod_python. 
     302 
     303DocumentRoot "/" 
     304Listen 8080 
     305LoadModule 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 
     316def 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 
    264342 
    265343 
    266344if __name__ == '__main__': 
    267     if "-notests" in sys.argv: 
     345    longopts = ['modpython', 'null', 'notests', 'help', 'ab=', 'apache='] 
     346    try: 
     347        switches, args = getopt.getopt(sys.argv[1:], "", longopts) 
     348        opts = dict(switches) 
     349    except getopt.GetoptError: 
     350        print __doc__ 
     351        sys.exit(2) 
     352     
     353    if "--help" in opts: 
     354        print __doc__ 
     355        sys.exit(0) 
     356     
     357    if "--ab" in opts: 
     358        AB_PATH = opts['--ab'] 
     359     
     360    if "--notests" in opts: 
    268361        # Return without stopping the server, so that the pages 
    269362        # can be tested from a standard web browser. 
    270         run = lambda: None 
     363        def run(): 
     364            if "--null" in opts: 
     365                print "Using null Request object" 
    271366    else: 
    272367        def run(): 
    273368            end = time.time() - start 
    274369            print "Started in %s seconds" % end 
     370            if "--null" in opts: 
     371                print "\nUsing null Request object" 
    275372            try: 
    276373                run_standard_benchmarks() 
     
    281378    start = time.time() 
    282379     
    283     if "-modpython" in sys.argv: 
    284         try: 
    285             mpconf = os.path.join(curdir, "bench_mp.conf") 
    286             if "-null" in sys.argv: 
    287                 # Pass the null option through Apache 
    288                 read_process("apache", "-k start -D nullreq -f %s" % mpconf) 
    289             else: 
    290                 read_process("apache", "-k start -f %s" % mpconf) 
    291             run() 
    292         finally: 
    293             os.popen("apache -k stop") 
     380    if "--modpython" in opts: 
     381        run_modpython() 
    294382    else: 
    295         if "-null" in sys.argv
     383        if "--null" in opts
    296384            cherrypy.server.request_class = NullRequest 
    297385            cherrypy.server.response_class = NullResponse 

Hosted by WebFaction

Log in as guest/cpguest to create tickets