Changeset 988
- Timestamp:
- 03/02/06 00:49:55
- Files:
-
- trunk/cherrypy/test/bench_mp.conf (added)
- trunk/cherrypy/test/benchmark.py (modified) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/cherrypy/test/benchmark.py
r987 r988 12 12 13 13 14 MOUNT_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 14 21 size_cache = {} 15 22 … … 22 29 resp = size_cache.get(size, None) 23 30 if resp is None: 24 size_cache[size] = resp = "X" * size31 size_cache[size] = resp = "X" * int(size) 25 32 return resp 26 33 sizer.exposed = True 27 34 28 35 29 cherrypy.tree.mount(Root()) 30 cherrypy.lowercase_api = True 31 cherrypy.config.update({ 36 conf = { 32 37 'global': { 33 38 'server.log_to_screen': False, 39 ## 'server.log_file': os.path.join(curdir, "bench.log"), 34 40 'server.environment': 'production', 41 'server.socket_host': 'localhost', 42 'server.socket_port': 8080, 35 43 }, 36 44 '/static': { … … 39 47 'static_filter.root': curdir, 40 48 }, 41 }) 49 } 50 cherrypy.tree.mount(Root(), MOUNT_POINT, conf) 51 cherrypy.lowercase_api = True 52 53 54 def 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 42 65 43 66 … … 114 137 ] 115 138 116 def __init__(self, path= "/", requests=1000, concurrency=10):139 def __init__(self, path=MOUNT_POINT + "/", requests=1000, concurrency=10): 117 140 self.path = path 118 141 self.requests = requests 119 142 self.concurrency = concurrency 120 143 121 def cmd(self):144 def args(self): 122 145 port = cherrypy.config.get('server.socket_port') 123 146 assert self.concurrency > 0 124 147 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" % 126 149 (self.requests, self.concurrency, port, self.path)) 127 150 128 151 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()) 141 154 for attr, name, pattern in self.parse_patterns: 142 155 val = re.search(pattern, self.output, re.MULTILINE) … … 152 165 153 166 154 def thread_chart(path= "/", concurrency=safe_threads):167 def thread_chart(path=MOUNT_POINT + "/", concurrency=safe_threads): 155 168 sess = ABSession(path) 156 169 attrs, names, patterns = zip(*sess.parse_patterns) … … 168 181 rows = [('bytes',) + names] 169 182 for sz in sizes: 170 sess.path = " /sizer?size=%s" % sz183 sess.path = "%s/sizer?size=%s" % (MOUNT_POINT, sz) 171 184 sess.run() 172 185 rows.append([sz] + [getattr(sess, attr) for attr in attrs]) … … 185 198 186 199 200 def 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 214 started = False 215 def 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 187 224 if __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: 191 242 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() 203 246 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)

