Skip to content
Open
4 changes: 2 additions & 2 deletions benchmark/_http-benchmarkers.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ const fs = require('fs');
const requirementsURL =
'https://github.com/nodejs/node/blob/HEAD/doc/contributing/writing-and-running-benchmarks.md#http-benchmark-requirements';

// The port used by servers and wrk
exports.PORT = Number(process.env.PORT) || 12346;
// The port used by servers and wrk. Zero lets the OS select an available port.
exports.PORT = process.env.PORT === '0' ? 0 : Number(process.env.PORT) || 12346;

class AutocannonBenchmarker {
constructor() {
Expand Down
1 change: 1 addition & 0 deletions benchmark/async_hooks/async-resource-vs-destroy.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ function main({ type, asyncMethod, connections, duration, path }) {
.on('listening', () => {

bench.http({
port: server.address().port,
path,
connections,
duration,
Expand Down
1 change: 1 addition & 0 deletions benchmark/async_hooks/http-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ function main({ asyncHooks, connections, duration }) {
const path = '/buffer/4/4/normal/1';

bench.http({
port: server.address().port,
connections,
path,
duration,
Expand Down
6 changes: 4 additions & 2 deletions benchmark/dgram/array-vs-concat.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ function main({ dur, len, n, type, chunks }) {
// Server
let sent = 0;
const socket = dgram.createSocket('udp4');
let port;
const onsend = type === 'concat' ? onsendConcat : onsendMulti;

function onsendConcat() {
Expand All @@ -33,7 +34,7 @@ function main({ dur, len, n, type, chunks }) {
// that only perform synchronous I/O on nonblocking UDP sockets.
setImmediate(() => {
for (let i = 0; i < n; i++) {
socket.send(Buffer.concat(chunk), PORT, '127.0.0.1', onsend);
socket.send(Buffer.concat(chunk), port, '127.0.0.1', onsend);
}
});
}
Expand All @@ -45,13 +46,14 @@ function main({ dur, len, n, type, chunks }) {
// that only perform synchronous I/O on nonblocking UDP sockets.
setImmediate(() => {
for (let i = 0; i < n; i++) {
socket.send(chunk, PORT, '127.0.0.1', onsend);
socket.send(chunk, port, '127.0.0.1', onsend);
}
});
}
}

socket.on('listening', () => {
port = socket.address().port;
bench.start();
onsend();

Expand Down
4 changes: 3 additions & 1 deletion benchmark/dgram/multi-buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,22 @@ function main({ dur, len, n, type, chunks }) {
let sent = 0;
let received = 0;
const socket = dgram.createSocket('udp4');
let port;

function onsend() {
if (sent++ % n === 0) {
// The setImmediate() is necessary to have event loop progress on OSes
// that only perform synchronous I/O on nonblocking UDP sockets.
setImmediate(() => {
for (let i = 0; i < n; i++) {
socket.send(chunk, PORT, '127.0.0.1', onsend);
socket.send(chunk, port, '127.0.0.1', onsend);
}
});
}
}

socket.on('listening', () => {
port = socket.address().port;
bench.start();
onsend();

Expand Down
4 changes: 3 additions & 1 deletion benchmark/dgram/offset-length.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,22 @@ function main({ dur, len, n, type }) {
let sent = 0;
let received = 0;
const socket = dgram.createSocket('udp4');
let port;

function onsend() {
if (sent++ % n === 0) {
// The setImmediate() is necessary to have event loop progress on OSes
// that only perform synchronous I/O on nonblocking UDP sockets.
setImmediate(() => {
for (let i = 0; i < n; i++) {
socket.send(chunk, 0, chunk.length, PORT, '127.0.0.1', onsend);
socket.send(chunk, 0, chunk.length, port, '127.0.0.1', onsend);
}
});
}
}

socket.on('listening', () => {
port = socket.address().port;
bench.start();
onsend();

Expand Down
4 changes: 3 additions & 1 deletion benchmark/dgram/send-to-ip.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,20 @@ function main({ dur, n }) {
const chunk = Buffer.allocUnsafe(1);
let sent = 0;
const socket = dgram.createSocket('udp4');
let port;

function onsend() {
if (sent++ % n === 0) {
setImmediate(() => {
for (let i = 0; i < n; i++) {
socket.send(chunk, PORT, '127.0.0.1', onsend);
socket.send(chunk, port, '127.0.0.1', onsend);
}
});
}
}

socket.on('listening', () => {
port = socket.address().port;
bench.start();
onsend();

Expand Down
4 changes: 3 additions & 1 deletion benchmark/dgram/single-buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,22 @@ function main({ dur, len, n, type }) {
let sent = 0;
let received = 0;
const socket = dgram.createSocket('udp4');
let port;

function onsend() {
if (sent++ % n === 0) {
// The setImmediate() is necessary to have event loop progress on OSes
// that only perform synchronous I/O on nonblocking UDP sockets.
setImmediate(() => {
for (let i = 0; i < n; i++) {
socket.send(chunk, PORT, '127.0.0.1', onsend);
socket.send(chunk, port, '127.0.0.1', onsend);
}
});
}
}

socket.on('listening', () => {
port = socket.address().port;
bench.start();
onsend();

Expand Down
1 change: 1 addition & 0 deletions benchmark/diagnostics_channel/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ function main({ apm, connections, duration, type, len, chunks, chunkedEnc }) {
.on('listening', () => {
const path = `/${type}/${len}/${chunks}/normal/${chunkedEnc}`;
bench.http({
port: server.address().port,
path,
connections,
duration,
Expand Down
3 changes: 2 additions & 1 deletion benchmark/http/cluster.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function main({ type, len, c, duration }) {
const w1 = cluster.fork();
const w2 = cluster.fork();

cluster.on('listening', () => {
cluster.on('listening', (worker, address) => {
workers++;
if (workers < 2)
return;
Expand All @@ -32,6 +32,7 @@ function main({ type, len, c, duration }) {
const path = `/${type}/${len}`;

bench.http({
port: address.port,
path: path,
connections: c,
duration,
Expand Down
1 change: 1 addition & 0 deletions benchmark/https/simple.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ function main({ type, len, chunks, c, chunkedEnc, duration }) {
const path = `/${type}/${len}/${chunks}/${chunkedEnc}`;

bench.http({
port: server.address().port,
path,
connections: c,
scheme: 'https',
Expand Down
2 changes: 1 addition & 1 deletion benchmark/net/net-c2s-cork.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function main({ dur, len, type }) {
});

server.listen(PORT, () => {
const socket = net.connect(PORT);
const socket = net.connect(server.address().port);
socket.on('connect', () => {
bench.start();

Expand Down
2 changes: 1 addition & 1 deletion benchmark/net/net-c2s.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function main({ dur, len, type }) {
});

server.listen(PORT, () => {
const socket = net.connect(PORT);
const socket = net.connect(server.address().port);
socket.on('connect', () => {
bench.start();

Expand Down
2 changes: 1 addition & 1 deletion benchmark/net/net-pipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function main({ dur, len, type }) {
});

server.listen(PORT, () => {
const socket = net.connect(PORT);
const socket = net.connect(server.address().port);
socket.on('connect', () => {
bench.start();

Expand Down
1 change: 1 addition & 0 deletions benchmark/net/net-s2c.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ function main({ dur, sendchunklen, type, recvbuflen, recvbufgenfn }) {
});

server.listen(PORT, () => {
socketOpts.port = server.address().port;
const socket = net.connect(socketOpts);
socket.on('connect', () => {
bench.start();
Expand Down
7 changes: 6 additions & 1 deletion benchmark/net/tcp-raw-c2s.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ function main({ dur, len, type }) {
if (err)
fail(err, 'listen');

const address = {};
err = serverHandle.getsockname(address);
if (err)
fail(err, 'getsockname');

serverHandle.onconnection = function(err, clientHandle) {
if (err)
fail(err, 'connect');
Expand Down Expand Up @@ -89,7 +94,7 @@ function main({ dur, len, type }) {

const clientHandle = new TCP(TCPConstants.SOCKET);
const connectReq = new TCPConnectWrap();
const err = clientHandle.connect(connectReq, '127.0.0.1', PORT);
const err = clientHandle.connect(connectReq, '127.0.0.1', address.port);

if (err)
fail(err, 'connect');
Expand Down
7 changes: 6 additions & 1 deletion benchmark/net/tcp-raw-pipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ function main({ dur, len, type }) {
if (err)
fail(err, 'listen');

const address = {};
err = serverHandle.getsockname(address);
if (err)
fail(err, 'getsockname');

serverHandle.onconnection = function(err, clientHandle) {
if (err)
fail(err, 'connect');
Expand Down Expand Up @@ -93,7 +98,7 @@ function main({ dur, len, type }) {
const connectReq = new TCPConnectWrap();
let bytes = 0;

err = clientHandle.connect(connectReq, '127.0.0.1', PORT);
err = clientHandle.connect(connectReq, '127.0.0.1', address.port);
if (err)
fail(err, 'connect');

Expand Down
7 changes: 6 additions & 1 deletion benchmark/net/tcp-raw-s2c.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ function main({ dur, len, type }) {
if (err)
fail(err, 'listen');

const address = {};
err = serverHandle.getsockname(address);
if (err)
fail(err, 'getsockname');

serverHandle.onconnection = function(err, clientHandle) {
if (err)
fail(err, 'connect');
Expand Down Expand Up @@ -107,7 +112,7 @@ function main({ dur, len, type }) {
function client(dur) {
const clientHandle = new TCP(TCPConstants.SOCKET);
const connectReq = new TCPConnectWrap();
const err = clientHandle.connect(connectReq, '127.0.0.1', PORT);
const err = clientHandle.connect(connectReq, '127.0.0.1', address.port);

if (err)
fail(err, 'connect');
Expand Down
4 changes: 2 additions & 2 deletions benchmark/streams/compose.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ const bench = common.createBenchmark(main, {
return type === 'creation' ? n === 1e3 : n === 1;
},
test: {
n: [1, 1e3],
type: ['creation', 'throughput'],
n: 1,
type: 'throughput',
},
});

Expand Down
6 changes: 3 additions & 3 deletions benchmark/tls/secure-pair.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const fixtures = require('../../test/common/fixtures');
const tls = require('tls');
const net = require('net');

const REDIRECT_PORT = 28347;
const REDIRECT_PORT = common.PORT === 0 ? 0 : 28347;

function main({ dur, size, securing }) {
const chunk = Buffer.alloc(size, 'b');
Expand All @@ -33,7 +33,7 @@ function main({ dur, size, securing }) {
const proxy = net.createServer(onProxyConnection);
proxy.listen(common.PORT, () => {
const clientOptions = {
port: common.PORT,
port: proxy.address().port,
ca: options.ca,
key: options.key,
cert: options.cert,
Expand Down Expand Up @@ -66,7 +66,7 @@ function main({ dur, size, securing }) {
});

function onProxyConnection(conn) {
const client = net.connect(REDIRECT_PORT, () => {
const client = net.connect(server.address().port, () => {
switch (securing) {
case 'TLSSocket':
secureTLSSocket(conn, client);
Expand Down
2 changes: 1 addition & 1 deletion benchmark/tls/throughput-c2s.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ function main({ dur, type, size }) {
const server = tls.createServer(options, onConnection);
let conn;
server.listen(common.PORT, () => {
const opt = { port: common.PORT, rejectUnauthorized: false };
const opt = { port: server.address().port, rejectUnauthorized: false };
conn = tls.connect(opt, () => {
setTimeout(done, dur * 1000);
bench.start();
Expand Down
1 change: 1 addition & 0 deletions benchmark/tls/throughput-s2c.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ function main({ dur, type, sendchunklen, recvbuflen, recvbufgenfn }) {

let conn;
server.listen(common.PORT, () => {
socketOpts.port = server.address().port;
conn = tls.connect(socketOpts, () => {
setTimeout(done, dur * 1000);
bench.start();
Expand Down
4 changes: 3 additions & 1 deletion benchmark/tls/tls-connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ let clientConn = 0;
let serverConn = 0;
let dur;
let concurrency;
let port;
let running = true;

function main(conf) {
Expand All @@ -30,6 +31,7 @@ function main(conf) {
}

function onListening() {
port = this.address().port;
setTimeout(done, dur * 1000);
bench.start();
for (let i = 0; i < concurrency; i++)
Expand All @@ -42,7 +44,7 @@ function onConnection(conn) {

function makeConnection() {
const options = {
port: common.PORT,
port,
rejectUnauthorized: false,
};
const conn = tls.connect(options, () => {
Expand Down
Loading
Loading