2013-02-07 19:13:26 -08:00
|
|
|
// In this benchmark, we connect a client to the server, and write
|
|
|
|
// as many bytes as we can in the specified time (default = 10s)
|
2016-02-19 17:03:16 -08:00
|
|
|
'use strict';
|
2013-02-07 19:13:26 -08:00
|
|
|
|
2017-09-13 22:48:53 -03:00
|
|
|
const common = require('../common.js');
|
|
|
|
const util = require('util');
|
2013-02-07 19:13:26 -08:00
|
|
|
|
2019-01-21 01:22:27 +01:00
|
|
|
// If there are --dur=N and --len=N args, then
|
2013-02-07 19:13:26 -08:00
|
|
|
// run the function with those settings.
|
|
|
|
// if not, then queue up a bunch of child processes.
|
2017-09-13 22:48:53 -03:00
|
|
|
const bench = common.createBenchmark(main, {
|
2024-03-13 20:02:14 +01:00
|
|
|
len: [102400, 1024 * 64 * 16],
|
2013-02-07 19:13:26 -08:00
|
|
|
type: ['utf', 'asc', 'buf'],
|
2023-02-03 11:43:15 +01:00
|
|
|
dur: [5],
|
2018-08-15 17:14:22 -07:00
|
|
|
}, {
|
2020-02-12 19:33:33 +01:00
|
|
|
test: { len: 1024 },
|
2023-02-03 11:43:15 +01:00
|
|
|
flags: [ '--expose-internals', '--no-warnings' ],
|
2013-02-07 19:13:26 -08:00
|
|
|
});
|
|
|
|
|
2018-08-15 17:14:22 -07:00
|
|
|
function main({ dur, len, type }) {
|
2018-10-12 12:45:26 -04:00
|
|
|
const {
|
|
|
|
TCP,
|
|
|
|
TCPConnectWrap,
|
2023-02-03 11:43:15 +01:00
|
|
|
constants: TCPConstants,
|
2018-10-12 12:45:26 -04:00
|
|
|
} = common.binding('tcp_wrap');
|
|
|
|
const { WriteWrap } = common.binding('stream_wrap');
|
2018-08-15 17:14:22 -07:00
|
|
|
const PORT = common.PORT;
|
2017-12-30 03:56:44 +01:00
|
|
|
|
2018-08-15 17:14:22 -07:00
|
|
|
function fail(err, syscall) {
|
|
|
|
throw util._errnoException(err, syscall);
|
|
|
|
}
|
2013-02-07 19:13:26 -08:00
|
|
|
|
2017-12-30 03:56:44 +01:00
|
|
|
// Server
|
2017-11-20 17:18:40 +01:00
|
|
|
const serverHandle = new TCP(TCPConstants.SERVER);
|
2020-01-31 16:50:00 +01:00
|
|
|
let err = serverHandle.bind('127.0.0.1', PORT);
|
2013-07-19 23:59:41 +02:00
|
|
|
if (err)
|
|
|
|
fail(err, 'bind');
|
2013-02-07 19:13:26 -08:00
|
|
|
|
2013-07-19 23:59:41 +02:00
|
|
|
err = serverHandle.listen(511);
|
|
|
|
if (err)
|
|
|
|
fail(err, 'listen');
|
2013-02-07 19:13:26 -08:00
|
|
|
|
2013-07-19 23:59:41 +02:00
|
|
|
serverHandle.onconnection = function(err, clientHandle) {
|
|
|
|
if (err)
|
|
|
|
fail(err, 'connect');
|
2013-02-07 19:13:26 -08:00
|
|
|
|
2018-10-21 08:34:00 +02:00
|
|
|
clientHandle.onread = function(buffer) {
|
2018-12-10 13:27:32 +01:00
|
|
|
// We're not expecting to ever get an EOF from the client.
|
|
|
|
// Just lots of data forever.
|
2018-10-21 08:34:00 +02:00
|
|
|
if (!buffer)
|
|
|
|
fail('read');
|
2013-02-07 19:13:26 -08:00
|
|
|
|
2017-09-13 22:48:53 -03:00
|
|
|
const writeReq = new WriteWrap();
|
2015-01-19 01:16:05 +09:00
|
|
|
writeReq.async = false;
|
2018-10-21 08:34:00 +02:00
|
|
|
err = clientHandle.writeBuffer(writeReq, Buffer.from(buffer));
|
2013-02-07 19:13:26 -08:00
|
|
|
|
2013-07-19 23:59:41 +02:00
|
|
|
if (err)
|
|
|
|
fail(err, 'write');
|
2013-02-07 19:13:26 -08:00
|
|
|
|
2018-02-08 04:59:10 +01:00
|
|
|
writeReq.oncomplete = function(status, handle, err) {
|
2015-01-19 01:16:05 +09:00
|
|
|
if (err)
|
2013-07-19 23:59:41 +02:00
|
|
|
fail(err, 'write');
|
2013-02-07 19:13:26 -08:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
clientHandle.readStart();
|
|
|
|
};
|
|
|
|
|
2017-12-30 03:56:44 +01:00
|
|
|
// Client
|
2020-01-31 16:50:00 +01:00
|
|
|
let chunk;
|
2013-02-07 19:13:26 -08:00
|
|
|
switch (type) {
|
|
|
|
case 'buf':
|
2016-01-25 15:00:06 -08:00
|
|
|
chunk = Buffer.alloc(len, 'x');
|
2013-02-07 19:13:26 -08:00
|
|
|
break;
|
|
|
|
case 'utf':
|
2017-04-03 00:32:50 +03:00
|
|
|
chunk = 'ü'.repeat(len / 2);
|
2013-02-07 19:13:26 -08:00
|
|
|
break;
|
|
|
|
case 'asc':
|
2017-04-03 00:32:50 +03:00
|
|
|
chunk = 'x'.repeat(len);
|
2013-02-07 19:13:26 -08:00
|
|
|
break;
|
|
|
|
default:
|
2017-04-17 04:01:12 +03:00
|
|
|
throw new Error(`invalid type: ${type}`);
|
2013-02-07 19:13:26 -08:00
|
|
|
}
|
|
|
|
|
2017-11-20 17:18:40 +01:00
|
|
|
const clientHandle = new TCP(TCPConstants.SOCKET);
|
2017-09-13 22:48:53 -03:00
|
|
|
const connectReq = new TCPConnectWrap();
|
2020-01-31 16:50:00 +01:00
|
|
|
let bytes = 0;
|
2013-02-07 19:13:26 -08:00
|
|
|
|
2017-12-30 03:56:44 +01:00
|
|
|
err = clientHandle.connect(connectReq, '127.0.0.1', PORT);
|
2013-07-19 23:59:41 +02:00
|
|
|
if (err)
|
|
|
|
fail(err, 'connect');
|
2013-02-07 19:13:26 -08:00
|
|
|
|
2018-10-21 08:34:00 +02:00
|
|
|
clientHandle.onread = function(buffer) {
|
|
|
|
if (!buffer)
|
|
|
|
fail('read');
|
2013-02-07 19:13:26 -08:00
|
|
|
|
2018-10-21 08:34:00 +02:00
|
|
|
bytes += buffer.byteLength;
|
2013-02-07 19:13:26 -08:00
|
|
|
};
|
|
|
|
|
2013-07-19 23:59:41 +02:00
|
|
|
connectReq.oncomplete = function(err) {
|
|
|
|
if (err)
|
|
|
|
fail(err, 'connect');
|
|
|
|
|
2013-02-07 19:13:26 -08:00
|
|
|
bench.start();
|
|
|
|
|
2017-04-06 08:42:46 -04:00
|
|
|
clientHandle.readStart();
|
|
|
|
|
2019-02-04 22:06:08 -08:00
|
|
|
setTimeout(() => {
|
2018-12-10 13:27:32 +01:00
|
|
|
// Multiply by 2 since we're sending it first one way
|
2019-10-02 21:44:14 -04:00
|
|
|
// then back again.
|
2013-02-07 19:13:26 -08:00
|
|
|
bench.end(2 * (bytes * 8) / (1024 * 1024 * 1024));
|
2016-05-31 21:49:16 +02:00
|
|
|
process.exit(0);
|
2013-02-07 19:13:26 -08:00
|
|
|
}, dur * 1000);
|
|
|
|
|
|
|
|
while (clientHandle.writeQueueSize === 0)
|
|
|
|
write();
|
|
|
|
};
|
|
|
|
|
|
|
|
function write() {
|
2017-09-13 22:48:53 -03:00
|
|
|
const writeReq = new WriteWrap();
|
2015-01-19 01:16:05 +09:00
|
|
|
writeReq.oncomplete = afterWrite;
|
2020-01-31 16:50:00 +01:00
|
|
|
let err;
|
2013-02-07 19:13:26 -08:00
|
|
|
switch (type) {
|
|
|
|
case 'buf':
|
2013-07-19 23:59:41 +02:00
|
|
|
err = clientHandle.writeBuffer(writeReq, chunk);
|
2013-02-07 19:13:26 -08:00
|
|
|
break;
|
|
|
|
case 'utf':
|
2013-07-19 23:59:41 +02:00
|
|
|
err = clientHandle.writeUtf8String(writeReq, chunk);
|
2013-02-07 19:13:26 -08:00
|
|
|
break;
|
|
|
|
case 'asc':
|
2013-07-19 23:59:41 +02:00
|
|
|
err = clientHandle.writeAsciiString(writeReq, chunk);
|
2013-02-07 19:13:26 -08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-07-19 23:59:41 +02:00
|
|
|
if (err)
|
|
|
|
fail(err, 'write');
|
2013-02-07 19:13:26 -08:00
|
|
|
}
|
|
|
|
|
2018-02-08 04:59:10 +01:00
|
|
|
function afterWrite(err, handle) {
|
2013-07-19 23:59:41 +02:00
|
|
|
if (err)
|
|
|
|
fail(err, 'write');
|
2013-02-07 19:13:26 -08:00
|
|
|
|
|
|
|
while (clientHandle.writeQueueSize === 0)
|
|
|
|
write();
|
|
|
|
}
|
|
|
|
}
|