forked from wilsonzlin/minify-html
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspeeds.js
More file actions
79 lines (71 loc) · 2.2 KB
/
speeds.js
File metadata and controls
79 lines (71 loc) · 2.2 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
const benchmark = require('benchmark');
const childProcess = require('child_process');
const minimist = require('minimist');
const path = require('path');
const minifiers = require('./minifiers');
const results = require('./results');
const tests = require('./tests');
const args = minimist(process.argv.slice(2));
const shouldRunRust = !!args.rust;
const cmd = (command, ...args) => {
const throwErr = msg => {
throw new Error(`${msg}\n ${command} ${args.join(' ')}`);
};
const {status, signal, error, stdout, stderr} = childProcess.spawnSync(command, args.map(String), {
stdio: ['ignore', 'pipe', 'pipe'],
encoding: 'utf8',
});
if (error) {
throwErr(error.message);
}
if (signal) {
throwErr(`Command exited with signal ${signal}`);
}
if (status !== 0) {
throwErr(`Command exited with status ${status}`);
}
if (stderr) {
throwErr(`stderr: ${stderr}`);
}
return stdout;
};
const fromEntries = entries => {
if (Object.fromEntries) return Object.fromEntries(entries);
const obj = {};
for (const [prop, val] of entries) obj[prop] = val;
return obj;
};
const runTest = test => new Promise((resolve, reject) => {
// Run JS libraries.
const suite = new benchmark.Suite();
for (const m of Object.keys(minifiers)) {
suite.add(m, {
defer: true,
fn (deferred) {
Promise.resolve(minifiers[m](test.contentAsString, test.contentAsBuffer)).then(() => deferred.resolve());
},
});
}
suite
.on('cycle', event => console.info(test.name, event.target.toString()))
.on('complete', () => resolve(fromEntries(suite.map(b => [b.name, b.hz]))))
.on('error', reject)
.run({'async': true});
});
(async () => {
const speeds = fromEntries(tests.map(t => [t.name, {}]));
// Run Rust library.
if (shouldRunRust) {
for (const [testName, testOps] of JSON.parse(cmd(
path.join(__dirname, 'minify-html-bench', 'target', 'release', 'minify-html-bench'),
'--iterations', 512,
'--tests', path.join(__dirname, 'tests'),
))) {
Object.assign(speeds[testName], {['minify-html']: testOps});
}
}
for (const t of tests) {
Object.assign(speeds[t.name], await runTest(t));
}
results.writeSpeedResults(speeds);
})();