-
Notifications
You must be signed in to change notification settings - Fork 22
/
bloat-test.py
executable file
·281 lines (246 loc) · 8.2 KB
/
bloat-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
#!/usr/bin/env python3
# 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
import os, re, sys
from contextlib import ExitStack
from glob import glob
from subprocess import check_call, Popen, PIPE, CalledProcessError
from timeit import timeit
template = r'''
#ifdef USE_BOOST
#include <boost/format.hpp>
#include <iostream>
void doFormat_a() {
std::cout << boost::format("%s\n") % "somefile.cpp";
std::cout << boost::format("%s:%d\n") % "somefile.cpp" % 42;
std::cout << boost::format("%s:%d:%s\n") % "somefile.cpp" % 42 % "asdf";
std::cout <<
boost::format("%s:%d:%d:%s\n") % "somefile.cpp" % 42 % 1 % "asdf";
std::cout <<
boost::format("%s:%d:%d:%d:%s\n") % "somefile.cpp" % 42 % 1 % 2 % "asdf";
}
#elif USE_FOLLY
#include <folly/Format.h>
#include <iostream>
void doFormat_a() {
std::cout << folly::format("{}\n", "somefile.cpp");
std::cout << folly::format("{}:{}\n", "somefile.cpp", 42);
std::cout << folly::format("{}:{}:{}\n", "somefile.cpp", 42, "asdf");
std::cout <<
folly::format("{}:{}:{}:{}\n", "somefile.cpp", 42, 1, "asdf");
std::cout <<
folly::format("{}:{}:{}:{}:{}\n", "somefile.cpp", 42, 1, 2, "asdf");
}
#elif defined(USE_FMT)
#include "fmt/base.h"
void doFormat_a() {
fmt::print("{}\n", "somefile.cpp");
fmt::print("{}:{}\n", "somefile.cpp", 42);
fmt::print("{}:{}:{}\n", "somefile.cpp", 42, "asdf");
fmt::print("{}:{}:{}:{}\n", "somefile.cpp", 42, 1, "asdf");
fmt::print("{}:{}:{}:{}:{}\n", "somefile.cpp", 42, 1, 2, "asdf");
}
#elif defined(USE_IOSTREAMS)
#include <iostream>
void doFormat_a() {
std::cout << "somefile.cpp" << "\n";
std::cout << "somefile.cpp:" << 42 << "\n";
std::cout << "somefile.cpp:" << 42 << ":asdf" << "\n";
std::cout << "somefile.cpp:" << 42 << ':' << 1 << ":asdf" << "\n";
std::cout << "somefile.cpp:" << 42 << ':' << 1 << ':' << 2 << ":asdf" << "\n";
}
#elif defined(USE_STB_SPRINTF)
#ifdef FIRST_FILE
# define STB_SPRINTF_IMPLEMENTATION
#endif
// since this test doesn't use floating point numbers shave ~20kb
#define STB_SPRINTF_NOFLOAT
#include "src/stb_sprintf.h"
#include <stdio.h>
void doFormat_a() {
char buf[100];
stbsp_sprintf(buf, "%s\n", "somefile.cpp");
fputs(buf, stdout);
stbsp_sprintf(buf, "%s:%d\n", "somefile.cpp", 42);
fputs(buf, stdout);
stbsp_sprintf(buf, "%s:%d:%s\n", "somefile.cpp", 42, "asdf");
fputs(buf, stdout);
stbsp_sprintf(buf, "%s:%d:%d:%s\n", "somefile.cpp", 42, 1, "asdf");
fputs(buf, stdout);
stbsp_sprintf(buf, "%s:%d:%d:%d:%s\n", "somefile.cpp", 42, 1, 2, "asdf");
fputs(buf, stdout);
}
#else
# ifdef USE_TINYFORMAT
# include "src/tinyformat.h"
# define PRINTF tfm::printf
# else
# include <stdio.h>
# define PRINTF ::printf
# endif
void doFormat_a() {
PRINTF("%s\n", "somefile.cpp");
PRINTF("%s:%d\n", "somefile.cpp", 42);
PRINTF("%s:%d:%s\n", "somefile.cpp", 42, "asdf");
PRINTF("%s:%d:%d:%s\n", "somefile.cpp", 42, 1, "asdf");
PRINTF("%s:%d:%d:%d:%s\n", "somefile.cpp", 42, 1, 2, "asdf");
}
#endif
'''
prefix = '/tmp/_bloat_test_tmp_'
num_translation_units = 100
# 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)
# Generate all the files.
main_source = prefix + 'main.cc'
main_header = prefix + 'all.h'
sources = [main_source]
with ExitStack() as stack:
main_file = stack.enter_context(open(main_source, 'w'))
header_file = stack.enter_context(open(main_header, 'w'))
main_file.write(re.sub('^ +', '', '''
#include "{}all.h"
int main() {{
'''.format(prefix), 0, re.MULTILINE))
for i in range(num_translation_units):
n = '{:03}'.format(i)
func_name = 'doFormat_a' + n
source = prefix + n + '.cc'
sources.append(source)
with open(source, 'w') as f:
if i == 0:
f.write('#define FIRST_FILE\n')
f.write(template.replace('doFormat_a', func_name).replace('42', str(i)))
main_file.write(func_name + '();\n')
header_file.write('void ' + func_name + '();\n')
main_file.write('}')
# 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
print('Using compiler', filename)
class Result:
pass
# Measure compile time and executable size.
expected_output = None
def benchmark(flags):
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++14', '-o', output_filename, include_dir] + sources + flags)
result = Result()
try:
result.time = timeit(
command, setup = 'from subprocess import check_call', number = 1)
except CalledProcessError:
return None
print('Compile time: {:.2f}s'.format(result.time))
result.size = os.stat(output_filename).st_size
print('Size: {}'.format(result.size))
check_call(['strip', output_filename])
result.stripped_size = os.stat(output_filename).st_size
print('Stripped size: {}'.format(result.stripped_size))
p = Popen([output_filename], stdout=PIPE,
env={'LD_LIBRARY_PATH': 'fmt', 'DYLD_LIBRARY_PATH': 'fmt'})
output = p.communicate()[0]
global expected_output
if not expected_output:
expected_output = output
elif output != expected_output:
print(output)
raise Exception("output doesn't match")
sys.stdout.flush()
return result
configs = [
('optimized', ['-O3', '-DNDEBUG']),
('debug', [])
]
fmt_library = 'fmt/libfmt.so'
if not os.path.exists(fmt_library):
fmt_library = fmt_library.replace('.so', '.dylib')
methods = [
('printf' , []),
('IOStreams' , ['-DUSE_IOSTREAMS']),
('fmt' , ['-DUSE_FMT', '-Ifmt/include', fmt_library]),
('tinyformat' , ['-DUSE_TINYFORMAT']),
('Boost Format' , ['-DUSE_BOOST']),
('Folly Format' , ['-DUSE_FOLLY', '-lfolly']),
('stb_sprintf' , ['-DUSE_STB_SPRINTF']),
]
def format_field(field, format = '', width = ''):
return '{:{}{}}'.format(field, width, format)
def print_rulers(widths):
for w in widths:
print('=' * w, end = ' ')
print()
# Prints a reStructuredText table.
def print_table(table, *formats):
widths = [len(i) for i in table[0]]
for row in table[1:]:
for i in range(len(row)):
widths[i] = max(widths[i], len(format_field(row[i], formats[i])))
print_rulers(widths)
row = table[0]
for i in range(len(row)):
print(format_field(row[i], '', widths[i]), end = ' ')
print()
print_rulers(widths)
for row in table[1:]:
for i in range(len(row)):
print(format_field(row[i], formats[i], widths[i]), end = ' ')
print()
print_rulers(widths)
# Converts n to kibibytes.
def to_kib(n):
return int(round(n / 1024.0))
exclude_list = []
NUM_RUNS = 3
for config, flags in configs:
results = {}
for i in range(NUM_RUNS):
for method, method_flags in methods:
if method in exclude_list:
continue
print('Benchmarking', config, method)
sys.stdout.flush()
new_result = benchmark(flags + method_flags + sys.argv[1:])
if not new_result:
exclude_list.append(method)
print(method + ' is not available')
continue
if method not in results:
results[method] = new_result
continue
old_result = results[method]
old_result.time = min(old_result.time, new_result.time)
if new_result.size != old_result.size or \
new_result.stripped_size != old_result.stripped_size:
raise Exception('size mismatch')
print(config, 'Results:')
table = [
('Method', 'Compile Time, s', 'Executable size, KiB', 'Stripped size, KiB')
]
for method, method_flags in methods:
if method not in results:
continue
result = results[method]
table.append(
(method, result.time, to_kib(result.size), to_kib(result.stripped_size)))
print_table(table, '', '.1f', '', '')