Skip to content

Commit

Permalink
benchmark: (net) use destructuring
Browse files Browse the repository at this point in the history
PR-URL: #18250
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
BridgeAR authored and evanlucas committed Jan 30, 2018
1 parent 88f4bf2 commit cf666d8
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 238 deletions.
60 changes: 24 additions & 36 deletions benchmark/net/net-c2s-cork.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
'use strict';

const common = require('../common.js');
const net = require('net');
const PORT = common.PORT;

const bench = common.createBenchmark(main, {
Expand All @@ -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');
Expand All @@ -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.
Expand Down Expand Up @@ -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() {};
63 changes: 25 additions & 38 deletions benchmark/net/net-c2s.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
'use strict';

const common = require('../common.js');
const net = require('net');
const PORT = common.PORT;

const bench = common.createBenchmark(main, {
Expand All @@ -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');
Expand All @@ -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;
Expand Down Expand Up @@ -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);
});
});
}
69 changes: 28 additions & 41 deletions benchmark/net/net-pipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
'use strict';

const common = require('../common.js');
const net = require('net');
const PORT = common.PORT;

const bench = common.createBenchmark(main, {
Expand All @@ -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');
Expand All @@ -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;
Expand Down Expand Up @@ -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);
});
});
}
60 changes: 24 additions & 36 deletions benchmark/net/net-s2c.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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');
Expand Down Expand Up @@ -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);
});
});
}
Loading

0 comments on commit cf666d8

Please sign in to comment.