-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_profiled_application.py
125 lines (106 loc) · 4.03 KB
/
example_profiled_application.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
from __future__ import absolute_import, print_function
import profilomatic
import profilomatic.monkey_patch
import profilomatic.monitor
import eliot
import datetime
import json
from profilomatic.fast_monotonic import monotonic
from eliot import startAction, Action, FileDestination
from threading import Thread
from prometheus_client.exposition import make_wsgi_app
try:
from urllib2 import urlopen, Request
except ImportError:
from urllib.request import urlopen, Request
from wsgiref.simple_server import make_server, WSGIServer
from wsgiref.util import shift_path_info
try:
from SocketServer import ThreadingMixIn
except ImportError:
from socketserver import ThreadingMixIn
import sys
import time
try:
from profilomatic._call_graph import CallGraphRoot
from profilomatic._stack_trace import generate_stack_trace
except ImportError:
from profilomatic.call_graph import CallGraphRoot
from profilomatic.stack_trace import generate_stack_trace
def ping(environ, start_response):
with startAction(action_type='ping') as action:
for i in range(1000):
req = Request(
'http://localhost:8090/pong',
headers={'X-Eliot-Context': action.serialize_task_id().decode('ascii')})
response = urlopen(req)
response.read()
response.close()
start_response('200 OK', [('Content-Type', 'text/plain')])
return [b'OK']
def pong(environ, start_response):
with Action.continueTask(
task_id=environ['HTTP_X_ELIOT_CONTEXT'].encode('ascii')) as action:
time.sleep(0.2)
start_response('200 OK', [('Content-Type', 'text/plain')])
return [b'OK']
def app(environ, start_response):
path = shift_path_info(environ)
if path == 'ping':
return ping(environ, start_response)
elif path == 'pong':
return pong(environ, start_response)
elif path == 'metrics':
return make_wsgi_app()(environ, start_response)
else:
start_response('404 Not Found', [])
return 'Not Found'
class ThreadingWSGIServer(ThreadingMixIn, WSGIServer):
daemon_threads = True
# profilomatic.configure(max_overhead=0.05, code_granularity='line', simultaneous_tasks_profiled=15, time_granularity=0.05)
# import socket
# s = socket.socket()
# s.connect(('127.0.0.1', 54637))
# profilomatic.add_destination(FileDestination(s.makefile()))
# profilomatic.add_destination(FileDestination(open('profile.log', 'wb')))
eliot.add_destination(FileDestination(open('app.log', 'w')))
# profilomatic.monkey_patch.patch()
# profilomatic.monitor.enable_prometheus()
if __name__ == '__main__':
server = make_server('', 8090, app, server_class=ThreadingWSGIServer)
server_thread = Thread(target=server.serve_forever, args=[0.1])
server_thread.start()
worker_threads = []
for i in range(5):
worker = Thread(target=urlopen, args=['http://localhost:8090/ping'])
worker.start()
worker_threads.append(worker)
# profiler_thread_id = profilomatic._instance.thread.ident
# profiler_callgraph = CallGraphRoot(
# profiler_thread_id,
# 'profile',
# datetime.datetime.now(), seconds=monotonic())
#
# def profile_profiler():
# before = monotonic()
# while True:
# time.sleep(0.01)
# frame = sys._current_frames()[profiler_thread_id]
# stack = generate_stack_trace(frame, 'line', False)
# after = monotonic()
# profiler_callgraph.ingest(stack, after - before, after)
# before = after
#
# profiler_profiler_thread = Thread(target=profile_profiler)
# profiler_profiler_thread.setDaemon(True)
# profiler_profiler_thread.start()
for thread in worker_threads:
thread.join()
server.shutdown()
server_thread.join()
print('Finished!')
time.sleep(0.1)
# with open('profiler_callgraph.json', 'w') as f:
# f.write(json.dumps(profiler_callgraph.jsonize(), indent=2))
# import prometheus_client
# print(prometheus_client.generate_latest())