Skip to content

Instantly share code, notes, and snippets.

@eklitzke
Created June 15, 2011 20:16
Show Gist options
  • Save eklitzke/1028001 to your computer and use it in GitHub Desktop.
Save eklitzke/1028001 to your computer and use it in GitHub Desktop.
simple tornado httpclient benchmark
import os
import optparse
import signal
import time
import tornado.httpserver
import tornado.httpclient
import tornado.simple_httpclient
import tornado.curl_httpclient
import tornado.ioloop
def make_http_callback(size):
response = os.urandom(size)
header = 'HTTP/1.1 200 OK\r\nConnection: close\r\nContent-Length: %d\r\n\r\n%s' % (size, response)
def http_callback(request):
request.write(header)
request.finish()
return http_callback
def main(curl, port, num_requests, size):
pid = os.fork()
loop = tornado.ioloop.IOLoop.instance()
if pid:
def reap(signum, frame):
loop.stop()
signal.signal(signal.SIGCHLD, reap)
callback = make_http_callback(size)
server = tornado.httpserver.HTTPServer(callback, no_keep_alive=True, io_loop=loop)
server.listen(port)
loop.start()
else:
time.sleep(0.1)
if curl:
client = tornado.curl_httpclient.CurlAsyncHTTPClient(io_loop=loop)
else:
client = tornado.simple_httpclient.SimpleAsyncHTTPClient(io_loop=loop)
request = tornado.httpclient.HTTPRequest('http://localhost:%d/' % port)
t0 = time.time()
count = [0]
def fetch_callback(data):
count[0] += 1
if count[0] >= num_requests:
ms_per_request = (time.time() - t0) * 1000.0 / num_requests
print '%1.3f ms' % (ms_per_request)
loop.stop()
else:
loop.add_callback(lambda: client.fetch(request, fetch_callback))
client.fetch(request, fetch_callback)
loop.start()
if __name__ == '__main__':
parser = optparse.OptionParser()
parser.add_option('-p', '--port', type='int', default=8000)
parser.add_option('--curl', action='store_true', default=False)
parser.add_option('-n', '--num-requests', type='int', default=1000)
parser.add_option('-s', '--size', type='int', default=1000, help='size of the http payload')
opts, args = parser.parse_args()
main(opts.curl, opts.port, opts.num_requests, opts.size)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment