-
Notifications
You must be signed in to change notification settings - Fork 3
/
bench.js
108 lines (95 loc) · 2.83 KB
/
bench.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
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
const path = require('path');
const util = require('util');
const { exec: execCb } = require('child_process');
const exec = util.promisify(execCb);
const { mkTestDir } = require('./test/util.js');
const QDD = path.resolve(__dirname, 'index.js');
const freshNpmTimes = [];
const freshYarnTimes = [];
const freshQddTimes = [];
const primedNpmTimes = [];
const primedYarnTimes = [];
const primedQddTimes = [];
async function timeCmd(cmd, cacheDir) {
if (cacheDir) {
await exec(`rm -rf ${cacheDir}`);
}
await exec(`rm -rf node_modules`)
const start = process.hrtime();
await exec(cmd);
const elapsed = process.hrtime(start);
return elapsed[0] + elapsed[1] / 1e9;
}
async function freshNpm () {
const time = await timeCmd(
`npm ci --ignore-scripts --cache .npm-cache`,
'.npm-cache'
);
freshNpmTimes.push(time);
console.log(' fresh cache npm ci:', time, 'seconds');
}
async function freshYarn () {
const time = await timeCmd(
`yarn --ignore-scripts --cache-folder .yarn-cache`,
'.yarn-cache'
);
freshYarnTimes.push(time);
console.log(' fresh cache yarn:', time, 'seconds');
}
async function freshQdd () {
const time = await timeCmd(
`node ${QDD} --cache .qdd-cache`,
'.qdd-cache'
);
freshQddTimes.push(time);
console.log(' fresh cache qdd:', time, 'seconds');
}
async function primedNpm () {
const time = await timeCmd(
`npm ci --ignore-scripts --cache .npm-cache`
);
primedNpmTimes.push(time);
console.log('primed cache npm ci:', time, 'seconds');
}
async function primedYarn () {
const time = await timeCmd(
`yarn --ignore-scripts --cache-folder .yarn-cache`
);
primedYarnTimes.push(time);
console.log(' primed cache yarn:', time, 'seconds');
}
async function primedQdd () {
const time = await timeCmd(
`node ${QDD} --cache .qdd-cache`
);
primedQddTimes.push(time);
console.log(' primed cache qdd:', time, 'seconds');
}
const TIMES = Number(process.env.ITERATIONS) || 10;
function average (nums) {
let total = 0;
for (const i of nums) {
total += i;
}
return total / nums.length;
}
(async () => {
const cleanup = await mkTestDir('bench');
for (let i = 0; i < TIMES; i++) {
await freshNpm();
await freshYarn();
await freshQdd();
}
for (let i = 0; i < TIMES; i++) {
await primedNpm();
await primedYarn();
await primedQdd();
}
await cleanup();
console.log(' fresh cache npm ci (avg):', average(freshNpmTimes), 'seconds');
console.log(' fresh cache yarn (avg):', average(freshYarnTimes), 'seconds');
console.log(' fresh cache qdd (avg):', average(freshQddTimes), 'seconds');
console.log('primed cache npm ci (avg):', average(primedNpmTimes), 'seconds');
console.log(' primed cache yarn (avg):', average(primedYarnTimes), 'seconds');
console.log(' primed cache qdd (avg):', average(primedQddTimes), 'seconds');
})();