-
Notifications
You must be signed in to change notification settings - Fork 10
/
master.js
172 lines (145 loc) · 4.17 KB
/
master.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
'use strict';
const yargs = require('yargs');
const fs = require('fs');
const path = require('path');
const jstp = require('../..');
const parseAddress = require('./parse-address');
const statistics = require('../statistics');
const args = yargs
.option('address', {
alias: 'a',
describe: 'Address for master server to listen on',
type: 'string',
default: 'tcp://localhost:3000',
})
.options('report', {
alias: 'r',
describe: 'Report interval in ms',
type: 'number',
default: 10000,
})
.options('key', {
alias: 'k',
describe: 'Path to file that contains SSL certificate key',
type: 'string',
coerce:
arg => (arg ? fs.readFileSync(path.resolve(__dirname, arg)) : undefined),
})
.options('cert', {
alias: 'c',
describe: 'Path to file that contains SSL certificate',
type: 'string',
coerce:
arg => (arg ? fs.readFileSync(path.resolve(__dirname, arg)) : undefined),
})
.help()
.alias('help', 'h')
.argv;
const {
address,
report: reportInterval,
key,
cert,
} = args;
const [error, parsed] = parseAddress(address);
if (error) {
console.error(`Invalid master address: ${error.message}`);
process.exit(1);
}
const { transport, listen: listenAddress } = parsed;
let startTimeHR = null;
let workersReportBuffer = [];
const reports = [];
const workers = [];
let server = null;
let master = null;
let serverAddress = null;
const application = {
master: {
registerServer: (connection, address, callback) => {
server = connection;
serverAddress = address;
workers.forEach(worker => {
worker.emitRemoteEvent('worker', 'connect', [serverAddress]);
});
callback(null);
},
registerWorker: (connection, callback) => {
workers.push(connection);
if (serverAddress) {
connection.emitRemoteEvent('master', 'connect', [serverAddress]);
}
callback(null);
},
},
};
const eventHandlers = {
master: {
workerReport: (connection, count, mean, stdev) => {
console.log(count, mean, stdev);
workersReportBuffer.push({ count, mean, stdev });
},
serverReport: (connection, connections) => {
server.connections = connections;
},
},
};
const config = {
applications: [new jstp.Application('master', application, eventHandlers)],
key,
cert,
};
// Prevent Node.js from throwing DEPTH_ZERO_SELF_SIGNED_CERT.
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
master = jstp[transport].createServer(config);
master.listen(listenAddress, () => {
console.log(`Master server listening on ${address}\n`);
startTimeHR = process.hrtime();
let intervalTimerHR = process.hrtime();
const report = () => {
const timeFromStartHR = process.hrtime(startTimeHR);
const timePassedHR = process.hrtime(intervalTimerHR);
if (!workersReportBuffer.length) {
return;
}
const timeFromStart = timeFromStartHR[0] * 1e9 + timeFromStartHR[1];
const timePassed = timePassedHR[0] * 1e9 + timePassedHR[1];
const sample = statistics.combineSamples(workersReportBuffer);
if (sample.count === 0) {
sample.mean = 0;
sample.stdev = 0;
}
workersReportBuffer = [];
reports.push([sample, server.connections, timeFromStartHR]);
const stat = statistics.combineSamples(
reports.map(element => element[0])
);
const logStats = (sample, timePassed) => {
console.log(`Current stats:
RPS : ${sample.count / timePassed * 1e9}
Response time mean : ${sample.mean * 1e-6} ms
Response time stdev: ${sample.stdev * 1e-6} ms\n`
);
};
logStats(sample, timePassed);
logStats(stat, timeFromStart);
console.log(`Current connections to server: ${server.connections}`);
console.log('\n' + '='.repeat(80) + '\n');
intervalTimerHR = process.hrtime();
};
setTimeout(() => {
setInterval(report, reportInterval);
}, reportInterval);
});
process.on('SIGINT', () => {
console.log('\nMaster server is being closed');
master.close(error => {
if (error) {
console.log(`Error occured when closing:\n${error}`);
if (transport === 'ipc') {
fs.unlink(listenAddress);
}
}
process.exit(0);
});
});