Created
August 29, 2012 20:02
-
-
Save anonymous/3518102 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import functools | |
| import json | |
| import tornado.web | |
| from tornado import httpclient | |
| from datetime import datetime | |
| class TestHandler(tornado.web.RequestHandler): | |
| SOME_URL = '__INSERT_URL__' | |
| @tornado.web.asynchronous | |
| def get(self): | |
| self.resp_latencies = [] | |
| self.num_urls = 20 | |
| urls = [self.SOME_URL] * self.num_urls | |
| http_client = httpclient.AsyncHTTPClient(max_clients=1000) | |
| body = json.dumps({'id': 1}) | |
| for url in urls: | |
| http_client.fetch(url, | |
| callback=functools.partial(self.on_response, | |
| datetime.now()), | |
| method='POST', | |
| body=body, | |
| headers={'Content-Type': 'application/json'}, | |
| request_timeout=1) | |
| def on_response(self, start_time, response): | |
| end_time = datetime.now() | |
| self.resp_latencies.append((end_time - start_time).total_seconds()) | |
| if len(self.resp_latencies) == self.num_urls: | |
| print sum(self.resp_latencies) / len(self.resp_latencies) | |
| self.write("%s" % (sum(self.resp_latencies) / len(self.resp_latencies))) | |
| self.finish() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment