benchmark: (net) use destructuring

PR-URL: https://github.com/nodejs/node/pull/18250
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Ruben Bridgewater 2017-12-30 03:56:44 +01:00
parent d163a6b8c2
commit 19bdfa5380
No known key found for this signature in database
GPG Key ID: F07496B3EB3C1762
8 changed files with 142 additions and 238 deletions

View File

@ -2,6 +2,7 @@
'use strict'; 'use strict';
const common = require('../common.js'); const common = require('../common.js');
const net = require('net');
const PORT = common.PORT; const PORT = common.PORT;
const bench = common.createBenchmark(main, { const bench = common.createBenchmark(main, {
@ -10,17 +11,10 @@ const bench = common.createBenchmark(main, {
dur: [5], dur: [5],
}); });
var dur;
var len;
var type;
var chunk; var chunk;
var encoding; var encoding;
function main(conf) { function main({ dur, len, type }) {
dur = +conf.dur;
len = +conf.len;
type = conf.type;
switch (type) { switch (type) {
case 'buf': case 'buf':
chunk = Buffer.alloc(len, 'x'); chunk = Buffer.alloc(len, 'x');
@ -37,34 +31,6 @@ function main(conf) {
throw new Error(`invalid type: ${type}`); throw new Error(`invalid type: ${type}`);
} }
server();
}
const net = require('net');
function Writer() {
this.received = 0;
this.writable = true;
}
Writer.prototype.write = function(chunk, encoding, cb) {
this.received += chunk.length;
if (typeof encoding === 'function')
encoding();
else if (typeof cb === 'function')
cb();
return true;
};
// doesn't matter, never emits anything.
Writer.prototype.on = function() {};
Writer.prototype.once = function() {};
Writer.prototype.emit = function() {};
Writer.prototype.prependListener = function() {};
function server() {
const writer = new Writer(); const writer = new Writer();
// the actual benchmark. // the actual benchmark.
@ -95,3 +61,25 @@ function server() {
}); });
}); });
} }
function Writer() {
this.received = 0;
this.writable = true;
}
Writer.prototype.write = function(chunk, encoding, cb) {
this.received += chunk.length;
if (typeof encoding === 'function')
encoding();
else if (typeof cb === 'function')
cb();
return true;
};
// doesn't matter, never emits anything.
Writer.prototype.on = function() {};
Writer.prototype.once = function() {};
Writer.prototype.emit = function() {};
Writer.prototype.prependListener = function() {};

View File

@ -2,6 +2,7 @@
'use strict'; 'use strict';
const common = require('../common.js'); const common = require('../common.js');
const net = require('net');
const PORT = common.PORT; const PORT = common.PORT;
const bench = common.createBenchmark(main, { const bench = common.createBenchmark(main, {
@ -10,17 +11,10 @@ const bench = common.createBenchmark(main, {
dur: [5], dur: [5],
}); });
var dur;
var len;
var type;
var chunk; var chunk;
var encoding; var encoding;
function main(conf) { function main({ dur, len, type }) {
dur = +conf.dur;
len = +conf.len;
type = conf.type;
switch (type) { switch (type) {
case 'buf': case 'buf':
chunk = Buffer.alloc(len, 'x'); chunk = Buffer.alloc(len, 'x');
@ -37,10 +31,30 @@ function main(conf) {
throw new Error(`invalid type: ${type}`); throw new Error(`invalid type: ${type}`);
} }
server(); const reader = new Reader();
} const writer = new Writer();
const net = require('net'); // the actual benchmark.
const server = net.createServer(function(socket) {
socket.pipe(writer);
});
server.listen(PORT, function() {
const socket = net.connect(PORT);
socket.on('connect', function() {
bench.start();
reader.pipe(socket);
setTimeout(function() {
const bytes = writer.received;
const gbits = (bytes * 8) / (1024 * 1024 * 1024);
bench.end(gbits);
process.exit(0);
}, dur * 1000);
});
});
}
function Writer() { function Writer() {
this.received = 0; this.received = 0;
@ -84,30 +98,3 @@ Reader.prototype.pipe = function(dest) {
this.flow(); this.flow();
return dest; return dest;
}; };
function server() {
const reader = new Reader();
const writer = new Writer();
// the actual benchmark.
const server = net.createServer(function(socket) {
socket.pipe(writer);
});
server.listen(PORT, function() {
const socket = net.connect(PORT);
socket.on('connect', function() {
bench.start();
reader.pipe(socket);
setTimeout(function() {
const bytes = writer.received;
const gbits = (bytes * 8) / (1024 * 1024 * 1024);
bench.end(gbits);
process.exit(0);
}, dur * 1000);
});
});
}

View File

@ -2,6 +2,7 @@
'use strict'; 'use strict';
const common = require('../common.js'); const common = require('../common.js');
const net = require('net');
const PORT = common.PORT; const PORT = common.PORT;
const bench = common.createBenchmark(main, { const bench = common.createBenchmark(main, {
@ -10,17 +11,10 @@ const bench = common.createBenchmark(main, {
dur: [5], dur: [5],
}); });
var dur;
var len;
var type;
var chunk; var chunk;
var encoding; var encoding;
function main(conf) { function main({ dur, len, type }) {
dur = +conf.dur;
len = +conf.len;
type = conf.type;
switch (type) { switch (type) {
case 'buf': case 'buf':
chunk = Buffer.alloc(len, 'x'); chunk = Buffer.alloc(len, 'x');
@ -37,10 +31,33 @@ function main(conf) {
throw new Error(`invalid type: ${type}`); throw new Error(`invalid type: ${type}`);
} }
server(); const reader = new Reader();
} const writer = new Writer();
const net = require('net'); // the actual benchmark.
const server = net.createServer(function(socket) {
socket.pipe(socket);
});
server.listen(PORT, function() {
const socket = net.connect(PORT);
socket.on('connect', function() {
bench.start();
reader.pipe(socket);
socket.pipe(writer);
setTimeout(function() {
// multiply by 2 since we're sending it first one way
// then then back again.
const bytes = writer.received * 2;
const gbits = (bytes * 8) / (1024 * 1024 * 1024);
bench.end(gbits);
process.exit(0);
}, dur * 1000);
});
});
}
function Writer() { function Writer() {
this.received = 0; this.received = 0;
@ -84,33 +101,3 @@ Reader.prototype.pipe = function(dest) {
this.flow(); this.flow();
return dest; return dest;
}; };
function server() {
const reader = new Reader();
const writer = new Writer();
// the actual benchmark.
const server = net.createServer(function(socket) {
socket.pipe(socket);
});
server.listen(PORT, function() {
const socket = net.connect(PORT);
socket.on('connect', function() {
bench.start();
reader.pipe(socket);
socket.pipe(writer);
setTimeout(function() {
// multiply by 2 since we're sending it first one way
// then then back again.
const bytes = writer.received * 2;
const gbits = (bytes * 8) / (1024 * 1024 * 1024);
bench.end(gbits);
process.exit(0);
}, dur * 1000);
});
});
}

View File

@ -10,17 +10,10 @@ const bench = common.createBenchmark(main, {
dur: [5] dur: [5]
}); });
var dur;
var len;
var type;
var chunk; var chunk;
var encoding; var encoding;
function main(conf) { function main({ dur, len, type }) {
dur = +conf.dur;
len = +conf.len;
type = conf.type;
switch (type) { switch (type) {
case 'buf': case 'buf':
chunk = Buffer.alloc(len, 'x'); chunk = Buffer.alloc(len, 'x');
@ -37,7 +30,29 @@ function main(conf) {
throw new Error(`invalid type: ${type}`); throw new Error(`invalid type: ${type}`);
} }
server(); const reader = new Reader();
const writer = new Writer();
// the actual benchmark.
const server = net.createServer(function(socket) {
reader.pipe(socket);
});
server.listen(PORT, function() {
const socket = net.connect(PORT);
socket.on('connect', function() {
bench.start();
socket.pipe(writer);
setTimeout(function() {
const bytes = writer.received;
const gbits = (bytes * 8) / (1024 * 1024 * 1024);
bench.end(gbits);
process.exit(0);
}, dur * 1000);
});
});
} }
const net = require('net'); const net = require('net');
@ -84,30 +99,3 @@ Reader.prototype.pipe = function(dest) {
this.flow(); this.flow();
return dest; return dest;
}; };
function server() {
const reader = new Reader();
const writer = new Writer();
// the actual benchmark.
const server = net.createServer(function(socket) {
reader.pipe(socket);
});
server.listen(PORT, function() {
const socket = net.connect(PORT);
socket.on('connect', function() {
bench.start();
socket.pipe(writer);
setTimeout(function() {
const bytes = writer.received;
const gbits = (bytes * 8) / (1024 * 1024 * 1024);
bench.end(gbits);
process.exit(0);
}, dur * 1000);
});
});
}

View File

@ -12,18 +12,12 @@ const bench = common.createBenchmark(main, {
flags: ['--expose-internals'] flags: ['--expose-internals']
}); });
var dur;
var len;
var type;
var chunk; var chunk;
var encoding; var encoding;
var JSStreamWrap; // Can only require internals inside main().
function main(conf) { function main({ dur, len, type }) {
dur = +conf.dur; // Can only require internals inside main().
len = +conf.len; const JSStreamWrap = require('internal/wrap_js_stream');
type = conf.type;
JSStreamWrap = require('internal/wrap_js_stream');
switch (type) { switch (type) {
case 'buf': case 'buf':
@ -41,7 +35,21 @@ function main(conf) {
throw new Error(`invalid type: ${type}`); throw new Error(`invalid type: ${type}`);
} }
doBenchmark(); const reader = new Reader();
const writer = new Writer();
// the actual benchmark.
const fakeSocket = new JSStreamWrap(new PassThrough());
bench.start();
reader.pipe(fakeSocket);
fakeSocket.pipe(writer);
setTimeout(function() {
const bytes = writer.received;
const gbits = (bytes * 8) / (1024 * 1024 * 1024);
bench.end(gbits);
process.exit(0);
}, dur * 1000);
} }
function Writer() { function Writer() {
@ -86,22 +94,3 @@ Reader.prototype.pipe = function(dest) {
this.flow(); this.flow();
return dest; return dest;
}; };
function doBenchmark() {
const reader = new Reader();
const writer = new Writer();
// the actual benchmark.
const fakeSocket = new JSStreamWrap(new PassThrough());
bench.start();
reader.pipe(fakeSocket);
fakeSocket.pipe(writer);
setTimeout(function() {
const bytes = writer.received;
const gbits = (bytes * 8) / (1024 * 1024 * 1024);
bench.end(gbits);
process.exit(0);
}, dur * 1000);
}

View File

@ -19,23 +19,7 @@ const TCPConnectWrap = process.binding('tcp_wrap').TCPConnectWrap;
const WriteWrap = process.binding('stream_wrap').WriteWrap; const WriteWrap = process.binding('stream_wrap').WriteWrap;
const PORT = common.PORT; const PORT = common.PORT;
var dur; function main({ dur, len, type }) {
var len;
var type;
function main(conf) {
dur = +conf.dur;
len = +conf.len;
type = conf.type;
server();
}
function fail(err, syscall) {
throw util._errnoException(err, syscall);
}
function server() {
const serverHandle = new TCP(TCPConstants.SERVER); const serverHandle = new TCP(TCPConstants.SERVER);
var err = serverHandle.bind('127.0.0.1', PORT); var err = serverHandle.bind('127.0.0.1', PORT);
if (err) if (err)
@ -73,10 +57,15 @@ function server() {
clientHandle.readStart(); clientHandle.readStart();
}; };
client(); client(type, len);
} }
function client() {
function fail(err, syscall) {
throw util._errnoException(err, syscall);
}
function client(type, len) {
var chunk; var chunk;
switch (type) { switch (type) {
case 'buf': case 'buf':

View File

@ -14,27 +14,17 @@ const bench = common.createBenchmark(main, {
dur: [5] dur: [5]
}); });
function fail(err, syscall) {
throw util._errnoException(err, syscall);
}
const { TCP, constants: TCPConstants } = process.binding('tcp_wrap'); const { TCP, constants: TCPConstants } = process.binding('tcp_wrap');
const TCPConnectWrap = process.binding('tcp_wrap').TCPConnectWrap; const TCPConnectWrap = process.binding('tcp_wrap').TCPConnectWrap;
const WriteWrap = process.binding('stream_wrap').WriteWrap; const WriteWrap = process.binding('stream_wrap').WriteWrap;
const PORT = common.PORT; const PORT = common.PORT;
var dur; function main({ dur, len, type }) {
var len; // Server
var type;
function main(conf) {
dur = +conf.dur;
len = +conf.len;
type = conf.type;
server();
}
function fail(err, syscall) {
throw util._errnoException(err, syscall);
}
function server() {
const serverHandle = new TCP(TCPConstants.SERVER); const serverHandle = new TCP(TCPConstants.SERVER);
var err = serverHandle.bind('127.0.0.1', PORT); var err = serverHandle.bind('127.0.0.1', PORT);
if (err) if (err)
@ -70,10 +60,7 @@ function server() {
clientHandle.readStart(); clientHandle.readStart();
}; };
client(); // Client
}
function client() {
var chunk; var chunk;
switch (type) { switch (type) {
case 'buf': case 'buf':
@ -91,9 +78,9 @@ function client() {
const clientHandle = new TCP(TCPConstants.SOCKET); const clientHandle = new TCP(TCPConstants.SOCKET);
const connectReq = new TCPConnectWrap(); const connectReq = new TCPConnectWrap();
const err = clientHandle.connect(connectReq, '127.0.0.1', PORT);
var bytes = 0; var bytes = 0;
err = clientHandle.connect(connectReq, '127.0.0.1', PORT);
if (err) if (err)
fail(err, 'connect'); fail(err, 'connect');

View File

@ -19,22 +19,7 @@ const TCPConnectWrap = process.binding('tcp_wrap').TCPConnectWrap;
const WriteWrap = process.binding('stream_wrap').WriteWrap; const WriteWrap = process.binding('stream_wrap').WriteWrap;
const PORT = common.PORT; const PORT = common.PORT;
var dur; function main({ dur, len, type }) {
var len;
var type;
function main(conf) {
dur = +conf.dur;
len = +conf.len;
type = conf.type;
server();
}
function fail(err, syscall) {
throw util._errnoException(err, syscall);
}
function server() {
const serverHandle = new TCP(TCPConstants.SERVER); const serverHandle = new TCP(TCPConstants.SERVER);
var err = serverHandle.bind('127.0.0.1', PORT); var err = serverHandle.bind('127.0.0.1', PORT);
if (err) if (err)
@ -103,10 +88,14 @@ function server() {
} }
}; };
client(); client(dur);
} }
function client() { function fail(err, syscall) {
throw util._errnoException(err, syscall);
}
function client(dur) {
const clientHandle = new TCP(TCPConstants.SOCKET); const clientHandle = new TCP(TCPConstants.SOCKET);
const connectReq = new TCPConnectWrap(); const connectReq = new TCPConnectWrap();
const err = clientHandle.connect(connectReq, '127.0.0.1', PORT); const err = clientHandle.connect(connectReq, '127.0.0.1', PORT);