2013-02-08 04:13:26 +01:00
|
|
|
// test the speed of .pipe() with sockets
|
2016-02-20 02:03:16 +01:00
|
|
|
'use strict';
|
2013-02-08 04:13:26 +01:00
|
|
|
|
2017-09-14 03:48:53 +02:00
|
|
|
const common = require('../common.js');
|
2017-12-30 03:56:44 +01:00
|
|
|
const net = require('net');
|
2017-09-14 03:48:53 +02:00
|
|
|
const PORT = common.PORT;
|
2013-02-08 04:13:26 +01:00
|
|
|
|
2017-09-14 03:48:53 +02:00
|
|
|
const bench = common.createBenchmark(main, {
|
2018-10-23 08:23:02 +02:00
|
|
|
len: [64, 102400, 1024 * 1024 * 16],
|
2013-02-08 04:13:26 +01:00
|
|
|
type: ['utf', 'asc', 'buf'],
|
2013-02-20 00:03:41 +01:00
|
|
|
dur: [5],
|
2013-02-08 04:13:26 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
var chunk;
|
|
|
|
var encoding;
|
|
|
|
|
2017-12-30 03:56:44 +01:00
|
|
|
function main({ dur, len, type }) {
|
2013-02-08 04:13:26 +01:00
|
|
|
switch (type) {
|
|
|
|
case 'buf':
|
2016-01-26 00:00:06 +01:00
|
|
|
chunk = Buffer.alloc(len, 'x');
|
2013-02-08 04:13:26 +01:00
|
|
|
break;
|
|
|
|
case 'utf':
|
|
|
|
encoding = 'utf8';
|
2017-04-02 23:32:50 +02:00
|
|
|
chunk = 'ü'.repeat(len / 2);
|
2013-02-08 04:13:26 +01:00
|
|
|
break;
|
|
|
|
case 'asc':
|
|
|
|
encoding = 'ascii';
|
2017-04-02 23:32:50 +02:00
|
|
|
chunk = 'x'.repeat(len);
|
2013-02-08 04:13:26 +01:00
|
|
|
break;
|
|
|
|
default:
|
2017-04-17 03:01:12 +02:00
|
|
|
throw new Error(`invalid type: ${type}`);
|
2013-02-08 04:13:26 +01:00
|
|
|
}
|
|
|
|
|
2017-12-30 03:56:44 +01:00
|
|
|
const reader = new Reader();
|
|
|
|
const writer = new Writer();
|
2013-02-08 04:13:26 +01:00
|
|
|
|
2017-12-30 03:56:44 +01:00
|
|
|
// 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);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2013-02-08 04:13:26 +01:00
|
|
|
|
|
|
|
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() {};
|
2016-04-27 02:29:05 +02:00
|
|
|
Writer.prototype.prependListener = function() {};
|
2013-02-08 04:13:26 +01:00
|
|
|
|
|
|
|
|
2016-12-25 05:29:22 +01:00
|
|
|
function flow() {
|
2017-09-14 03:48:53 +02:00
|
|
|
const dest = this.dest;
|
|
|
|
const res = dest.write(chunk, encoding);
|
2016-12-25 05:29:22 +01:00
|
|
|
if (!res)
|
|
|
|
dest.once('drain', this.flow);
|
|
|
|
else
|
|
|
|
process.nextTick(this.flow);
|
|
|
|
}
|
|
|
|
|
2013-02-08 04:13:26 +01:00
|
|
|
function Reader() {
|
2016-12-25 05:29:22 +01:00
|
|
|
this.flow = flow.bind(this);
|
2013-02-08 04:13:26 +01:00
|
|
|
this.readable = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Reader.prototype.pipe = function(dest) {
|
|
|
|
this.dest = dest;
|
|
|
|
this.flow();
|
|
|
|
return dest;
|
|
|
|
};
|