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';
const common = require('../common.js');
const net = require('net');
const PORT = common.PORT;
const bench = common.createBenchmark(main, {
@ -10,17 +11,10 @@ const bench = common.createBenchmark(main, {
dur: [5],
});
var dur;
var len;
var type;
var chunk;
var encoding;
function main(conf) {
dur = +conf.dur;
len = +conf.len;
type = conf.type;
function main({ dur, len, type }) {
switch (type) {
case 'buf':
chunk = Buffer.alloc(len, 'x');
@ -37,34 +31,6 @@ function main(conf) {
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();
// 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';
const common = require('../common.js');
const net = require('net');
const PORT = common.PORT;
const bench = common.createBenchmark(main, {
@ -10,17 +11,10 @@ const bench = common.createBenchmark(main, {
dur: [5],
});
var dur;
var len;
var type;
var chunk;
var encoding;
function main(conf) {
dur = +conf.dur;
len = +conf.len;
type = conf.type;
function main({ dur, len, type }) {
switch (type) {
case 'buf':
chunk = Buffer.alloc(len, 'x');
@ -37,10 +31,30 @@ function main(conf) {
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() {
this.received = 0;
@ -84,30 +98,3 @@ Reader.prototype.pipe = function(dest) {
this.flow();
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';
const common = require('../common.js');
const net = require('net');
const PORT = common.PORT;
const bench = common.createBenchmark(main, {
@ -10,17 +11,10 @@ const bench = common.createBenchmark(main, {
dur: [5],
});
var dur;
var len;
var type;
var chunk;
var encoding;
function main(conf) {
dur = +conf.dur;
len = +conf.len;
type = conf.type;
function main({ dur, len, type }) {
switch (type) {
case 'buf':
chunk = Buffer.alloc(len, 'x');
@ -37,10 +31,33 @@ function main(conf) {
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() {
this.received = 0;
@ -84,33 +101,3 @@ Reader.prototype.pipe = function(dest) {
this.flow();
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]
});
var dur;
var len;
var type;
var chunk;
var encoding;
function main(conf) {
dur = +conf.dur;
len = +conf.len;
type = conf.type;
function main({ dur, len, type }) {
switch (type) {
case 'buf':
chunk = Buffer.alloc(len, 'x');
@ -37,7 +30,29 @@ function main(conf) {
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');
@ -84,30 +99,3 @@ Reader.prototype.pipe = function(dest) {
this.flow();
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']
});
var dur;
var len;
var type;
var chunk;
var encoding;
var JSStreamWrap; // Can only require internals inside main().
function main(conf) {
dur = +conf.dur;
len = +conf.len;
type = conf.type;
JSStreamWrap = require('internal/wrap_js_stream');
function main({ dur, len, type }) {
// Can only require internals inside main().
const JSStreamWrap = require('internal/wrap_js_stream');
switch (type) {
case 'buf':
@ -41,7 +35,21 @@ function main(conf) {
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() {
@ -86,22 +94,3 @@ Reader.prototype.pipe = function(dest) {
this.flow();
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 PORT = common.PORT;
var dur;
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() {
function main({ dur, len, type }) {
const serverHandle = new TCP(TCPConstants.SERVER);
var err = serverHandle.bind('127.0.0.1', PORT);
if (err)
@ -73,10 +57,15 @@ function server() {
clientHandle.readStart();
};
client();
client(type, len);
}
function client() {
function fail(err, syscall) {
throw util._errnoException(err, syscall);
}
function client(type, len) {
var chunk;
switch (type) {
case 'buf':

View File

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

View File

@ -19,22 +19,7 @@ const TCPConnectWrap = process.binding('tcp_wrap').TCPConnectWrap;
const WriteWrap = process.binding('stream_wrap').WriteWrap;
const PORT = common.PORT;
var dur;
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() {
function main({ dur, len, type }) {
const serverHandle = new TCP(TCPConstants.SERVER);
var err = serverHandle.bind('127.0.0.1', PORT);
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 connectReq = new TCPConnectWrap();
const err = clientHandle.connect(connectReq, '127.0.0.1', PORT);