This repository has been archived by the owner on Oct 12, 2022. It is now read-only.
/
runbench.d
137 lines (114 loc) · 3.72 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
/**
* 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;
// cmdline flags
bool verbose;
string runCmd(string cmd)
{
import std.exception : enforce;
import std.process : executeShell;
if (verbose) writeln(cmd);
auto res = executeShell(cmd);
enforce(res.status == 0, res.output);
return res.output;
}
void runTest(string pattern, string dmd, string dflags, string runArgs, uint repeat)
{
import std.algorithm, std.file, std.path, std.regex, std.string;
string[] sources;
auto re = regex(pattern, "g");
auto self = buildPath(".", "runbench.d");
foreach(DirEntry src; dirEntries(".", SpanMode.depth))
{
if (src.isFile && !match(src.name, re).empty &&
endsWith(src.name, ".d") && src.name != self)
{
sources ~= src.name;
}
}
immutable bindir = absolutePath("bin");
foreach(ref src; sources)
{
writeln("COMPILING ", src);
auto bin = buildPath(bindir, src.chompPrefix("./").chomp(".d"));
auto cmd = std.string.format("%s %s -op -odobj -of%s %s", dmd, dflags, bin, src);
runCmd(cmd);
src = bin;
}
foreach(bin; sources)
{
import std.datetime, std.algorithm : min;
auto sw = StopWatch(AutoStart.yes);
auto minDur = Duration.max;
stdout.writef("RUNNING %-20s", bin.relativePath(bindir));
stdout.flush();
auto cmd = bin ~ " " ~ runArgs;
string gcprof;
foreach (_; 0 .. repeat)
{
sw.reset;
auto output = runCmd(cmd);
auto dur = cast(Duration)sw.peek;
if (dur >= minDur) continue;
minDur = dur;
auto lines = output.splitter(ctRegex!`\r\n|\r|\n`)
.find!(ln => ln.startsWith("maxPoolMemory"));
if (!lines.empty) gcprof = lines.front;
}
auto res = minDur.split!("seconds", "msecs");
if (gcprof.length)
writefln(" %s.%03s s, %s", res.seconds, res.msecs, gcprof);
else
writefln(" %s.%03s s", res.seconds, res.msecs, gcprof);
}
}
void printHelp()
{
import std.ascii : nl=newline;
auto helpString =
"usage: runbench [<test_regex>] [<dflags>] [-h|--help] [-v|--verbose] [-r n|--repeat=n] [-- <runargs>]"~nl~nl~
" tests - Regular expressions to select tests. Default: '.*\\.d'"~nl~
" dflags - Flags passed to compiler. Default: '-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);
}
void main(string[] args)
{
import std.algorithm;
string runArgs;
{
import std.range : only;
string[] tmp = args;
if (findSkip(tmp, only("--")))
{
runArgs = std.string.join(tmp, " ");
args = args[0 .. $ - 1 - tmp.length];
}
}
import std.getopt;
bool help; uint repeat = 10;
getopt(args, config.passThrough,
"h|help", &help,
"v|verbose", &verbose,
"r|repeat", &repeat);
string pattern = r".*\.d";
if (args.length >= 2)
{
pattern = args[1];
args = args.remove(1);
}
if (help) return printHelp();
auto dflags = std.string.join(args[1 .. $], " ");
if (!dflags.length)
dflags = "-O -release -inline";
import std.process : env=environment;
auto dmd = env.get("DMD", "dmd");
writeln("compiler: "~dmd~' '~dflags);
runTest(pattern, dmd, dflags, runArgs, repeat);
}