-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathbench.js
More file actions
79 lines (67 loc) · 2.07 KB
/
bench.js
File metadata and controls
79 lines (67 loc) · 2.07 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
'use strict';
const fs = require('fs');
const process = require('process');
const exec = require('child_process').execSync;
const path = require('path');
const benchmark = require('benchmark');
// function time(filename, expected) {
// const results = exec(filename).toString();
// const lines = results.split('\n');
// const timingsLine = lines.pop();
//
// const time = timingsLine.match(/([\d.]+) total/)[1];
//
// if (expected != null) {
// const resultString = lines.join('\n').trim();
// if (expected !== resultString) {
// throw new Error("unexpected output for benchmark file ", filename);
// }
// }
//
// return JSON.parse(time);
// }
function benchmark2json(b) {
if (b.error) {
return {error: b.error};
} else {
return {
hz: b.hz,
rme: b.stats.rme,
samples: b.stats.sample.length,
};
}
}
function runBenchmark(dir) {
// const expectedFile = path.join(dir, 'expected-output');
// const expected = fs.existsSync(expectedFile) ? fs.readFileSync(expectedFile).toString().trim() : null;
//
const jsFiles = fs.readdirSync(dir).filter(f => f.match(/^[^_].*\.js$/));
const lib2results = {};
jsFiles.forEach(filename => {
const lib = filename.split(".")[1];
lib2results[lib] = [];
console.log("running", filename);
var fn = require(path.resolve(path.join(dir, filename)));
var b = new benchmark.Benchmark(lib, fn);
b.run();
lib2results[lib] = benchmark2json(b);
if (lib2results[lib].error) {
console.error(lib2results[lib].error);
}
});
return lib2results;
}
if (require.main === module) {
if (process.argv.length > 2) {
const benchmark = process.argv[2];
console.dir(runBenchmark(path.join('benchmarks', benchmark)));
} else {
const bench2results = {};
fs.readdirSync('benchmarks')
.filter(nm => fs.lstatSync(path.join('benchmarks', nm)).isDirectory())
.forEach(dir => {
bench2results[dir] = runBenchmark(path.join('benchmarks', dir));
});
fs.writeFileSync('benchmark-results.json', JSON.stringify(bench2results, null, ' '));
}
}