]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Add a simple HTTP benchmark
authorBen Darnell <ben@bendarnell.com>
Sat, 11 Dec 2010 01:03:21 +0000 (17:03 -0800)
committerBen Darnell <ben@bendarnell.com>
Sat, 11 Dec 2010 01:03:21 +0000 (17:03 -0800)
demos/benchmark/benchmark.py [new file with mode: 0755]

diff --git a/demos/benchmark/benchmark.py b/demos/benchmark/benchmark.py
new file mode 100755 (executable)
index 0000000..9236a24
--- /dev/null
@@ -0,0 +1,45 @@
+#!/usr/bin/env python
+#
+# A simple benchmark of tornado's HTTP stack.
+# Requires 'ab' to be installed.
+#
+# Running without profiling:
+# demos/benchmark/benchmark.py
+#
+# Running with profiling:
+#
+# python -m cProfile -o /tmp/prof demos/benchmark/benchmark.py
+# python -c 'import pstats; pstats.Stats("/tmp/prof").strip_dirs().sort_stats("time").print_callers(20)'
+
+from tornado.ioloop import IOLoop
+from tornado.options import define, options, parse_command_line
+from tornado.web import RequestHandler, Application
+
+import signal
+import subprocess
+
+
+define("port", type=int, default=8888)
+
+class RootHandler(RequestHandler):
+    def get(self):
+        self.write("Hello, world")
+
+    def _log(self):
+        pass
+
+def handle_sigchld(sig, frame):
+    IOLoop.instance().add_callback(IOLoop.instance().stop)
+
+def main():
+    parse_command_line()
+    app = Application([("/", RootHandler)])
+    app.listen(options.port)
+    signal.signal(signal.SIGCHLD, handle_sigchld)
+    proc = subprocess.Popen(
+        "ab -n 10000 -c 25 http://127.0.0.1:%d/" % options.port,
+        shell=True)
+    IOLoop.instance().start()
+
+if __name__ == '__main__':
+    main()