From: Ben Darnell Date: Mon, 20 Jan 2014 22:23:20 +0000 (-0500) Subject: Add a benchmark for coroutine processing. X-Git-Tag: v4.0.0b1~132 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b1482368b19e75f1ac679ba398af31319f8beb6d;p=thirdparty%2Ftornado.git Add a benchmark for coroutine processing. --- diff --git a/demos/benchmark/gen_benchmark.py b/demos/benchmark/gen_benchmark.py new file mode 100644 index 000000000..adbe44fc8 --- /dev/null +++ b/demos/benchmark/gen_benchmark.py @@ -0,0 +1,47 @@ +#!/usr/bin/env python +# +# A simple benchmark of the tornado.gen module. +# Runs in two modes, testing new-style (@coroutine and Futures) +# and old-style (@engine and Tasks) coroutines. + +from timeit import Timer + +from tornado import gen +from tornado.options import options, define, parse_command_line + +define('num', default=10000, help='number of iterations') + +# These benchmarks are delicate. They hit various fast-paths in the gen +# machinery in order to stay synchronous so we don't need an IOLoop. +# This removes noise from the results, but it's easy to change things +# in a way that completely invalidates the results. + +@gen.engine +def e2(callback): + callback() + +@gen.engine +def e1(): + for i in range(10): + yield gen.Task(e2) + +@gen.coroutine +def c2(): + pass + +@gen.coroutine +def c1(): + for i in range(10): + yield c2() + +def main(): + parse_command_line() + t = Timer(e1) + results = t.timeit(options.num) / options.num + print 'engine: %0.3f ms per iteration' % (results * 1000) + t = Timer(c1) + results = t.timeit(options.num) / options.num + print 'coroutine: %0.3f ms per iteration' % (results * 1000) + +if __name__ == '__main__': + main()