2017-05-16 17:08:49 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const common = require('../common.js');
|
|
|
|
const net = require('net');
|
|
|
|
|
|
|
|
const bench = common.createBenchmark(main, {
|
|
|
|
n: [5, 1000]
|
|
|
|
});
|
|
|
|
|
|
|
|
const reqData = 'GET / HTTP/1.1\r\n' +
|
|
|
|
'Upgrade: WebSocket\r\n' +
|
|
|
|
'Connection: Upgrade\r\n' +
|
|
|
|
'\r\n' +
|
|
|
|
'WjN}|M(6';
|
|
|
|
|
|
|
|
const resData = 'HTTP/1.1 101 Web Socket Protocol Handshake\r\n' +
|
|
|
|
'Upgrade: WebSocket\r\n' +
|
|
|
|
'Connection: Upgrade\r\n' +
|
|
|
|
'\r\n\r\n';
|
|
|
|
|
|
|
|
function main({ n }) {
|
2019-03-26 05:21:27 +01:00
|
|
|
const server = require('../fixtures/simple-http-server.js')
|
2017-05-16 17:08:49 +02:00
|
|
|
.listen(common.PORT)
|
2019-02-05 07:06:08 +01:00
|
|
|
.on('listening', () => {
|
2017-05-16 17:08:49 +02:00
|
|
|
bench.start();
|
2019-02-05 07:06:08 +01:00
|
|
|
doBench(server.address(), n, () => {
|
2017-05-16 17:08:49 +02:00
|
|
|
bench.end(n);
|
|
|
|
server.close();
|
|
|
|
});
|
|
|
|
})
|
2019-02-05 07:06:08 +01:00
|
|
|
.on('upgrade', (req, socket, upgradeHead) => {
|
2017-05-16 17:08:49 +02:00
|
|
|
socket.resume();
|
|
|
|
socket.write(resData);
|
|
|
|
socket.end();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function doBench(address, count, done) {
|
|
|
|
if (count === 0) {
|
|
|
|
done();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const conn = net.createConnection(address.port);
|
|
|
|
conn.write(reqData);
|
|
|
|
conn.resume();
|
|
|
|
|
2019-02-05 07:06:08 +01:00
|
|
|
conn.on('end', () => {
|
2017-05-16 17:08:49 +02:00
|
|
|
doBench(address, count - 1, done);
|
|
|
|
});
|
|
|
|
}
|