-
Notifications
You must be signed in to change notification settings - Fork 6
/
bench.d
236 lines (187 loc) · 6.29 KB
/
bench.d
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
@safe:
enum numTries = 20;
int main(string[] args) {
try {
run(Options(args));
return 0;
} catch(Exception e) {
import std.stdio: stderr;
() @trusted { stderr.writeln("Error: ", e.msg); }();
return 1;
}
}
struct Options {
Compiler compiler;
Implementation implementation;
Mode mode;
this(string[] args) {
import std.getopt: getopt, defaultGetoptPrinter;
import std.stdio;
auto helpInfo =
getopt(
args,
"i|implementation", "Implementation (simple|...)", &implementation,
"c|compiler", "compiler (dmd|ldc|clang|rust)", &compiler,
"m|mode", "mode (debug|release)", &mode,
);
if(helpInfo.helpWanted) {
() @trusted { defaultGetoptPrinter("bench <options>", helpInfo.options); }();
}
writeln("Benchmarking ", compiler, " for ", implementation, " in ", mode, " mode");
}
}
void run(in Options options) {
import std.stdio: writeln;
{
const ct = benchCT(options.compiler, options.implementation, options.mode);
writeln("CT: ", ct);
}
{
const rt = benchRT(options.compiler, options.implementation, options.mode);
writeln("RT: ", rt);
}
}
enum Compiler {
dmd,
ldc,
clang,
rust,
}
enum Implementation {
simple,
lambda,
range,
generator,
}
enum Mode {
debug_,
release,
}
auto benchCT(in Compiler compiler, in Implementation implementation, in Mode mode) {
return time(compileCommand(compiler, implementation, mode));
}
auto benchRT(in Compiler compiler, in Implementation implementation, in Mode mode) {
execute(linkCommand(compiler, implementation, mode));
const binFileName = binFileName(compiler, implementation, mode);
return time(["./" ~ binFileName]);
}
auto time(in string[] args) {
import std.process: execute;
import std.exception: enforce;
import std.conv: text;
import std.array: join;
import std.datetime.stopwatch: StopWatch, AutoStart;
import std.datetime: minutes, hours, Duration;
import std.algorithm: min;
import std.stdio: write, writeln, stdout;
auto globalSw = StopWatch(AutoStart.yes);
auto duration = 99.hours;
foreach(i; 0 .. numTries) {
write(i, " ");
() @trusted { stdout.flush; }();
auto sw = StopWatch(AutoStart.yes);
execute(args);
duration = min(duration, cast(Duration) sw.peek);
// ain't nobody got time for that
if((cast(Duration) globalSw.peek) > 2.minutes) {
writeln;
return duration;
}
}
writeln;
return duration;
}
void execute(in string[] args) {
import std.process: execute_ = execute;
import std.exception: enforce;
import std.conv: text;
import std.array: join;
const ret = execute_(args);
enforce(ret.status == 0, text("Could not execute '", args.join(" "), "':\n", ret.output));
}
string[] compileCommand(in Compiler compiler, in Implementation implementation, in Mode mode) {
const fileName = srcFileName(compiler, implementation);
final switch(compiler) with(Compiler) {
case dmd:
final switch(mode) with(Mode) {
case debug_:
return ["dmd", "-c", fileName];
case release:
return ["dmd", "-c", "-O", "-inline", fileName];
}
case ldc:
final switch(mode) with(Mode) {
case debug_:
return ["ldc", "-c", fileName];
case release:
return ["ldc2", "-c", "-O2", fileName];
}
case clang:
final switch(mode) with(Mode) {
case debug_:
return ["clang++", "-Irange-v3/include/", "-c", fileName];
case release:
return ["clang++", "-Irange-v3/include/", "-c", "-O2", fileName];
}
case rust:
final switch(mode) with(Mode) {
case debug_:
return ["rustc", "+nightly", "--emit=obj", fileName];
case release:
return ["rustc", "+nightly", "--emit=obj", "-C", "opt-level=2", fileName];
}
}
}
string[] linkCommand(in Compiler compiler, in Implementation implementation, in Mode mode) {
const srcFileName = srcFileName(compiler, implementation);
const binFileName = binFileName(compiler, implementation, mode);
final switch(compiler) with(Compiler) {
case dmd:
final switch(mode) with(Mode) {
case debug_:
return ["dmd", "-of" ~ binFileName, srcFileName];
case release:
return ["dmd", "-of" ~ binFileName, "-O", "-inline", srcFileName];
}
case ldc:
final switch(mode) with(Mode) {
case debug_:
return ["ldc2", "-of", binFileName, srcFileName];
case release:
return ["ldc2", "-of", binFileName, "-O2", srcFileName];
}
case clang:
final switch(mode) with(Mode) {
case debug_:
return ["clang++", "-Irange-v3/include/", "-o", binFileName, srcFileName];
case release:
return ["clang++", "-Irange-v3/include/", "-o", binFileName, "-O2", srcFileName];
}
case rust:
final switch(mode) with(Mode) {
case debug_:
return ["rustc", "+nightly", "-o", binFileName, srcFileName];
case release:
return ["rustc", "+nightly", "-o", binFileName, "-C", "opt-level=2", srcFileName];
}
}
}
string srcFileName(in Compiler compiler, in Implementation implementation) {
import std.conv: text;
return text(implementation) ~ extension(compiler);
}
string binFileName(in Compiler compiler, in Implementation implementation, in Mode mode) {
import std.conv: text;
return text(implementation, "_", compiler, "_", mode);
}
string extension(in Compiler compiler) {
final switch(compiler) with(Compiler) {
case dmd:
case ldc:
return ".d";
case clang:
return ".cpp";
case rust:
return ".rs";
}
}