-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathreport.ts
More file actions
168 lines (152 loc) · 4.48 KB
/
report.ts
File metadata and controls
168 lines (152 loc) · 4.48 KB
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
import * as util from "util";
import * as fs from "fs";
const writeFile = util.promisify(fs.writeFile);
const webpack = require("webpack");
const webpackAsync = util.promisify(webpack);
import prettyMs = require("pretty-ms");
import yargs = require("yargs");
import * as Benchmark from "benchmark";
const webpackConfig = require("./webpack.config");
function runAsync(benchmark: Benchmark) {
return new Promise(resolve => {
benchmark.on("complete", resolve).run();
});
}
async function runTest<A>(
name: string,
suite: Bench<any>,
test: Test<A>,
input: A[]
): Promise<Benchmark[]> {
const results = [];
for (let i = 0; i < input.length; i++) {
const n = input[i];
if (suite.before !== undefined) {
suite.before(n);
}
if (test.before !== undefined) {
test.before(n);
}
const b = new Benchmark(name + n.toString(), {
fn: test.run
});
b.on("complete", event => {
console.log(`${i + 1}:${input.length}`, String(event.target));
});
await runAsync(b);
results.push(b);
}
return results;
}
type Test<Input> = {
before?: (input: Input) => void;
run: () => void;
};
type Tests<Input> = { [name: string]: Test<Input> | (() => void) };
type BenchmarkOptions<Input> = {
name: string;
description?: string;
input?: Input[];
before?: (input: Input) => void;
};
type Bench<Input> = {
name: string;
description?: string;
tests: Tests<Input>;
input?: Input[];
before?: (input: Input) => void;
};
const benchmarks: Bench<any>[] = [];
export function benchmark<Input = any>(
options: string | BenchmarkOptions<Input>,
tests: Tests<Input>
): void {
if (typeof options === "string") {
options = { name: options };
}
benchmarks.push(Object.assign({}, options, { tests }));
}
function areSubstrings(s: string, ss: string[]): boolean {
return ss.some(s2 => s.toLowerCase().includes(s2.toLowerCase()));
}
async function runBenchmarks(argv: any): Promise<void> {
const { b: benchmarkNames, p, exP } = argv;
(<any>require)("./prepend.perf");
(<any>require)("./concat.perf");
(<any>require)("./map.perf");
(<any>require)("./filter.perf");
(<any>require)("./foldl.perf");
(<any>require)("./slice.perf");
(<any>require)("./random-access.perf");
(<any>require)("./update.perf");
(<any>require)("./insert.perf");
(<any>require)("./iterator.perf");
(<any>require)("./sort.perf");
(<any>require)("./foldl-iterator.perf");
const startTime = Date.now();
const results = [];
const relevantBenchmarks =
benchmarkNames === undefined
? benchmarks
: benchmarks.filter(({ name }) => areSubstrings(name, benchmarkNames));
console.log("Running", relevantBenchmarks.length, "benchmarks");
for (const suite of relevantBenchmarks) {
const { name, description, input, tests } = suite;
console.log("Running", name);
const data = [];
const names = Object.keys(tests);
const names2 =
p !== undefined ? names.filter(name => areSubstrings(name, p)) : names;
const names3 =
exP !== undefined
? names2.filter(name => !areSubstrings(name, exP))
: names2;
for (let i = 0; i < names3.length; i++) {
console.log(`${i} out of ${names3.length}`);
const testName = names3[i];
const testData = tests[testName];
const test =
typeof testData === "function" ? { run: testData } : testData;
const result = await runTest(testName, suite, test, input);
data.push({ testName, result });
}
results.push({ name, description, input, data });
}
await writeFile("data.json", JSON.stringify(results));
console.log("Generating bundle");
const stats = await webpackAsync(webpackConfig);
if (stats.hasErrors()) {
// Handle errors here
console.log(stats.toString({ colors: true }));
}
const endTime = Date.now();
console.log("Done in ", prettyMs(endTime - startTime));
}
// tslint:disable-next-line:no-unused-expression
yargs
.command(
"run",
"run the benchmarks",
yargs => yargs,
argv => {
runBenchmarks(argv);
}
)
.option("b", {
alias: "benchmarks",
describe:
"Filtering of benchmarks. Only run those that include one of the names.",
type: "array"
})
.option("p", {
alias: "performers",
describe:
"Filtering of performers. Only run those that include one of the names.",
type: "array"
})
.option("exP", {
alias: "excludePerformers",
describe: "Exclude any performers that includes any of the given strings.",
type: "array"
})
.help().argv;