From cf666d8529a8ef33db16ce3b6c5e9c9230530fa5 Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Sat, 30 Dec 2017 03:56:44 +0100 Subject: [PATCH] benchmark: (net) use destructuring PR-URL: https://github.com/nodejs/node/pull/18250 Reviewed-By: Matteo Collina Reviewed-By: James M Snell --- benchmark/net/net-c2s-cork.js | 60 +++++++--------- benchmark/net/net-c2s.js | 63 +++++++---------- benchmark/net/net-pipe.js | 69 ++++++++----------- benchmark/net/net-s2c.js | 60 +++++++--------- .../net/net-wrap-js-stream-passthrough.js | 47 +++++-------- benchmark/net/tcp-raw-c2s.js | 27 +++----- benchmark/net/tcp-raw-pipe.js | 29 +++----- benchmark/net/tcp-raw-s2c.js | 25 ++----- 8 files changed, 142 insertions(+), 238 deletions(-) diff --git a/benchmark/net/net-c2s-cork.js b/benchmark/net/net-c2s-cork.js index a6582caa16be56..55bb99f6f73d2b 100644 --- a/benchmark/net/net-c2s-cork.js +++ b/benchmark/net/net-c2s-cork.js @@ -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() {}; diff --git a/benchmark/net/net-c2s.js b/benchmark/net/net-c2s.js index 140f9612ab1ed9..4add79a1664d4a 100644 --- a/benchmark/net/net-c2s.js +++ b/benchmark/net/net-c2s.js @@ -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); - }); - }); -} diff --git a/benchmark/net/net-pipe.js b/benchmark/net/net-pipe.js index a8ae50edfbfde0..3dd3bb78ccf9ac 100644 --- a/benchmark/net/net-pipe.js +++ b/benchmark/net/net-pipe.js @@ -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); - }); - }); -} diff --git a/benchmark/net/net-s2c.js b/benchmark/net/net-s2c.js index 9fec2d8577c098..2ddf8fd6c5ff67 100644 --- a/benchmark/net/net-s2c.js +++ b/benchmark/net/net-s2c.js @@ -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); - }); - }); -} diff --git a/benchmark/net/net-wrap-js-stream-passthrough.js b/benchmark/net/net-wrap-js-stream-passthrough.js index bf84285e81b53a..05a66f4e7ab783 100644 --- a/benchmark/net/net-wrap-js-stream-passthrough.js +++ b/benchmark/net/net-wrap-js-stream-passthrough.js @@ -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); -} diff --git a/benchmark/net/tcp-raw-c2s.js b/benchmark/net/tcp-raw-c2s.js index bd41be87728308..2be3bb3b538ffd 100644 --- a/benchmark/net/tcp-raw-c2s.js +++ b/benchmark/net/tcp-raw-c2s.js @@ -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 fail(err, syscall) { + throw util._errnoException(err, syscall); } -function client() { +function client(type, len) { var chunk; switch (type) { case 'buf': diff --git a/benchmark/net/tcp-raw-pipe.js b/benchmark/net/tcp-raw-pipe.js index 4dd06ed446d6c1..2fc03f08cd4a90 100644 --- a/benchmark/net/tcp-raw-pipe.js +++ b/benchmark/net/tcp-raw-pipe.js @@ -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'); diff --git a/benchmark/net/tcp-raw-s2c.js b/benchmark/net/tcp-raw-s2c.js index 2ca6016ce017a1..339f5e393d9254 100644 --- a/benchmark/net/tcp-raw-s2c.js +++ b/benchmark/net/tcp-raw-s2c.js @@ -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 fail(err, syscall) { + throw util._errnoException(err, syscall); } -function client() { +function client(dur) { const clientHandle = new TCP(TCPConstants.SOCKET); const connectReq = new TCPConnectWrap(); const err = clientHandle.connect(connectReq, '127.0.0.1', PORT);