-
Notifications
You must be signed in to change notification settings - Fork 0
/
client-indirect-publishers.js
75 lines (63 loc) · 1.86 KB
/
client-indirect-publishers.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
let socketClusterClient = require('socketcluster-client');
let argv = require('minimist')(process.argv.slice(2));
let options = JSON.parse(argv.options || '{}');
let clientOptions = {
port: options.targetPort || 8000,
hostname: options.targetHost || '127.0.0.1'
};
let clientCount = options.clientCount || 1;
let publishInterval = options.publishInterval || 500;
let publishRandomness = options.publishRandomness || 500;
let uniqueChannelCount = options.uniqueChannelCount || 100;
let publishesPerClient = options.publishesPerClient || 10;
let channelNames = [];
for (let i = 0; i < uniqueChannelCount; i++) {
channelNames.push(`testChannel${i}`);
}
let c = 0;
for (let i = 0; i < clientCount; i++) {
let intervalRandomness = Math.random() * publishRandomness;
let socket = socketClusterClient.create(clientOptions);
(async () => {
for await (let {error} of socket.listener('error')) {
process.send({
type: 'error',
name: error.name,
message: error.message
});
}
})();
let packet = {
message: `This is socket ${i}`
};
socket.invokeCount = 0;
socket.publishInterval = setInterval(() => {
let targetChannelName = channelNames[c++ % uniqueChannelCount];
packet.channel = targetChannelName;
(async () => {
try {
await socket.invoke('publishToChannel', packet);
} catch (error) {
process.send({
type: 'failedToSend',
socketId: socket.id,
channel: targetChannelName,
data: packet
});
return;
}
process.send({
type: 'sent',
socketId: socket.id,
channel: targetChannelName,
data: packet
});
})();
if (++socket.invokeCount >= publishesPerClient) {
clearInterval(socket.publishInterval);
}
}, publishInterval + intervalRandomness);
}
process.send({
type: 'ready'
});