forked from piscinajs/piscina
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhistogram.ts
192 lines (165 loc) · 5.56 KB
/
histogram.ts
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
192
import Piscina from '..';
import { test } from 'tap';
import { resolve } from 'path';
import { PiscinaWorker } from '../dist/worker_pool';
test('pool will maintain run and wait time histograms by default', async ({ equal, ok }) => {
const pool = new Piscina({
filename: resolve(__dirname, 'fixtures/eval.js')
});
const tasks = [];
for (let n = 0; n < 10; n++) {
tasks.push(pool.run('42'));
}
await Promise.all(tasks);
const waitTime = pool.waitTime as any;
ok(waitTime);
equal(typeof waitTime.average, 'number');
equal(typeof waitTime.mean, 'number');
equal(typeof waitTime.stddev, 'number');
equal(typeof waitTime.min, 'number');
equal(typeof waitTime.max, 'number');
const runTime = pool.runTime as any;
ok(runTime);
equal(typeof runTime.average, 'number');
equal(typeof runTime.mean, 'number');
equal(typeof runTime.stddev, 'number');
equal(typeof runTime.min, 'number');
equal(typeof runTime.max, 'number');
});
test('pool will maintain run and wait time histograms when recordTiming is true', async ({ ok }) => {
const pool = new Piscina({
filename: resolve(__dirname, 'fixtures/eval.js'),
recordTiming: true
});
const tasks = [];
for (let n = 0; n < 10; n++) {
tasks.push(pool.run('42'));
}
await Promise.all(tasks);
const waitTime = pool.waitTime as any;
ok(waitTime);
const runTime = pool.runTime as any;
ok(runTime);
});
test('pool does not maintain run and wait time histograms when recordTiming is false', async ({ equal }) => {
const pool = new Piscina({
filename: resolve(__dirname, 'fixtures/eval.js'),
recordTiming: false
});
const tasks = [];
for (let n = 0; n < 10; n++) {
tasks.push(pool.run('42'));
}
await Promise.all(tasks);
equal(pool.waitTime, null);
equal(pool.runTime, null);
});
test('workers has histogram', async t => {
let index = 0;
let list: PiscinaWorker[];
// Its expected to have one task get balanced twice due to the load balancer distribution
// first task enters, its distributed; second is enqueued, once first is done, second is distributed and normalizes
t.plan(4);
const pool = new Piscina({
filename: resolve(__dirname, 'fixtures/eval.js'),
maxThreads: 1,
concurrentTasksPerWorker: 1,
workerHistogram: true,
loadBalancer (_task, workers) {
// Verify distribution to properly test this feature
const candidate = workers[index++ % workers.length];
// We assign it everytime is called to check the histogram
// and that the list remains the same
list = workers;
if (candidate.currentUsage !== 0) {
return null;
}
return candidate;
}
});
const tasks = [];
for (let n = 0; n < 10; n++) {
tasks.push(pool.run('new Promise(resolve => setTimeout(resolve, 500))'));
}
await Promise.all(tasks);
const histogram = list[0].histogram;
t.type(histogram?.average, 'number');
t.type(histogram?.max, 'number');
t.type(histogram?.mean, 'number');
t.type(histogram?.min, 'number');
});
test('workers does not have histogram if disabled', async t => {
let index = 0;
// After each task the balancer is called to distribute the next task
// The first task is distributed, the second is enqueued, once the first is done, the second is distributed and normalizes
t.plan(10 * 2);
const pool = new Piscina({
filename: resolve(__dirname, 'fixtures/eval.js'),
maxThreads: 1,
concurrentTasksPerWorker: 1,
workerHistogram: false,
loadBalancer (_task, workers) {
// Verify distribution to properly test this feature
const candidate = workers[index++ % workers.length];
const histogram = candidate.histogram;
t.notOk(histogram);
if (candidate.currentUsage !== 0) {
return null;
}
return candidate;
}
});
const tasks = [];
for (let n = 0; n < 10; n++) {
tasks.push(pool.run('new Promise(resolve => setTimeout(resolve, 500))'));
}
await Promise.all(tasks);
});
// test('histogram of worker should be initialized with max concurrent task set as min', { only: true }, async t => {
// // After each task the balancer is called to distribute the next task
// // The first task is distributed, the second is enqueued, once the first is done, the second is distributed and normalizes
// let counter = 0;
// const pool = new Piscina({
// filename: resolve(__dirname, 'fixtures/eval.js'),
// maxThreads: 2,
// concurrentTasksPerWorker: 1,
// workerHistogram: true,
// });
// const tasks = [];
// t.plan(10 * 2);
// pool.on('workerCreate', worker => {
// if (counter === 0) {
// t.equal(worker.histogram.min, 0);
// } else {
// t.equal(worker.histogram.min, 1);
// }
// })
// for (let n = 0; n < 10; n++) {
// tasks.push(pool.run('new Promise(resolve => setTimeout(resolve, 500))'));
// }
// await Promise.all(tasks);
// });
test('opts.workerHistogram should be a boolean value', async t => {
let index = 0;
t.plan(1);
t.throws(() => {
// eslint-disable-next-line no-new
new Piscina({
filename: resolve(__dirname, 'fixtures/eval.js'),
maxThreads: 1,
concurrentTasksPerWorker: 1,
// @ts-expect-error
workerHistogram: 1,
loadBalancer (_task, workers) {
// Verify distribution to properly test this feature
const candidate = workers[index++ % workers.length];
const histogram = candidate.histogram;
t.notOk(histogram);
if (candidate.currentUsage !== 0) {
return null;
}
return candidate;
}
});
}, 'options.workerHistogram must be a boolean');
});