tornado/maint/benchmark/benchmark.py

93 lines
2.4 KiB
Python
Raw Normal View History

2010-12-11 01:03:21 +00:00
#!/usr/bin/env python
#
# A simple benchmark of tornado's HTTP stack.
# Requires 'ab' to be installed.
#
# Running without profiling:
# demos/benchmark/benchmark.py
2011-07-06 17:00:17 +00:00
# demos/benchmark/benchmark.py --quiet --num_runs=5|grep "Requests per second"
2010-12-11 01:03:21 +00:00
#
# Running with profiling:
#
# python -m cProfile -o /tmp/prof demos/benchmark/benchmark.py
2011-07-06 17:00:17 +00:00
# python -m pstats /tmp/prof
# % sort time
# % stats 20
2010-12-11 01:03:21 +00:00
from tornado.ioloop import IOLoop
from tornado.options import define, options, parse_command_line
from tornado.web import RequestHandler, Application
2011-07-06 17:00:17 +00:00
import random
2010-12-11 01:03:21 +00:00
import signal
import subprocess
try:
xrange
except NameError:
xrange = range
2011-07-06 17:00:17 +00:00
# choose a random port to avoid colliding with TIME_WAIT sockets left over
# from previous runs.
define("min_port", type=int, default=8000)
define("max_port", type=int, default=9000)
2010-12-11 01:03:21 +00:00
2011-07-06 17:00:17 +00:00
# Increasing --n without --keepalive will eventually run into problems
# due to TIME_WAIT sockets
define("n", type=int, default=15000)
define("c", type=int, default=25)
define("keepalive", type=bool, default=False)
2011-07-06 17:00:17 +00:00
define("quiet", type=bool, default=False)
# Repeat the entire benchmark this many times (on different ports)
# This gives JITs time to warm up, etc. Pypy needs 3-5 runs at
# --n=15000 for its JIT to reach full effectiveness
define("num_runs", type=int, default=1)
2010-12-11 01:03:21 +00:00
define("ioloop", type=str, default=None)
2010-12-11 01:03:21 +00:00
class RootHandler(RequestHandler):
def get(self):
self.write("Hello, world")
def _log(self):
pass
2010-12-11 01:03:21 +00:00
def handle_sigchld(sig, frame):
IOLoop.current().add_callback_from_signal(IOLoop.current().stop)
2010-12-11 01:03:21 +00:00
2010-12-11 01:03:21 +00:00
def main():
parse_command_line()
if options.ioloop:
IOLoop.configure(options.ioloop)
2011-07-06 17:00:17 +00:00
for i in xrange(options.num_runs):
run()
2011-07-06 17:00:17 +00:00
def run():
io_loop = IOLoop(make_current=True)
2010-12-11 01:03:21 +00:00
app = Application([("/", RootHandler)])
2011-07-06 17:00:17 +00:00
port = random.randrange(options.min_port, options.max_port)
app.listen(port, address='127.0.0.1')
2010-12-11 01:03:21 +00:00
signal.signal(signal.SIGCHLD, handle_sigchld)
args = ["ab"]
args.extend(["-n", str(options.n)])
args.extend(["-c", str(options.c)])
if options.keepalive:
args.append("-k")
2011-07-06 17:00:17 +00:00
if options.quiet:
# just stops the progress messages printed to stderr
args.append("-q")
args.append("http://127.0.0.1:%d/" % port)
subprocess.Popen(args)
io_loop.start()
io_loop.close()
io_loop.clear_current()
2010-12-11 01:03:21 +00:00
2010-12-11 01:03:21 +00:00
if __name__ == '__main__':
main()