-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathperformance_test.py
More file actions
307 lines (260 loc) · 9.81 KB
/
Copy pathperformance_test.py
File metadata and controls
307 lines (260 loc) · 9.81 KB
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Performance Tool for SocRocket
==============================
Requirements
------------
Please enable FlameGraph and gprof2dot in core/waf beforehand
Usage
-----
Just call it as an application
"""
from __future__ import print_function
import os
import sys
import subprocess
# from usi.tools.waf import OBJECT as waf_config # Does not work
sys.path.insert(0, os.path.abspath('pysc/usi/tools'))
# pylint: disable=import-error,wrong-import-position
from waf import OBJECT as waf_config
__author__ = "Luise Moritz <luise.moritz@tu-braunschweig.de>"
__copyright__ = "Copyright 2018, SoCRocket TLM Framework"
__license__ = "GPL"
__version__ = "1.0.1"
APP_DESCRIPTION = """
SocRocket performance test tool
-------------------------------
For this script you need to install:
perf (linux generic tools),
graphviz (dot),
sudo,
c++filt[, gprof2dot, FlameGraph]
- FlameGraph is part of /core/waf
- gprof2dot is part of usi's virtualenv
"""
FILENAME_DATA = "{}.perf.data"
FILENAME_PERF = "{}.perf"
FILENAME_FOLDED = "{}.perf.folded"
FILENAME_SVG = "{}.perf.flamegraph.svg"
FILENAME_CALL = "{}.perf.callgraph.png"
FILENAME_REPORT = "{}.perf.report.txt"
FILENAME_STATS = "{}.perf.stat.txt"
FILENAME_AVG_TIME = "{}.perf.time.txt"
OUT_DIR = waf_config['out_dir']
VENV_DIR = os.path.join(OUT_DIR, '.conf_check_venv', 'bin')
DIST_DIR = os.path.join(OUT_DIR, '.conf_check_deps', 'dist')
FGRAPH_DIR = os.path.join(DIST_DIR, 'FlameGraph-1.0')
STACKCOLLAPSE = os.path.join(FGRAPH_DIR, 'stackcollapse-perf.pl')
FLAMEGRAPH = os.path.join(FGRAPH_DIR, 'flamegraph.pl')
GPROF2DOT = os.path.join(VENV_DIR, 'gprof2dot')
SUDO = 'sudo'
PERF = 'perf'
CFILT = 'c++filt'
DOT = 'dot'
def sudo(as_root, cmd):
"""
Add sudo to a command if as_root is True.
Parameter
---------
as_root: boolean
cmd: list of strings
"""
if as_root:
return [SUDO] + cmd
return cmd
class PerformanceTest(object):
"""Wrapper class for the perf command and some tools."""
def __init__(self, name, root, command, loop=1):
"""Init member of Class."""
self.name = name
self.root = root
self.command = command
self.loop = loop
def _flame(self):
"""Generate a FlameGraph of perf.data.
Enable FlameGraph in wscript of core/waf beforehand.
"""
with open(FILENAME_FOLDED.format(self.name), "w") as flame_folded_file:
print("Generating FlameGraph of {}:".format(self.name))
print(" Fold Report Data")
cmd = [STACKCOLLAPSE, FILENAME_PERF.format(self.name)]
print(' '.join(cmd))
fg_p3 = subprocess.Popen(cmd, stdout=flame_folded_file)
fg_p3.wait()
with open(FILENAME_SVG.format(self.name), "w") as flame_image_file:
print(" Generate Image")
fg_p4 = subprocess.Popen(
[FLAMEGRAPH, FILENAME_FOLDED.format(self.name)],
stdout=flame_image_file)
fg_p4.wait()
def _dot(self):
"""
Generate a DotGraph.
Enable gprof2dot in pysc/usi/wscript beforehand
"""
with open(FILENAME_PERF.format(self.name), "r") as dot_perf_file:
print("Generating Dot Graph of {}:".format(self.name))
cg_p3 = subprocess.Popen([
GPROF2DOT, # creates a dot graph
"-f", "perf", # format perf
"--strip", # strip funktction parameters
"--wrap" # wrap function names
], stdin=dot_perf_file, stdout=subprocess.PIPE)
subprocess.Popen(
[DOT, '-Tpng', '-o', FILENAME_CALL.format(self.name)],
stdin=cg_p3.stdout)
def _txt(self):
"""
Save Results in a Textfile.
Sorted by command and name of library.
"""
print("Generating Report txt")
for rootuser in [self.root, True]:
with open(FILENAME_REPORT.format(self.name), "w") as txt_file:
txt_p1 = subprocess.Popen(sudo(rootuser, [
"perf",
"report",
"--input={}".format(FILENAME_DATA.format(self.name)),
"-I", # show CPU information in header
"--header", # show header
"--sort",
"comm,dso"]), stdout=subprocess.PIPE)
txt_p2 = subprocess.Popen(
"c++filt",
stdin=txt_p1.stdout,
stdout=txt_file)
txt_p2.wait()
if os.stat(FILENAME_REPORT.format(self.name)).st_size != 0:
break
# Saves results to textfile
def _txt_stat(self):
"""
Measure Execution Time.
Extract Execution Time of File.
"""
result = 0.0
print("Generating statistics with execution time")
perf_record = sudo(self.root, [
PERF,
'stat',
'-o',
FILENAME_STATS.format(self.name),
'-e' 'cpu-clock'
] + self.command)
print(" ".join(perf_record))
txt_p1 = subprocess.Popen(perf_record)
txt_p1.wait()
lines = []
with open(FILENAME_STATS.format(self.name), "r") as cg_report_file:
lines = cg_report_file.readlines()
for i, line in enumerate(lines):
if 'seconds time elapsed' in line and i+1 < len(lines):
seconds = lines[i]
start = seconds.find(',')
end = seconds.find('seconds time elapsed')
# extract number of seconds in line
seconds = seconds[start-1:end-1]
# replace , to . to convert to float
seconds = seconds.replace(',', '.')
# add to array
result = float(seconds)
print("Execution Time: {}s".format(seconds))
break
return result
def _collect(self):
"""Collect performance data."""
print("Collect performance data:")
perf_record = sudo(self.root, [
PERF,
'record',
'--output={}'.format(FILENAME_DATA.format(self.name)),
'--call-graph=dwarf',
'--freq=99',
'--'
] + self.command)
print(" ".join(perf_record))
pt_p0 = subprocess.Popen(perf_record)
pt_p0.wait()
def _prepare(self):
"""Prepare performance data."""
print("Prepare performance data")
with open(FILENAME_PERF.format(self.name), "w") as name_perf_file:
# If script is run as root
# converts perf.data into readable file
pt_p1 = subprocess.Popen(sudo(self.root, [
PERF,
'script',
'--input={}'.format(FILENAME_DATA.format(self.name))
]), stdout=subprocess.PIPE)
pt_p2 = subprocess.Popen([CFILT],
stdin=pt_p1.stdout,
stdout=name_perf_file)
pt_p2.communicate()
pt_p2.wait()
def _check(self):
"""Check if data file exists."""
return os.stat(FILENAME_DATA.format(self.name)).st_size > 0
def _cleanup(self):
"""Clean up data files."""
print("Clean up data files")
os.remove(FILENAME_DATA.format(self.name))
os.remove(FILENAME_PERF.format(self.name))
def averagetime(self):
"""Sample execution time and calculate its average time."""
exec_time = []
loop = 1
if self.loop:
loop = self.loop
for _ in range(0, loop, 1):
exec_time.append(self._txt_stat())
exec_time_avg = sum(exec_time)/loop
exec_time_avg_print = "Average Execution Time of {} Executions: "\
"{} seconds" \
.format(loop, exec_time_avg)
print(exec_time_avg_print)
with open(FILENAME_AVG_TIME.format(self.name), "w") as avg_time_file:
for item in exec_time:
avg_time_file.write("%s,\n" % item)
avg_time_file.write(exec_time_avg_print)
os.remove(FILENAME_STATS.format(self.name))
def test(self):
"""Start performance test."""
self._collect()
# If performance data were collected
if self._check():
self._prepare()
if os.stat(FILENAME_PERF.format(self.name)).st_size == 0:
print(FILENAME_PERF.format(self.name), "not found")
print("Performance data cannot be analysed.")
else:
# post_processing
self._flame()
self._dot()
self._txt()
self._cleanup()
else:
print("No samples recorded")
def main():
"""Run performance test as a cli command."""
import argparse
from argparse import RawTextHelpFormatter
parser = argparse.ArgumentParser(description=APP_DESCRIPTION,
formatter_class=RawTextHelpFormatter)
parser.add_argument('-s', '--sudo', dest='root', action='store_true',
default=False,
help="run this script as root")
parser.add_argument('-n', '--name', dest='name', default=None,
help="basename of the outputfiles")
parser.add_argument('-l', '--loop', type=int,
help="increase output verbosity")
parser.add_argument('--', dest='orig_args', nargs=argparse.REMAINDER)
argv = sys.argv[1:]
idx = argv.index('--') if '--' in argv else len(argv)
args, extra = parser.parse_args(argv[:idx]), argv[idx + 1:]
perf = PerformanceTest(args.name or os.path.basename(extra[0]),
args.root, extra, args.loop)
perf.test()
perf.averagetime()
if __name__ == "__main__":
main()