-
Notifications
You must be signed in to change notification settings - Fork 10
/
worker.js
160 lines (137 loc) · 3.81 KB
/
worker.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
'use strict';
const yargs = require('yargs');
const jstp = require('../..');
const parseAddress = require('./parse-address');
const statistics = require('../statistics');
const args = yargs
.option('address', {
alias: 'a',
describe: 'Address of master server',
type: 'string',
default: 'tcp://localhost:3000',
})
.option('connection', {
alias: 'c',
describe: 'Amount of connections',
type: 'number',
default: 128,
})
.option('interval', {
alias: 'i',
describe: 'Interval of requests sent by connection in ms',
type: 'number',
default: 10,
})
.option('size', {
alias: 's',
describe: 'Size of argument to call remote method with',
type: 'number',
default: 0,
})
.option('report', {
alias: 'r',
describe: 'Report interval in ms',
type: 'number',
default: 10000,
})
.help()
.alias('help', 'h')
.argv;
const {
address,
size: argumentSize,
connection: connectionAmount,
interval: requestInterval,
report: reportInterval,
} = args;
const [error, parsed] = parseAddress(address);
if (error) {
console.error(`Invalid master address: ${error.message}`);
process.exit(1);
}
const { transport, connect: masterAddress } = parsed;
// Prevent Node.js from throwing DEPTH_ZERO_SELF_SIGNED_CERT.
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
let api = null;
const connections = new Array(connectionAmount);
let requestTimer = null;
let reportTimer = null;
let reportCycleStartHR = null;
let responseTimesHR = [];
const argument = '0'.repeat(argumentSize);
let requests = 0;
const createConnection = (index, address) => {
jstp[transport].connectAndInspect(
'server', null, ['server'], ...address, (error, connection) => {
if (error) {
console.error(`Could not connect to server:\n${error}`);
return;
}
connections[index] = connection;
}
);
};
const report = () => {
const req = requests;
requests = 0;
const responseTimes = responseTimesHR.map(
([seconds, nanoseconds]) => seconds * 1e9 + nanoseconds
);
responseTimesHR = [];
const mean = statistics.mean(responseTimes) || 0;
const stdev = statistics.stdev(responseTimes, mean) || 0;
const timePassedHR = process.hrtime(reportCycleStartHR);
console.log(`${req} requests in ${timePassedHR[0]}s\n` +
`Mean response time: ${mean * 1e-6} ms, stdev: ${stdev * 1e-6} ms`
);
api.master.emit('workerReport', req, mean, stdev);
reportCycleStartHR = process.hrtime();
};
const request = () => {
const startTimeHR = process.hrtime();
connections.forEach(connection => {
connection.remoteProxies['server'].method(argument, (error, data) => {
if (error) {
console.error(`Error during call:\n${error}`);
return;
}
requests++;
responseTimesHR.push(process.hrtime(startTimeHR));
if (data !== argument) {
console.error('Server returned invalid data');
}
});
});
};
jstp[transport].connectAndInspect(
'master', null, ['master'], ...masterAddress, (error, connection, master) => {
if (error) {
console.error(`Couldn't connect to master:\n${error}`);
return;
}
api = master;
api.master.on('connect', address => {
for (let i = 0; i < connectionAmount; i++) {
createConnection(i, address);
}
});
api.master.registerWorker(error => {
if (error) {
console.error(`Couldn't register worker:\n${error}`);
return;
}
reportCycleStartHR = process.hrtime();
reportTimer = setInterval(report, reportInterval);
requestTimer = setInterval(request, requestInterval);
});
}
);
process.on('SIGINT', () => {
console.log('\nWorker is being closed');
clearInterval(requestTimer);
clearInterval(reportTimer);
connections.forEach(connection => {
connection.close();
});
process.exit(0);
});