1'use strict'; 2 3const common = require('../common.js'); 4const net = require('net'); 5 6const bench = common.createBenchmark(main, { 7 n: [5, 1000] 8}); 9 10const reqData = 'GET / HTTP/1.1\r\n' + 11 'Upgrade: WebSocket\r\n' + 12 'Connection: Upgrade\r\n' + 13 '\r\n' + 14 'WjN}|M(6'; 15 16const resData = 'HTTP/1.1 101 Web Socket Protocol Handshake\r\n' + 17 'Upgrade: WebSocket\r\n' + 18 'Connection: Upgrade\r\n' + 19 '\r\n\r\n'; 20 21function main({ n }) { 22 const server = require('../fixtures/simple-http-server.js') 23 .listen(common.PORT) 24 .on('listening', () => { 25 bench.start(); 26 doBench(server.address(), n, () => { 27 bench.end(n); 28 server.close(); 29 }); 30 }) 31 .on('upgrade', (req, socket, upgradeHead) => { 32 socket.resume(); 33 socket.write(resData); 34 socket.end(); 35 }); 36} 37 38function doBench(address, count, done) { 39 if (count === 0) { 40 done(); 41 return; 42 } 43 44 const conn = net.createConnection(address.port); 45 conn.write(reqData); 46 conn.resume(); 47 48 conn.on('end', () => { 49 doBench(address, count - 1, done); 50 }); 51} 52