-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunbench.d
executable file
·248 lines (214 loc) · 7.65 KB
/
runbench.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
237
238
239
240
241
242
243
244
245
246
247
248
#!/usr/bin/env rdmd
/**
* This is a driver script that runs the benchmarks.
*
* Copyright: Copyright Martin Nowak 2011 -.
* License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
* Authors: Martin Nowak
*/
import std.stdio;
extern(C) __gshared bool rt_cmdline_enabled = false;
struct Config
{
string pattern = r".*\.d", dmd = "dmd", dflags = "-mcpu=native -O -release -inline", args;
bool help, verbose, compile = true;
uint repeat = 10;
}
string runCmd(string cmd, bool verbose, in char[] workDir = null)
{
import std.exception : enforce;
import std.process : executeShell, Config;
if (verbose) writeln(cmd);
auto res = executeShell(cmd, null, Config.none, size_t.max, workDir);
enforce(res.status == 0, res.output);
return res.output;
}
string extraSourceOf(string path)
{
import std.path, std.string;
string dir = path.dirName;
while(dir != path)
{
string base = dir.baseName;
if(base.endsWith(".extra"))
return dir[0..$-6] ~ ".d";
path = dir;
dir = path.dirName;
}
return null;
}
void runTests(Config cfg)
{
import std.algorithm, std.file, std.path, std.regex, std.string;
if (exists("gcx.log")) remove("gcx.log");
string[] sources;
string[string] extra_sources;
auto re = regex(cfg.pattern, "g");
auto cwd = __FILE_FULL_PATH__.dirName;
auto self = buildPath(cwd, "runbench.d");
foreach(DirEntry src; dirEntries(cwd, "*.d", SpanMode.depth))
{
if (!src.isFile || src.name == self || src.name.withExtension(".ignore").exists)
continue;
string mainsrc = extraSourceOf(src.name);
if (mainsrc)
{
if (cfg.verbose) writeln(src.name, " is extra file for ", mainsrc);
extra_sources[mainsrc] ~= " " ~ src.name;
}
else if (!match(src.name, re).empty)
sources ~= src.name;
}
import std.parallelism : parallel;
immutable bindir = absolutePath("bin", cwd);
immutable objdir = absolutePath("obj", cwd);
foreach(ref src; sources.parallel(1))
{
version (Windows) enum exe = "exe"; else enum exe = "";
auto bin = buildPath(bindir, src.relativePath(cwd).setExtension(exe));
auto obj = buildPath(objdir, src.relativePath(cwd).setExtension(exe));
auto cmd = std.string.format("%s %s -op -od%s -of%s %s", cfg.dmd, cfg.dflags, obj, bin, src);
if (auto ex = src in extra_sources)
cmd ~= " -I" ~ src[0..$-2] ~ ".extra" ~ *ex;
if (cfg.compile)
{
writeln("COMPILING ", src);
runCmd(cmd, cfg.verbose);
}
src = bin;
}
foreach(bin; sources)
{
import core.time : Duration;
import std.algorithm : min;
import std.datetime.stopwatch : AutoStart, StopWatch;
auto sw = StopWatch(AutoStart.yes);
auto minDur = Duration.max;
string minGCProf;
immutable benchName = bin.baseName.stripExtension;
void report(string pfx, Duration dur, string gcProf)
{
auto parts = dur.split!("seconds", "msecs");
if (gcProf.length)
writefln("%s %-16s %s.%03s s, %s", pfx, benchName, parts.seconds, parts.msecs, gcProf);
else
writefln("%s %-16s %s.%03s s", pfx, benchName, parts.seconds, parts.msecs);
}
auto cmd = bin ~ " " ~ cfg.args;
foreach (_; 0 .. cfg.repeat)
{
sw.reset;
auto output = runCmd(cmd, cfg.verbose, cwd);
auto dur = cast(Duration)sw.peek;
auto parts = dur.split!("seconds", "msecs");
if (cfg.verbose) stdout.write(output);
string gcProf;
if (exists("gcx.log"))
{
auto tgt = bin.setExtension("gcx.log");
rename("gcx.log", tgt);
auto lines = File(tgt, "r").byLine()
.find!(ln => ln.canFind("GC summary:"));
if (!lines.empty) gcProf = lines.front.find("GC summary:")[11..$].idup;
}
else
{
auto lines = output.splitter(ctRegex!`\r\n|\r|\n`)
.find!(ln => ln.startsWith("GC summary:"));
if (!lines.empty) gcProf = lines.front[11..$];
}
if (cfg.verbose) report("RUN", dur, gcProf);
if (dur < minDur)
{
minDur = dur;
minGCProf = gcProf;
}
}
report("MIN", minDur, minGCProf);
}
}
void printHelp()
{
import std.ascii : nl=newline;
auto helpString =
"usage: runbench [-h|--help] [-v|--verbose] [-r n|--repeat=n] [<test_regex>] [<dflags>] [-- <runargs>]"~nl~nl~
" tests - Regular expressions to select tests. Default: '.*\\.d'"~nl~
" dflags - Flags passed to compiler. Default: '-mcpu=native -O -release -inline'"~nl~
" runargs - Arguments passed to each test, e.g. '--DRT-gcopt=profile=1'"~nl~nl~
"Don't pass any argument to run all tests with optimized builds.";
writeln(helpString);
}
Config parseArgs(string[] args)
{
import std.algorithm, std.string : join;
Config cfg;
{
import std.range : only;
string[] tmp = args;
if (findSkip(tmp, only("--")))
{
import std.process : escapeShellCommand;
cfg.args = escapeShellCommand(tmp);
args = args[0 .. $ - 1 - tmp.length];
}
}
import std.getopt;
getopt(args, config.stopOnFirstNonOption,
config.passThrough,
"h|help", &cfg.help,
"v|verbose", &cfg.verbose,
"N|no-compile", (string option) { cfg.compile = false; },
"dflags", &cfg.dflags,
"r|repeat", &cfg.repeat);
if (args.length >= 2 && !args[1].startsWith("-"))
{
cfg.pattern = args[1];
args = args.remove(1);
}
if (args.length > 1)
cfg.dflags = join(args[1 .. $], " ");
return cfg;
}
unittest
{
template cfg(N...)
{
static Config cfg(T...)(T vals) if (T.length == N.length)
{
Config res;
foreach (i, ref v; vals) __traits(getMember, res, N[i]) = v;
return res;
}
}
import std.typecons : t=tuple;
auto check = [
t(["bin"], cfg),
t(["bin", "-h"], cfg!("help")(true)),
t(["bin", "-v"], cfg!("verbose")(true)),
t(["bin", "-h", "-v"], cfg!("help", "verbose")(true, true)),
t(["bin", "gcbench"], cfg!("pattern")("gcbench")),
t(["bin", "-v", "gcbench"], cfg!("pattern", "verbose")("gcbench", true)),
t(["bin", "-r", "4", "gcbench"], cfg!("pattern", "repeat")("gcbench", 4)),
t(["bin", "-g"], cfg!("dflags")("-g")),
t(["bin", "gcbench", "-g"], cfg!("pattern", "dflags")("gcbench", "-g")),
t(["bin", "-r", "2", "gcbench", "-g"], cfg!("pattern", "dflags", "repeat")("gcbench", "-g", 2)),
t(["bin", "--", "--DRT-gcopt=profile:1"], cfg!("args")("--DRT-gcopt=profile:1")),
t(["bin", "--", "foo", "bar"], cfg!("args")("foo bar")),
t(["bin", "gcbench", "--", "args"], cfg!("pattern", "args")("gcbench", "args")),
t(["bin", "--repeat=5", "gcbench", "--", "args"], cfg!("pattern", "args", "repeat")("gcbench", "args", 5)),
];
foreach (pair; check)
assert(parseArgs(pair[0]) == pair[1]);
}
void main()
{
import core.runtime;
// use Runtime.args for --DRT-gcopt
auto cfg = parseArgs(Runtime.args);
if (cfg.help) return printHelp();
import std.process : env=environment;
cfg.dmd = env.get("DMD", "dmd");
if (cfg.compile)
writeln("compiler: "~cfg.dmd~' '~cfg.dflags);
runTests(cfg);
}