forked from jestjs/jest
-
Notifications
You must be signed in to change notification settings - Fork 3
/
test.js
191 lines (150 loc) Β· 4.26 KB
/
test.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
/* eslint-disable no-async-promise-executor */
/**
* To start the test, build the repo and run:
* node --expose-gc test.js empty 100000
* node --expose-gc test.js loadTest 10000
*/
'use strict';
const assert = require('assert');
const {performance} = require('perf_hooks');
const workerFarm = require('worker-farm');
const JestWorker = require('../').Worker;
assert(process.argv[2], 'Pass a child method name');
assert(process.argv[3], 'Pass the number of iterations');
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
const method = process.argv[2];
const calls = Number(process.argv[3]);
const threads = 6;
const iterations = 10;
function testWorkerFarm() {
return new Promise(async resolve => {
const startTime = performance.now();
let count = 0;
async function countToFinish() {
if (++count === calls) {
workerFarm.end(api);
const endTime = performance.now();
// Let all workers go down.
await sleep(2000);
resolve({
globalTime: endTime - startTime - 2000,
processingTime: endTime - startProcess,
});
}
}
const api = workerFarm(
{
autoStart: true,
maxConcurrentCallsPerWorker: 1,
maxConcurrentWorkers: threads,
},
require.resolve('./workers/worker_farm'),
[method],
);
// Let all workers come up.
await sleep(2000);
const startProcess = performance.now();
for (let i = 0; i < calls; i++) {
const promisified = new Promise((resolve, reject) => {
api[method]((err, result) => {
if (err) {
reject(err);
} else {
resolve(result);
}
});
});
promisified.then(countToFinish);
}
});
}
function testJestWorker() {
return new Promise(async resolve => {
const startTime = performance.now();
let count = 0;
async function countToFinish() {
if (++count === calls) {
const endTime = performance.now();
// Let all workers go down.
await farm.end();
resolve({
globalTime: endTime - startTime - 2000,
processingTime: endTime - startProcess,
});
}
}
const farm = new JestWorker(require.resolve('./workers/jest_worker'), {
exposedMethods: [method],
forkOptions: {execArgv: []},
numWorkers: threads,
});
farm.getStdout().pipe(process.stdout);
farm.getStderr().pipe(process.stderr);
// Let all workers come up.
await farm.start();
const startProcess = performance.now();
for (let i = 0; i < calls; i++) {
const promisified = farm[method]();
promisified.then(countToFinish);
}
});
}
function profile(x) {
console.profile(x);
}
function profileEnd(x) {
console.profileEnd(x);
}
async function main() {
if (!globalThis.gc) {
throw new Error('GC not present, start with node --expose-gc');
}
const wFResults = [];
const jWResults = [];
for (let i = 0; i < iterations; i++) {
console.log('-'.repeat(75));
profile('worker farm');
const wF = await testWorkerFarm();
profileEnd('worker farm');
await sleep(3000);
globalThis.gc?.();
profile('jest worker');
const jW = await testJestWorker();
profileEnd('jest worker');
await sleep(3000);
globalThis.gc?.();
wFResults.push(wF);
jWResults.push(jW);
console.log('jest-worker:', jW);
console.log('worker-farm:', wF);
}
let wFGT = 0;
let wFPT = 0;
let jWGT = 0;
let jWPT = 0;
for (let i = 0; i < iterations; i++) {
wFGT += wFResults[i].globalTime;
wFPT += wFResults[i].processingTime;
jWGT += jWResults[i].globalTime;
jWPT += jWResults[i].processingTime;
}
console.log('-'.repeat(75));
console.log('total worker-farm:', {wFGT, wFPT});
console.log('total jest-worker:', {jWGT, jWPT});
console.log('-'.repeat(75));
console.log(
`% improvement over ${calls} calls (global time):`,
(100 * (wFGT - jWGT)) / wFGT,
);
console.log(
`% improvement over ${calls} calls (processing time):`,
(100 * (wFPT - jWPT)) / wFPT,
);
}
main();