-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperfmeasure.js
79 lines (60 loc) · 1.96 KB
/
perfmeasure.js
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
const fs = require('fs');
const path = require('path');
const {performance} = require('perf_hooks');
const argv = require('minimist')(process.argv.slice(2));
let filter = null;
if (argv.f) {
filter = new RegExp(argv.f);
}
let iterations = 1;
if (argv.n) {
const n = Number.parseInt(argv.n);
if (!Number.isNaN(n)) {
iterations = n;
}
}
const csvFile = argv.csv;
async function runPerfTests(filter = null, iterations = 1, csvFile = null) {
const content = fs.readFileSync(path.resolve(__dirname, './.vscode/launch.json'), 'utf8');
const json = JSON.parse(content.replace(/^\s*\/\/ .+/mg, ''));
let csvContent = 'Test';
for (let i = 0; i < iterations; i++) {
csvContent += `,Iteration ${i}`;
}
csvContent += '\n';
for (const config of json.configurations) {
if (config.args !== undefined) {
const name = config.name;
if (filter != null && !filter.test(name)) {
continue;
}
const testName = config.args[0];
const input = config.args[1].replace('${workspaceFolder}', `${__dirname}`);
const testModule = require(`./${testName}`);
console.log(`Running ${name}...`);
csvContent += name;
for (let i = 0; i < iterations; i++) {
const start = performance.now();
let results = testModule.run(input);
if (results instanceof Promise) {
results = await results;
}
// Make sure we read all results so a generator doesn't just run part of
// the test.
if (Array.from(results).length <= 0) {
throw new Error('invalid result');
}
const stop = performance.now();
const executionTime = stop - start;
console.log(`Iteration ${i}: ${executionTime.toFixed(3)}ms`);
csvContent += `,${executionTime}`;
}
csvContent += '\n';
}
}
if (csvFile) {
fs.writeFileSync(path.normalize(csvFile), csvContent, 'utf8');
}
console.log('Done!');
}
runPerfTests(filter, iterations, csvFile);