2019-03-07 01:03:53 +01:00
|
|
|
// Test the speed of .pipe() with sockets
|
2016-02-20 02:03:16 +01:00
|
|
|
'use strict';
|
2015-12-16 09:39:11 +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;
|
2015-12-16 09:39:11 +01:00
|
|
|
|
2017-09-14 03:48:53 +02:00
|
|
|
const bench = common.createBenchmark(main, {
|
2015-12-16 09:39:11 +01:00
|
|
|
len: [4, 8, 16, 32, 64, 128, 512, 1024],
|
|
|
|
type: ['buf'],
|
|
|
|
dur: [5],
|
|
|
|
});
|
|
|
|
|
2020-01-31 16:50:00 +01:00
|
|
|
let chunk;
|
|
|
|
let encoding;
|
2015-12-16 09:39:11 +01:00
|
|
|
|
2017-12-30 03:56:44 +01:00
|
|
|
function main({ dur, len, type }) {
|
2015-12-16 09:39:11 +01:00
|
|
|
switch (type) {
|
|
|
|
case 'buf':
|
2016-01-26 00:00:06 +01:00
|
|
|
chunk = Buffer.alloc(len, 'x');
|
2015-12-16 09:39:11 +01:00
|
|
|
break;
|
|
|
|
case 'utf':
|
|
|
|
encoding = 'utf8';
|
2017-04-02 23:32:50 +02:00
|
|
|
chunk = 'ü'.repeat(len / 2);
|
2015-12-16 09:39:11 +01:00
|
|
|
break;
|
|
|
|
case 'asc':
|
|
|
|
encoding = 'ascii';
|
2017-04-02 23:32:50 +02:00
|
|
|
chunk = 'x'.repeat(len);
|
2015-12-16 09:39:11 +01:00
|
|
|
break;
|
|
|
|
default:
|
2017-04-17 03:01:12 +02:00
|
|
|
throw new Error(`invalid type: ${type}`);
|
2015-12-16 09:39:11 +01:00
|
|
|
}
|
|
|
|
|
2017-09-14 03:48:53 +02:00
|
|
|
const writer = new Writer();
|
2015-12-16 09:39:11 +01:00
|
|
|
|
2019-03-22 03:44:26 +01:00
|
|
|
// The actual benchmark.
|
2019-02-05 07:06:08 +01:00
|
|
|
const server = net.createServer((socket) => {
|
2015-12-16 09:39:11 +01:00
|
|
|
socket.pipe(writer);
|
|
|
|
});
|
|
|
|
|
2019-02-05 07:06:08 +01:00
|
|
|
server.listen(PORT, () => {
|
2017-09-14 03:48:53 +02:00
|
|
|
const socket = net.connect(PORT);
|
2019-02-05 07:06:08 +01:00
|
|
|
socket.on('connect', () => {
|
2015-12-16 09:39:11 +01:00
|
|
|
bench.start();
|
|
|
|
|
2016-02-25 07:30:10 +01:00
|
|
|
socket.on('drain', send);
|
|
|
|
send();
|
2015-12-16 09:39:11 +01:00
|
|
|
|
2019-02-05 07:06:08 +01:00
|
|
|
setTimeout(() => {
|
2017-09-14 03:48:53 +02:00
|
|
|
const bytes = writer.received;
|
|
|
|
const gbits = (bytes * 8) / (1024 * 1024 * 1024);
|
2015-12-16 09:39:11 +01:00
|
|
|
bench.end(gbits);
|
2016-05-31 21:49:16 +02:00
|
|
|
process.exit(0);
|
2015-12-16 09:39:11 +01:00
|
|
|
}, dur * 1000);
|
|
|
|
|
|
|
|
function send() {
|
|
|
|
socket.cork();
|
2016-02-25 07:30:10 +01:00
|
|
|
while (socket.write(chunk, encoding)) {}
|
2015-12-16 09:39:11 +01:00
|
|
|
socket.uncork();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2017-12-30 03:56:44 +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;
|
|
|
|
};
|
|
|
|
|
2019-03-07 01:03:53 +01:00
|
|
|
// Doesn't matter, never emits anything.
|
2017-12-30 03:56:44 +01:00
|
|
|
Writer.prototype.on = function() {};
|
|
|
|
Writer.prototype.once = function() {};
|
|
|
|
Writer.prototype.emit = function() {};
|
|
|
|
Writer.prototype.prependListener = function() {};
|