-
Notifications
You must be signed in to change notification settings - Fork 21
/
variadic-test.py
executable file
·492 lines (370 loc) · 12.6 KB
/
variadic-test.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
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
#!/usr/bin/env python
# Script to test how much bloating a large project will suffer when using
# different formatting methods.
# Based on bloat_test.sh from https://github.com/c42f/tinyformat.
from __future__ import print_function, division
import argparse
import os
import pickle
import re
import sys
from glob import glob
from subprocess import check_call, Popen, PIPE
from timeit import timeit
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(help='possible commands', dest='command')
parser_bench = subparsers.add_parser('bench', help='run the benchmark')
parser_bench.add_argument('min', type=int, help='minimum number of arguments')
parser_bench.add_argument('max', type=int, help='maximum number of arguments')
parser_bench.add_argument('num_translation_units', metavar='N', type=int,
help='number of translation units')
parser_plot = subparsers.add_parser('plot', help='plot the results')
parser_plot.add_argument('--filename', type=str, default='variadic-test.pkl',
help='bench result file path')
parser_plotdiff = subparsers.add_parser(
'plotdiff', help='plot the difference between result files'
)
parser_plotdiff.add_argument('files', type=str, nargs='*',
help='result files to be compared')
parser_plotdiff.add_argument('--method', type=str, default='C++ Format',
help='formatting library')
parser_plotdiff.add_argument('--config', type=str, default='optimized',
help='optimized or debug')
options, more_compiler_flags = parser.parse_known_args(sys.argv[1:])
if 'plot' in options.command:
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
prefix = '_variadic_test_tmp_'
NUM_RUNS = 3
use_clobber = False
configs = [
('optimized', ['-O3', '-DNDEBUG']),
('debug', [])
]
methods = [
('printf' , []),
('IOStreams' , ['-DUSE_IOSTREAMS']),
('fmt' , ['-DUSE_FMT', '-Lfmt', '-lfmt']),
('tinyformat' , ['-DUSE_TINYFORMAT']),
('Boost Format', ['-DUSE_BOOST'])
]
method_templates = {
'boost': {
'statement': r'std::cout << boost::format("{fmt_str}\n") % {args};',
'sep': ' % ',
'specifier': '%{type}',
},
'fmt': {
'statement': r'fmt::print("{fmt_str}\n", {args});',
'sep': ', ',
'specifier': '{{:{type}}}',
},
'iostream': {
'statement': r'std::cout << {args} << "\n";',
'sep': ' << ":" << ',
'specifier': '',
},
'tinyformat': {
'statement': r'tfm::printf("{fmt_str}\n", {args});',
'sep': ', ',
'specifier': '%{type}',
},
'printf': {
'statement': r'::printf("{fmt_str}\n", {args});',
'sep': ', ',
'specifier': '%{type}',
}
}
main_template = r'''
#ifdef USE_BOOST
#include <boost/format.hpp>
#include <iostream>
{boost}
#elif defined(USE_FMT)
#include "fmt/format.h"
{fmt}
#elif defined(USE_IOSTREAMS)
#include <iostream>
{iostream}
#elif defined(USE_TINYFORMAT)
#include "tinyformat.h"
{tinyformat}
#else
#include <stdio.h>
{printf}
#endif
'''
def make_format_string(method, args):
specifiers = [method['specifier'].format(type=a[1]) for a in args]
return ':'.join(specifiers)
def make_statement(method, args):
d = {'args': method['sep'].join(a[0] for a in args)}
if method['specifier']:
d['fmt_str'] = make_format_string(method, args)
return method['statement'].format(**d)
def generate_args(start_n):
n = start_n
while True:
args = [(str(n), 'd'),
(str(float(n)), '.1f'),
('"String{}"'.format(n), 's'),
('i', 'd'),
('f', '.1f'),
('s', 's')]
for a in args:
yield a
n += 1
def make_function(func_def, method, n, num_args):
from itertools import islice
mul = 5
args = list(islice(generate_args(n), 2 * mul * num_args))
statements = [make_statement(method, args[shift:shift + num_args])
for shift in range(mul * num_args)]
if use_clobber:
sep = ' asm volatile("" : : : "memory");\n '
else:
sep = '\n '
function = (func_def + ' {{\n'
' {}\n'
'}}').format(sep.join(statements))
return function
def make_template(func_def, n, num_args):
functions = {k: make_function(func_def, v, n, num_args)
for k, v in method_templates.items()}
return main_template.format(**functions)
class Table:
"""Prints a reStructuredText table"""
def __init__(self, header, formats):
self.widths = [len(i) for i in header]
self.formats = formats
self.print_rulers()
for field, width in zip(header, self.widths):
print(self.format_field(field, '', width), end=' ')
print()
self.print_rulers()
@staticmethod
def format_field(field, fmt='', width=''):
return '{:{}{}}'.format(field, width, fmt)
def print_rulers(self):
for w in self.widths:
print('=' * w, end=' ')
print()
def print_row(self, *row):
for field, fmt, width in zip(row, self.formats, self.widths):
print(self.format_field(field, fmt, width), end=' ')
print()
def to_kib(n):
"""Converts n to kibibytes"""
return int(round(n / 1024.0))
def remove_old_files():
filenames = glob(prefix + '??.cc')
for f in [prefix + 'main.cc', prefix + 'all.h']:
if os.path.exists(f):
filenames.append(f)
for f in filenames:
os.remove(f)
def generate_files(num_args):
main_source = prefix + 'main.cc'
main_header = prefix + 'all.h'
sources = [main_source]
with open(main_source, 'w') as cppfile, open(main_header, 'w') as hppfile:
cppfile.write(re.sub('^ +', '', '''\
#include "{}all.h"
#ifdef USE_IOSTREAMS
# include <iostream>
#endif
int main() {{
#ifdef USE_IOSTREAMS
std::cout.setf(std::ios::fixed);
std::cout.precision(1);
#endif
'''.format(prefix), 0, re.MULTILINE))
for i in range(options.num_translation_units):
n = '{:03}'.format(i)
func_name = 'doFormat_a' + n
func_params = '(int i, float f, const char* s)'
func_def = 'void ' + func_name + func_params
source = prefix + n + '.cc'
sources.append(source)
with open(source, 'w') as f:
f.write(make_template(func_def, i, num_args))
cppfile.write(func_name + '(1, 1.0f, "String");\n')
hppfile.write(func_def + ';\n')
cppfile.write('}')
return sources
def find_compiler():
compiler_path = None
for path in os.getenv('PATH').split(os.pathsep):
filename = os.path.join(path, 'g++')
if os.path.exists(filename):
if os.path.islink(filename) and \
os.path.basename(os.path.realpath(filename)) == 'ccache':
# Don't use ccache.
print('Ignoring ccache link at', filename)
continue
compiler_path = filename
break
return compiler_path
def measure_compile(compiler_path, sources, flags):
"""Measure compile time and executable size"""
output_filename = prefix + '.out'
if os.path.exists(output_filename):
os.remove(output_filename)
include_dir = '-I' + os.path.dirname(os.path.realpath(__file__))
command = 'check_call({})'.format(
[compiler_path, '-std=c++11', '-o', output_filename,
include_dir] + sources + flags + more_compiler_flags
)
result = {
'time': timeit(command, number=1,
setup='from subprocess import check_call'),
'size': os.stat(output_filename).st_size
}
check_call(['strip', output_filename])
result['stripped_size'] = os.stat(output_filename).st_size
p = Popen(['./' + output_filename], stdout=PIPE,
env={'LD_LIBRARY_PATH': 'fmt'})
result['output'] = p.communicate()[0]
sys.stdout.flush()
return result
def bench_single(num_args, flags):
remove_old_files()
sources = generate_files(num_args)
compiler_path = find_compiler()
result = {}
for i in range(NUM_RUNS):
sys.stdout.flush()
old_result = result
result = measure_compile(compiler_path, sources, flags)
if 'time' not in old_result:
continue
result['time'] = min(old_result['time'], result['time'])
if any(result[k] != old_result[k] for k in ('size', 'stripped_size')):
raise Exception('size mismatch')
return result
def bench(method, config, flags):
print('Benchmarking', config, method)
table = Table(
['Args', 'Compile time, s', 'Executable size, KiB', 'Stripped size, KiB'],
['', '.1f', '', '']
)
results = []
for num_args in range(options.min, options.max):
result = bench_single(num_args, flags)
table.print_row(num_args, result['time'], to_kib(result['size']),
to_kib(result['stripped_size']))
results.append(result)
table.print_rulers()
print()
return results
def check_output(expected_list, actual_list):
for expected, actual in zip(expected_list, actual_list):
if expected['output'] != actual['output']:
print(expected['output'])
print(actual['output'])
raise Exception("output doesn't match")
def bench_command():
data = {'options': options}
for method, method_flags in methods:
data[method] = {config: bench(method, config, method_flags + config_flags)
for config, config_flags in configs}
if 'printf' in data:
for config, _ in configs:
check_output(data['printf'][config], data[method][config])
for method, _ in methods:
for config, _ in configs:
for result in data[method][config]:
del result['output']
with open('variadic-test.pkl', 'wb') as file:
pickle.dump(data, file)
def load_data(filename):
if not filename.endswith('.pkl'):
filename += '.pkl'
with open(filename, 'rb') as file:
return pickle.load(file)
def set_plot_style():
sns.set_style("ticks", rc={'xtick.direction': 'in', 'ytick.direction': 'in'})
sns.set_palette('Set1')
def plot_title(s):
plt.annotate(s, (0.05, 0.9), xycoords='axes fraction',
fontsize=14, horizontalalignment='left',
bbox=dict(boxstyle="round,pad=0.2", fc='white'))
def plot_subfigure(x, y, prop, **kwargs):
zorder = 3 if kwargs.get('label') == 'C++ Format' else 2
plt.plot(x, y, marker='o', markersize=6, zorder=zorder, **kwargs)
plt.axvline(16, ls='--', color='grey')
xmax = max(x) + 1
plt.xlim(0, xmax)
plt.xticks(np.arange(0, xmax, 4))
plt.grid(color='k', alpha=0.4, ls=':')
sns.despine()
plt.xlabel('number of arguments')
if prop == 'time':
plt.ylabel('compile time (s)')
else:
plt.ylabel('binary size (MiB)')
def plot_all(filename, prop):
data = load_data(filename)
x = np.arange(data['options'].min, data['options'].max)
def make_y(results):
y = [result[prop] for result in results]
if prop == 'size':
y = np.array(y) / 1024 / 1024
return y
set_plot_style()
plt.figure(figsize=(8, 3.5))
plt.subplot('121')
plot_title('release')
for method, _ in methods:
plot_subfigure(x, make_y(data[method]['optimized']), prop, label=method)
plt.legend(loc='upper center', bbox_to_anchor=(1.05, 1.17),
ncol=5, fontsize=11)
plt.subplot('122')
plot_title('debug')
for method, _ in methods:
plot_subfigure(x, make_y(data[method]['debug']), prop, label=method)
plt.suptitle('variadic-test', fontsize=16, y=1.1)
plt.savefig('variadic-test_{}.png'.format(prop), bbox_inches='tight')
def plot_command():
for prop in 'time', 'size':
plot_all(options.filename, prop)
def plot_diff(filenames, method, config, prop):
dataset = [load_data(f) for f in filenames]
baseline = dataset[0][method][config]
x = np.arange(dataset[0]['options'].min, dataset[0]['options'].max)
set_plot_style()
plt.figure(figsize=(8, 3.5))
plt.subplot('121')
plot_title('absolute')
for name, data in zip(filenames, dataset):
y = [result[prop] for result in data[method][config]]
if prop == 'size':
y = np.array(y) / 1024 / 1024
plot_subfigure(x, y, prop, label=name)
plt.legend(loc='upper center', bbox_to_anchor=(1.05, 1.17),
ncol=5, fontsize=11)
plt.subplot('122')
plot_title('relative')
for name, data in zip(filenames, dataset):
results = data[method][config]
y = [result[prop] / base[prop] for result, base in zip(results, baseline)]
plot_subfigure(x, y, prop, label=name)
ylim = plt.ylim()
plt.ylim(ylim[0] * 0.8, ylim[1] * 1.2)
ax = plt.gca()
vals = ax.get_yticks()
ax.set_yticklabels(['{:3.0%}'.format(x) for x in vals])
plt.ylabel('')
plt.suptitle('variadic-diff', fontsize=16, y=1.1)
plt.savefig('variadic-diff_{}.png'.format(prop), bbox_inches='tight')
def plotdiff_command():
for prop in 'time', 'size':
plot_diff(options.files, options.method, options.config, prop)
commands = {
'bench': bench_command,
'plot': plot_command,
'plotdiff': plotdiff_command,
}
if __name__ == '__main__':
commands[options.command]()