Search code examples
pythonbenchmarkingtornadokeep-aliveapachebench

Why ApacheBench test on tornado aborts when I add `-k`?


I start a tornado http server like this:

app = tornado.web.Application([
    (r'.*', MyRequestHandler),
])

http_server = tornado.httpserver.HTTPServer(app, no_keep_alive=True)
http_server.listen(port)
ioloop = tornado.ioloop.IOLoop.instance()

ioloop.start()

I want to ingore request with header Connection: keep-alive then set no_keep_alive to True.

when I run

ab -n 1000 -c 10 -k http://127.0.0.1:28000/

output

Benchmarking 127.0.0.1 (be patient)
apr_socket_recv: Connection reset by peer (104)
Total of 11 requests completed

when I remove -k, everything is working well.


Solution

  • If short - because apache benchmark opens connection once on tests. In your case, you have 10 connections - and all of them killed after first 10 requests.

    Here is code of finish request for HTTPConnection from tornado.

    def _finish_request(self):
        if self.no_keep_alive:
            disconnect = True
        else:
            connection_header = self._request.headers.get("Connection")
            if connection_header is not None:
                connection_header = connection_header.lower()
            if self._request.supports_http_1_1():
                disconnect = connection_header == "close"
            elif ("Content-Length" in self._request.headers
                    or self._request.method in ("HEAD", "GET")):
                disconnect = connection_header != "keep-alive"
            else:
                disconnect = True
        self._request = None
        self._request_finished = False
        if disconnect:
            self.stream.close()
            return
        self.stream.read_until(b("\r\n\r\n"), self._header_callback)
    

    Your error:

    apr_socket_recv: Connection reset by peer (104)
    

    As we can guess, you must force apache benchmark to open new connection, if old connection closed. I am not sure if you will able to get representative results in this case.