-
Notifications
You must be signed in to change notification settings - Fork 69
/
busy_server.py
56 lines (47 loc) · 1.69 KB
/
busy_server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/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()