forked from jestjs/jest
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest.js
More file actions
187 lines (148 loc) · 4.21 KB
/
test.js
File metadata and controls
187 lines (148 loc) · 4.21 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
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
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
const assert = require('assert');
// eslint-disable-next-line import/no-extraneous-dependencies
const workerFarm = require('worker-farm');
const JestWorker = require('../../build').Worker;
// Typical tests: node --expose-gc test.js empty 100000
// node --expose-gc test.js loadTest 10000
assert(process.argv[2], 'Pass a child method name');
assert(process.argv[3], 'Pass the number of iteratitons');
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
const method = process.argv[2];
const calls = +process.argv[3];
const threads = 6;
function testWorkerFarm() {
return new Promise(async resolve => {
const startTime = Date.now();
let count = 0;
async function countToFinish() {
if (++count === calls) {
workerFarm.end(api);
const endTime = Date.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 = Date.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 = Date.now();
let count = 0;
async function countToFinish() {
if (++count === calls) {
farm.end();
const endTime = Date.now();
// Let all workers go down.
await sleep(2000);
resolve({
globalTime: endTime - startTime - 2000,
processingTime: endTime - startProcess,
});
}
}
const farm = new JestWorker(require.resolve('./workers/jest_worker'), {
exposedMethods: [method],
forkOptions: {execArgv: []},
workers: threads,
});
farm.getStdout().pipe(process.stdout);
farm.getStderr().pipe(process.stderr);
// Let all workers come up.
await sleep(2000);
const startProcess = Date.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 (!global.gc) {
console.log('GC not present');
}
const wFResults = [];
const jWResults = [];
for (let i = 0; i < 10; i++) {
console.log('-'.repeat(75));
profile('worker farm');
const wF = await testWorkerFarm();
profileEnd('worker farm');
await sleep(3000);
// eslint-disable-next-line no-undef
global.gc && gc();
profile('jest worker');
const jW = await testJestWorker();
profileEnd('jest worker');
await sleep(3000);
// eslint-disable-next-line no-undef
global.gc && 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 < 10; 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();