Created
October 7, 2012 21:13
-
-
Save daharon/3849612 to your computer and use it in GitHub Desktop.
Attempting to induce memory leaks, a la http://stackoverflow.com/questions/12626064
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
| #!/usr/bin/env python | |
| import time | |
| import resource | |
| import logging | |
| from tornado import web, ioloop, gen, options | |
| from tornado.httpclient import AsyncHTTPClient | |
| from tornado.escape import url_escape | |
| log = logging.getLogger() | |
| port = 8080 | |
| websites = [ | |
| "http://localhost:%d/echo?text=%s" % (port, url_escape('Some content')), | |
| "http://localhost:%d/echo?text=%s" % (port, url_escape('Some different content')), | |
| "http://localhost:%d/timeout" % port | |
| ] | |
| class Aggregate(web.RequestHandler): | |
| @web.asynchronous | |
| @gen.engine | |
| def get(self): | |
| """ Retrieve combined website contents. """ | |
| http_client = AsyncHTTPClient(max_clients=20) | |
| responses = yield [ | |
| gen.Task(http_client.fetch, website, request_timeout=5) | |
| for website in websites | |
| ] | |
| response_summary = ''.join("<p><hr>%s</p>" % r for r in responses) | |
| aggregated_content = self._aggregate(responses) | |
| mem_usage = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss | |
| self.write("<p>Memory usage: %d KB</p>" % mem_usage) | |
| self.write("<p>Aggregated content length: %d bytes</p>" % len(aggregated_content)) | |
| self.write(response_summary) | |
| self.finish() | |
| @staticmethod | |
| def _aggregate(responses): | |
| return ''.join( [ | |
| r.body for r in responses if r.body | |
| ] ) | |
| class Timeout(web.RequestHandler): | |
| @web.asynchronous | |
| def get(self): | |
| """ Simulate a timeout. """ | |
| self._timeout_handle = ioloop.IOLoop.instance().add_timeout( | |
| time.time() + 60 * 10, # Sleep for ten minutes. | |
| self._send_response | |
| ) | |
| def on_connection_close(self): | |
| log.debug('Connection closed. Removing timeout.') | |
| ioloop.IOLoop.instance().remove_timeout(self._timeout_handle) | |
| def _send_response(self): | |
| self.write('Response body') | |
| self.finish() | |
| class Echo(web.RequestHandler): | |
| def get(self): | |
| text = self.get_argument('text') | |
| self.write(self.decode_argument(text)) | |
| routes = [ | |
| (r"/", Aggregate), | |
| (r"/timeout", Timeout), | |
| (r"/echo", Echo) | |
| ] | |
| if __name__ == "__main__": | |
| options.parse_command_line() | |
| application = web.Application(routes) | |
| application.listen(port) | |
| ioloop.IOLoop.instance().start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment