forked from rtsao/css-in-js-perf-tests
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrun.js
76 lines (60 loc) · 2.4 KB
/
run.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
import fs from 'fs';
import { Suite } from 'benchmark';
import beautifyBenchmark from 'beautify-benchmark';
import { toKebabCase, pad, createOutputDir } from './utilities';
export const runTest = (testName, cases) => {
return new Promise((resolve, reject) => {
const testCases = {};
Object.keys(cases).forEach((k) => {
testCases[toKebabCase(k)] = { testCase: cases[k], result: null };
});
console.log(`Running ${testName} test.\n`);
const testSuite = new Suite();
Object.keys(testCases).forEach((caseName) => {
testSuite.add(caseName, () => { testCases[caseName].result = testCases[caseName].testCase(pad(caseName)); });
});
testSuite.on('cycle', (e) => {
beautifyBenchmark.add(e.target);
});
testSuite.on('complete', function onComplete() {
let smallestSize = Number.MAX_VALUE;
let smallest = '?';
Object.keys(testCases).forEach((caseName) => {
const length = testCases[caseName].result.length;
console.log(`${caseName} length`, length);
if (smallestSize > length) {
smallestSize = length;
smallest = caseName;
}
});
console.log(`\nSmallest is: ${smallest}`);
beautifyBenchmark.log();
console.log(`Fastest is: ${this.filter('fastest').map('name')}\n`);
resolve();
});
testSuite.run({ async: true });
});
};
export const runView = (testName, cases) => {
return new Promise((resolve, reject) => {
const testCases = {};
Object.keys(cases).forEach((caseName) => {
testCases[toKebabCase(caseName)] = { testCase: cases[caseName], result: null };
});
console.log(`Running view ${testName}.\n`);
const outputDir = createOutputDir(testName.replace(/ /, '-'));
Object.keys(testCases).forEach((caseName) => {
const html = testCases[caseName].testCase(pad(caseName));
const path = `${outputDir}/${caseName}.html`;
fs.writeFile(path, html, (err) => {
if (err) {
console.error(err);
reject(err);
} else {
console.log(`Wrote ${path}`);
resolve(path);
}
});
});
});
};