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