Permalink
Cannot retrieve contributors at this time
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
56 lines (47 sloc)
1.69 KB
This file contains 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 datetime | |
from plop.collector import Collector, PlopFormatter | |
from tornado import gen | |
from tornado.httpclient import AsyncHTTPClient | |
from tornado.ioloop import IOLoop | |
from tornado.options import parse_command_line, define, options | |
from tornado.web import Application, RequestHandler, asynchronous | |
define('port', default=8888) | |
define('output_file', default='profile.out') | |
class HelloHandler(RequestHandler): | |
@asynchronous | |
@gen.engine | |
def get(self): | |
# Completely unnecessary, it just brings in a little more code | |
# to make for a more interesting profile. | |
yield gen.Task(IOLoop.instance().add_callback) | |
self.render("hello.html", name="world") | |
class ProfileHandler(RequestHandler): | |
@asynchronous | |
def get(self): | |
self.collector = Collector() | |
self.collector.start() | |
IOLoop.instance().add_timeout(datetime.timedelta(seconds=60), | |
self.finish_profile) | |
def finish_profile(self): | |
self.collector.stop() | |
formatter = PlopFormatter() | |
self.finish(formatter.format(self.collector)) | |
@gen.engine | |
def generate_traffic(): | |
client = AsyncHTTPClient() | |
while True: | |
resp = yield gen.Task(client.fetch, | |
'http://localhost:%d/' % options.port) | |
assert resp.body.strip().decode("utf-8") == 'Hello world!' | |
def main(): | |
parse_command_line() | |
app = Application([ | |
('/', HelloHandler), | |
('/_profile', ProfileHandler), | |
], log_function=lambda x: None) | |
app.listen(options.port, address='localhost') | |
generate_traffic() | |
IOLoop.instance().start() | |
if __name__ == '__main__': | |
main() |