-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark.py
More file actions
387 lines (316 loc) · 13.4 KB
/
benchmark.py
File metadata and controls
387 lines (316 loc) · 13.4 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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
import os
import sys
import time
import subprocess
import argparse
import glob
import psutil
import shlex
import logging
from pathlib import Path
import numpy as np
import pandas as pd
import json
from tqdm import tqdm
from utils.monitors import ResourceMonitor, RAPLMonitor, PCMMonitor
MIN_RUNTIME = 5
class PipelineError(Exception):
"""Raised when the gst pipeline won't preroll"""
pass
def run(cmdline, cores_per_proc=1, procs=1, monitors=None, timeout=None, environ=None, retries=5):
if isinstance(cmdline, str):
cmdline = shlex.split(cmdline)
if cores_per_proc is None: # Don't set cpu affinity
cpusets = None
else:
cpusets = []
for i in range(procs):
cpuid_down = cores_per_proc*i
cpuid_up = cores_per_proc*(i+1)
proc_cpuset = list(np.arange(cpuid_down, cpuid_up, 1))
cpusets.append(proc_cpuset)
while retries > 0:
try:
for monitor in monitors:
monitor.start(interval=1.0)
subprocs = []
for i in range(procs):
if environ is not None:
os.environ['GST_DEBUG'] = environ
subproc = subprocess.Popen(
cmdline,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
if environ is not None:
del os.environ['GST_DEBUG']
environ = None
if cpusets is not None:
proc = psutil.Process(pid=subproc.pid)
proc.cpu_affinity(cpusets[i])
subprocs.append(subproc)
error_found = False
for i in range(procs):
out, err = subprocs[i].communicate(timeout=timeout)
out = out.decode('utf-8')
err = err.decode('utf-8')
subprocs[i] = [out, err]
if 'ERROR' in err:
error_found = True
print(err)
if not error_found:
for monitor in monitors:
monitor.stop(checkpoint=True)
return subprocs
except:
raise
for monitor in monitors:
monitor.stop(checkpoint=False)
retries = retries - 1
# if we reach this point, retries = 0
raise PipelineError("Command failed all retries: {}".format(' '.join(cmdline)))
def get_inputs(input_path, benchmark):
if benchmark == 'decoding':
extensions = ['.mp4', '.webm']
elif benchmark == 'inference':
extensions = ['.xml']
inputs = []
if any(ext in input_path for ext in extensions):
if not os.path.isfile(args.input):
raise ValueError('{} does not exist.'.format(args.input))
inputs = [Path(input_path)]
elif os.path.isdir(input_path):
inputs = []
for ext in extensions:
inputs.extend([f for f in Path(input_path).glob('*{}'.format(ext))])
else:
raise ValueError(
'{} is not a valid directory nor a valid input file.'.format(input_path)
)
return inputs
def get_video_info(video):
ffprobe = 'ffprobe -loglevel quiet -print_format json -show_format -show_streams {}'
ffproc = subprocess.Popen(
shlex.split(ffprobe.format(str(video))),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
out, err = ffproc.communicate()
video_info = json.loads(out)
codec = video_info['streams'][0]['codec_name']
resolution = video_info['streams'][0]['height']
fr, div = video_info['streams'][0]['avg_frame_rate'].split('/')
frame_rate = float(fr) / float(div)
bitrate = int(video_info['streams'][0]['bit_rate'])
duration = float(video_info['streams'][0]['duration'])
if codec == 'hevc':
codec = 'h265'
return [codec, resolution, frame_rate, bitrate, duration]
def decoding(args):
if args.prefix:
output_file = args.output + '/' + args.prefix + '-'
else:
output_file = args.output + '/'
inputs = get_inputs(args.input, benchmark=args.benchmark)
cpu_count = psutil.cpu_count(logical=False)
if args.config is not None:
configs = pd.read_csv(args.config)
else:
default_config = [[cpu_count, 1]]
configs = pd.DataFrame(default_config, columns=['cores', 'procs'])
#System monitors
cpu_monitor = ResourceMonitor()
rapl_monitor = RAPLMonitor()
pcm_monitor = PCMMonitor()
monitors = [cpu_monitor, rapl_monitor, pcm_monitor]
proc = psutil.Process()
benchmark_stats = []
benchmark_metrics = ['Video', 'CPUs', 'Procs', 'Device', 'Sync', 'Codec', 'Bitrate', 'Resolution', 'Throughput']
benchmark_metrics += ['Latency Avg', 'Latency Max', 'Latency Min', 'Latency 95%', 'Latency 99%', 'Latency Median', 'Latency StdDev']
configs_per_video = len(configs)
if not args.no_latency:
configs_per_video = configs_per_video * 2
total_runs = len(inputs) * configs_per_video
gst_pipeline = 'gst-launch-1.0 {} filesrc location={} ! qtdemux ! {} {}parse ! {} {} ! fakesink sync={}'
with tqdm(total=total_runs, desc="Total runs") as pbar:
for video in tqdm(inputs, desc='Videos to decode', total=len(inputs)):
video_name = video.stem
codec, resolution, frame_rate, bitrate, video_duration = get_video_info(video)
if args.device.lower() == 'cpu':
decoder = f'avdec_{codec}'
else:
decoder = f'vaapi{codec}dec'
gst_throughput = gst_pipeline.format('', str(video), '', codec, decoder, '', str(args.sync))
gst_latency = gst_pipeline.format(f'--gst-plugin-path={args.plugin}', str(video), 'markin name=moo !', codec, decoder, '! markout', str(args.sync))
for _, cores, procs in tqdm(configs.itertuples(), desc='Runs with {}'.format(video_name), total=configs_per_video, leave=False):
if not isinstance(cores, int):
cores = cpu_count
if not isinstance(procs, int):
procs = 1
if cores * procs > cpu_count:
cores = None # Don't set cpu affinity
# 1. First run to get throughput and telemetry
try:
outputs = run(
gst_throughput,
cores_per_proc=cores,
procs=procs,
monitors=monitors,
timeout=args.time
)
except PipelineError as e:
logging.warning('Skipping experiment with video {} and {} procs with {} cpus'.format(video_name, procs, cores))
continue
except:
# Save work
logging.error("Benchmark failed with pipeline: {}".format(gst_latency))
logging.error("Saving current work...")
df = pd.DataFrame(benchmark_stats, columns=benchmark_metrics)
df.to_csv('{}summary.csv.bak'.format(output_file), sep=',', index=False, float_format='%.3f')
df = pd.concat([df, cpu_monitor.checkpoints, rapl_monitor.checkpoints, pcm_monitor.checkpoints], axis=1, sort=False)
df.to_csv('{}detailed.csv.bak'.format(output_file), sep=',', index=False, float_format='%.3f')
raise
# raise SystemExit('Exiting benchmark')
runtimes = []
throughputs = []
for output in outputs:
out = output[0]
err = output[1]
runtime = None
for line in out.split('\n')[-10:]:
if 'Execution ended after' in line:
runtime = line.split(' ')[-1]
hours, minutes, seconds = runtime.split(':')
runtime = int(hours)*3600 + int(minutes)*60 + float(seconds)
relative_speed = video_duration / runtime
decoding_fps = frame_rate * relative_speed
if runtime < MIN_RUNTIME:
logging.warning("Runtime is too low for meaningful telemetry (runtime: {})".format(runtime))
runtimes.append(runtime)
throughputs.append(decoding_fps)
# Average runtime and throughput
avg_runtime = np.mean(np.array(runtimes))
avg_fps = np.mean(np.array(throughputs))
# 2. Second run to only get latency, unless otherwise specified
if not args.no_latency:
pbar.update(1)
try:
outputs = run(
gst_latency,
cores_per_proc=cores,
procs=procs,
monitors=monitors,
timeout=args.time,
environ='markout:5'
)
except:
# Save work
logging.error("Benchmark failed with pipeline: {}".format(gst_latency))
logging.error("Saving current work...")
df = pd.DataFrame(benchmark_stats, columns=benchmark_metrics)
df.to_csv('{}summary.csv.bak'.format(output_file), sep=',', index=False, float_format='%.3f')
df = pd.concat([df, cpu_monitor.checkpoints, rapl_monitor.checkpoints, pcm_monitor.checkpoints], axis=1, sort=False)
df.to_csv('{}detailed.csv.bak'.format(output_file), sep=',', index=False, float_format='%.3f')
raise
raise SystemExit('Exiting benchmark')
latency_stats = []
# Only interested in the output of the first instance
out = outputs[0][0]
err = outputs[0][1]
frame_latencies = []
for line in err.split('\n'):
if 'Mark Duration' not in line:
continue
duration = float(line.split(':')[-1].strip().replace('ms',''))
frame_latencies.append(duration)
lat = np.array(frame_latencies)
latency_stats = [
lat.mean(),
lat.min(),
lat.max(),
np.percentile(lat, 95),
np.percentile(lat, 99),
np.median(lat),
np.std(lat)
]
else:
latency_stats = [0, 0, 0, 0, 0, 0]
if cores is None:
cores = cpu_count
stats = [video_name, cores, procs, args.device, args.sync, codec, bitrate, resolution, avg_fps ] + latency_stats
benchmark_stats.append(stats)
pbar.update(1)
df = pd.DataFrame(benchmark_stats, columns=benchmark_metrics)
df.to_csv('{}summary.csv'.format(output_file), sep=',', index=False, float_format='%.3f')
df = pd.concat([df, cpu_monitor.checkpoints, rapl_monitor.checkpoints, pcm_monitor.checkpoints], axis=1, sort=False)
df.to_csv('{}detailed.csv'.format(output_file), sep=',', index=False, float_format='%.3f')
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
"-b", "--bin",
help="Path to the benchmark app binary",
type=str,
default='./bin/benchmark_app'
)
parser.add_argument(
"--benchmark",
help="Benchmark: [decoding, inference]",
type=str,
default='decoding'
)
parser.add_argument(
"-d", "--device",
help="Device to run the benchmark.",
type=str,
default='CPU'
)
parser.add_argument(
"-c", "--config",
help="Config file from which configurations of {cores, requests, streams, and batch size} are read.",
type=str,
)
parser.add_argument(
"-o", "--output",
help="Path to output directory file where results are stored.",
default="./results/",
type=str
)
parser.add_argument(
"--prefix",
help="Prefix to name the different output files generated.",
type=str,
default=None
)
parser.add_argument(
"-t", "--time",
help="Duration of each experiment.",
type=int,
default=None
)
parser.add_argument(
"-i", "--input",
help="Path to the input video or directory containing the videos.",
type=str,
)
parser.add_argument(
"--plugin",
help="GST_PLUGIN_PATH (directory where latency pluting is).",
type=str,
)
parser.add_argument(
"--sync",
help="Synchronous decoding (decoding rate locked at input's framerate).",
default=False,
action='store_true'
)
parser.add_argument(
"--no-latency",
help="Skip the second run to get latency.",
default=False,
action='store_true'
)
args = parser.parse_args()
logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.DEBUG)
decoding(args)
if __name__ == '__main__':
main()