
"""Test the performance of simple HTTP serving and client using the Tornado
framework.

A trivial "application" is generated which generates a number of chunks of
data as a HTTP response's body.
"""

import socket

from six.moves import xrange
import perf

from tornado.httpclient import AsyncHTTPClient
from tornado.httpserver import HTTPServer
from tornado.gen import coroutine, Task
from tornado.ioloop import IOLoop
from tornado.netutil import bind_sockets
from tornado.web import RequestHandler, Application


HOST = "127.0.0.1"
FAMILY = socket.AF_INET

CHUNK = b"Hello world\n" * 1000
NCHUNKS = 5

CONCURRENCY = 150


class MainHandler(RequestHandler):

    @coroutine
    def get(self):
        for i in range(NCHUNKS):
            self.write(CHUNK)
            yield Task(self.flush)

    def compute_etag(self):
        # Overriden to avoid stressing hashlib in this benchmark
        return None


def make_application():
    return Application([
        (r"/", MainHandler),
    ])


def make_http_server(loop, request_handler):
    server = HTTPServer(request_handler, io_loop=loop)
    sockets = bind_sockets(0, HOST, family=FAMILY)
    assert len(sockets) == 1
    server.add_sockets(sockets)
    return sockets[0].getsockname()


def bench_tornado(loops):
    loop = IOLoop.instance()
    host, port = make_http_server(loop, make_application())
    url = "http://%s:%s/" % (host, port)
    namespace = {}

    @coroutine
    def run_client():
        client = AsyncHTTPClient()
        range_it = xrange(loops)
        t0 = perf.perf_counter()

        for _ in range_it:
            futures = [client.fetch(url) for j in xrange(CONCURRENCY)]
            for fut in futures:
                resp = yield fut
                buf = resp.buffer
                buf.seek(0, 2)
                assert buf.tell() == len(CHUNK) * NCHUNKS

        namespace['dt'] = perf.perf_counter() - t0

    loop.run_sync(run_client)
    return namespace['dt']


if __name__ == "__main__":
    kw = {}
    if perf.python_has_jit():
        # PyPy needs to compute more warmup values to warmup its JIT
        kw['warmups'] = 30
    runner = perf.Runner(**kw)
    runner.metadata['description'] = ("Test the performance of HTTP requests "
                                      "with Tornado.")
    runner.bench_time_func('tornado_http', bench_tornado)

@@ -1205,22 +1205,22 @@
 ts)%0A    
-return
+sock =
  sockets
@@ -1226,22 +1226,24 @@
 s%5B0%5D
-.getsockname()
+%0A    return sock
 %0A%0A%0Ad
@@ -1300,26 +1300,20 @@
 e()%0A    
-host, port
+sock
  = make_
@@ -1350,16 +1350,52 @@
 tion())%0A
+    host, port = sock.getsockname()%0A
     url 
@@ -1923,16 +1923,39 @@
 r() - t0
+%0A        client.close()
 %0A%0A    lo
@@ -1978,16 +1978,34 @@
 client)%0A
+    sock.close()%0A%0A
     retu

fix ResourceWarning warnings