-
Notifications
You must be signed in to change notification settings - Fork 0
/
profiletk.py
289 lines (227 loc) · 10.4 KB
/
profiletk.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
from pyinstrument import Profiler
from line_profiler import LineProfiler
import memory_profiler
import tracemalloc
from pycallgraph import PyCallGraph
from pycallgraph.output import GraphvizOutput
from pycallgraph import Config
import pandas as pd
import numpy as np
from statistics import mean
import os
import linecache
class ProfileTK:
"""
ProfileTK (a profiling toolkit) creates a performance object
This package combines pyinstrument, line_profiler, memory_profiler, tracemalloc, and pycallgraph
in order to provide a seemless and constant interface between them. We provide timing profiles
that can be analyized across different parameters, standalone timing profiles by function call
and line by line, memory profiles by function call and line by line, and call graphs color coded
by runtime costs.
"""
def __init__(self):
"""
Initialize
Variables
---------
profiles : dict
The full profiles of each pyinstrument call within the collect_functional_times function.
Key: the key argument passed to collect_functional_times. Value: The text profile.
timingsDF : pandas.DataFrame
The profiling information gathered by calling pyinstrument within the collect_functional_times
function. Columns consist of the key and function names.
Profiler : pyinstrument.Profiler
The Profiler object within pyinstument.
index : int, private
A running index for the timingsDF dataframe.
"""
self.profiles = {}
self.timingsDF = pd.DataFrame()
self._profiler = Profiler()
self._index = 0
def collect_functional_times(self, key: str, func: callable, *args, **kwargs):
"""
Profiles a function with pyinstrument and populate the new timings into the dataframe.
Parameters
----------
key : str
The unique identifier asigned to this new entry within the dataframe.
func : callable
The function to profile.
args : list, optional
A list of arguments to pass to the function that is being profiled.
kwargs : dict, optional
A dictionary of keyword, value arguments to pass to the function that is being profiled.
"""
# start profiler, run function, and stop profiler
self._profiler.start()
func(*args, **kwargs)
self._profiler.stop()
timers = {}
local_timings = {}
# get profile results in a text format
results = (self._profiler.output_text(unicode=True, show_all=True))
# store the profile for future inqueries
self.profiles[key] = results
# parse the profile into a dictionary
start = False
for line in results.splitlines():
if start and 'module' not in line and 'self' not in line and '__' not in line and len(line.split())>3:
ls = line.split('─ ')[1]
t = ls.split()[1]
if t in timers.keys():
timers[t] += float(ls.split()[0])
else:
timers[t] = float(ls.split()[0])
if ('Program:' in line):
start = True
local_timings[self._index] = {
'key': key
}
for k,v in timers.items():
local_timings[self._index][k] = v
self._index += 1
# store prfile into the dataframe
df = pd.DataFrame(local_timings).T
self.timingsDF = self.timingsDF.append(df).reset_index(drop=True).fillna(0.0)
def print_timer_hotspots(self, key: str, l=25):
"""
Prints the function names within the specified profile in descenting oder based on run time.
This function prints the rank, the run time, the function name, file name and line number, and
the call tree depth. This function uses the timing information provided by pyinstrument gathered
from the call to collect_functional_times.
Parameters
----------
key : str
The unique identifier of a profile that was set within the call to collect_functional_times.
l : int
The number of functions to print.
"""
values = {}
# parse the saved profile to determine call tree depth and timings of each function
for line in self.profiles[key].splitlines():
if '─ ' in line:
depth = int((len(line.split('─ ')[0])-1)/3)
values[line.split('─ ')[1]+' call tree depth: '+str(depth)] = float(line.split('─ ')[1].split()[0])
# sort into descending order and print
sorted_values = sorted(values, reverse=True)[:l]
for i,val in enumerate(sorted_values,1):
print("#%s: %s" % (i,val))
def collect_linebyline_times(self, func: callable, o_funcs: list, *args, **kwargs):
"""
Calls the line_profiler utility to collect line by line timing information for specified functions.
New information is gathered each time this function is called so some variability exists between calls.
Parameters
----------
func : callable
The function to profile.
o_funcs : list
A list of additional functions to profile. These must be in the call tree of the function
specified in the func argument.
args : list, optional
A list of arguments to pass to the function that is being profiled.
kwargs : dict, optional
A dictionary of keyword, value arguments to pass to the function that is being profiled.
"""
lp = LineProfiler()
for f in o_funcs:
lp.add_function(f)
lp_wrapper = lp(func)
lp_wrapper(*args, **kwargs)
lp.print_stats()
def print_memory_hotspots(self, func: callable, l=10, *args, **kwargs):
"""
Prints the memory hotspots within the selected function by calling tracemalloc in descending order.
This function prints the rank, the location of the code, size, and line of code. New information
is gathered each time this function is called so some variability may exist between calls.
Parameters
----------
func : callable
The function to profile.
l : int
The number of hotspots to print.
args : list, optional
A list of arguments to pass to the function that is being profiled.
kwargs : dict, optional
A dictionary of keyword, value arguments to pass to the function that is being profiled.
"""
# start the profile, run the function, stop the profile
tracemalloc.start()
func(*args, **kwargs)
trace = tracemalloc.take_snapshot()
tracemalloc.stop()
# filter out calls we don't want to report
trace = trace.filter_traces((
tracemalloc.Filter(False, "<frozen importlib._bootstrap>"),
tracemalloc.Filter(False, "<unknown>"),
))
# get the trace
top = trace.statistics('lineno')
# format the print for the selected number of lines to report
for i,stat in enumerate(top[:l],1):
frame = stat.traceback[0]
print("#%s: %s:%s: %.1f KiB" % (i, frame.filename, frame.lineno, stat.size / 1024))
line = linecache.getline(frame.filename, frame.lineno).strip()
if line:
print(' '+line)
# print the total amount allocated to show relational value
total = sum(stat.size for stat in top)
print("Total amount allocated: %.1f KiB" % (total / 1024))
def collect_linebyline_memory_usage(self, func: callable, o_funcs: list, *args, **kwargs):
"""
Calls the memory_profiler utility to collect line by line memory information for specified functions.
New information is gathered each time this function is called so some variability exists between calls.
Parameters
----------
func : callable
The function to profile.
o_funcs : list
A list of additional functions to profile. These must be in the call tree of the function
specified in the func argument.
args : list, optional
A list of arguments to pass to the function that is being profiled.
kwargs : dict, optional
A dictionary of keyword, value arguments to pass to the function that is being profiled.
"""
prof = memory_profiler.LineProfiler(backend='psutil')
for f in o_funcs:
prof.add_function(f)
val = prof(func)(*args, **kwargs)
memory_profiler.show_results(prof, stream=None, precision=1)
def collect_memory_usage(self, func: callable, *args, **kwargs):
"""
Calls the memory_profiler utility to collect the high water memory information for the specified function.
New information is gathered each time this function is called so some variability exists between calls.
Parameters
----------
func : callable
The function to profile.
args : list, optional
A list of arguments to pass to the function that is being profiled.
kwargs : dict, optional
A dictionary of keyword, value arguments to pass to the function that is being profiled.
"""
mem_usage = memory_profiler.memory_usage((func, args, kwargs))
print (mean(mem_usage))
def show_call_graph(self, func: callable, fn: str, *args, **kwargs):
"""
Creates a call graph for the speficed function that is color coded to indicate which functions
take longer to run.
This requires an install of graphviz (https://www.graphviz.org/).
Parameters
----------
func : callable
The function to profile.
fn : str
The name of the filename to save the graph image to.
args : list, optional
A list of arguments to pass to the function that is being profiled.
kwargs : dict, optional
A dictionary of keyword, value arguments to pass to the function that is being profiled.
"""
graphviz = GraphvizOutput()
graphviz.output_file = fn
config = Config()
config.include_stdlib = True
with PyCallGraph(config=config, output=graphviz):
func(*args, **kwargs)