2013-02-08 04:13:26 +01: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-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');
|
|
|
|
const util = require('util');
|
2013-02-08 04:13:26 +01:00
|
|
|
|
|
|
|
// if there are dur=N and len=N args, then
|
|
|
|
// run the function with those settings.
|
|
|
|
// if not, then queue up a bunch of child processes.
|
2017-09-14 03:48:53 +02:00
|
|
|
const bench = common.createBenchmark(main, {
|
2013-02-08 04:13:26 +01:00
|
|
|
len: [102400, 1024 * 1024 * 16],
|
|
|
|
type: ['utf', 'asc', 'buf'],
|
2013-02-20 00:03:41 +01:00
|
|
|
dur: [5]
|
2013-02-08 04:13:26 +01:00
|
|
|
});
|
|
|
|
|
2017-11-20 17:18:40 +01:00
|
|
|
const { TCP, constants: TCPConstants } = process.binding('tcp_wrap');
|
2017-09-14 03:48:53 +02:00
|
|
|
const TCPConnectWrap = process.binding('tcp_wrap').TCPConnectWrap;
|
|
|
|
const WriteWrap = process.binding('stream_wrap').WriteWrap;
|
|
|
|
const PORT = common.PORT;
|
2013-02-08 04:13:26 +01:00
|
|
|
|
2017-12-30 03:56:44 +01:00
|
|
|
function main({ dur, len, type }) {
|
2017-11-20 17:18:40 +01:00
|
|
|
const serverHandle = new TCP(TCPConstants.SERVER);
|
2013-07-19 23:59:41 +02:00
|
|
|
var err = serverHandle.bind('127.0.0.1', PORT);
|
|
|
|
if (err)
|
|
|
|
fail(err, 'bind');
|
2013-02-08 04:13:26 +01:00
|
|
|
|
2013-07-19 23:59:41 +02:00
|
|
|
err = serverHandle.listen(511);
|
|
|
|
if (err)
|
|
|
|
fail(err, 'listen');
|
2013-02-08 04:13:26 +01:00
|
|
|
|
2013-07-19 23:59:41 +02:00
|
|
|
serverHandle.onconnection = function(err, clientHandle) {
|
|
|
|
if (err)
|
|
|
|
fail(err, 'connect');
|
2013-02-08 04:13:26 +01:00
|
|
|
|
|
|
|
var chunk;
|
|
|
|
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':
|
2017-04-02 23:32:50 +02:00
|
|
|
chunk = 'ü'.repeat(len / 2);
|
2013-02-08 04:13:26 +01:00
|
|
|
break;
|
|
|
|
case 'asc':
|
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
|
|
|
}
|
|
|
|
|
|
|
|
clientHandle.readStart();
|
|
|
|
|
|
|
|
while (clientHandle.writeQueueSize === 0)
|
|
|
|
write();
|
|
|
|
|
|
|
|
function write() {
|
2017-09-14 03:48:53 +02:00
|
|
|
const writeReq = new WriteWrap();
|
2015-01-18 17:16:05 +01:00
|
|
|
writeReq.async = false;
|
|
|
|
writeReq.oncomplete = afterWrite;
|
2013-07-19 23:59:41 +02:00
|
|
|
var err;
|
2013-02-08 04:13:26 +01:00
|
|
|
switch (type) {
|
|
|
|
case 'buf':
|
2013-07-19 23:59:41 +02:00
|
|
|
err = clientHandle.writeBuffer(writeReq, chunk);
|
2013-02-08 04:13:26 +01:00
|
|
|
break;
|
|
|
|
case 'utf':
|
2013-07-19 23:59:41 +02:00
|
|
|
err = clientHandle.writeUtf8String(writeReq, chunk);
|
2013-02-08 04:13:26 +01:00
|
|
|
break;
|
|
|
|
case 'asc':
|
2013-07-19 23:59:41 +02:00
|
|
|
err = clientHandle.writeAsciiString(writeReq, chunk);
|
2013-02-08 04:13:26 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-01-28 23:48:10 +01:00
|
|
|
if (err) {
|
2013-07-19 23:59:41 +02:00
|
|
|
fail(err, 'write');
|
2014-01-28 23:48:10 +01:00
|
|
|
} else if (!writeReq.async) {
|
|
|
|
process.nextTick(function() {
|
2018-02-08 04:59:10 +01:00
|
|
|
afterWrite(0, clientHandle);
|
2014-01-28 23:48:10 +01:00
|
|
|
});
|
|
|
|
}
|
2013-02-08 04:13:26 +01:00
|
|
|
}
|
|
|
|
|
2018-02-08 04:59:10 +01:00
|
|
|
function afterWrite(status, handle) {
|
|
|
|
if (status)
|
|
|
|
fail(status, 'write');
|
2013-02-08 04:13:26 +01:00
|
|
|
|
|
|
|
while (clientHandle.writeQueueSize === 0)
|
|
|
|
write();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-12-30 03:56:44 +01:00
|
|
|
client(dur);
|
|
|
|
}
|
|
|
|
|
|
|
|
function fail(err, syscall) {
|
|
|
|
throw util._errnoException(err, syscall);
|
2013-02-08 04:13:26 +01:00
|
|
|
}
|
|
|
|
|
2017-12-30 03:56:44 +01:00
|
|
|
function client(dur) {
|
2017-11-20 17:18:40 +01:00
|
|
|
const clientHandle = new TCP(TCPConstants.SOCKET);
|
2017-09-14 03:48:53 +02:00
|
|
|
const connectReq = new TCPConnectWrap();
|
|
|
|
const err = clientHandle.connect(connectReq, '127.0.0.1', PORT);
|
2013-02-08 04:13:26 +01:00
|
|
|
|
2013-07-19 23:59:41 +02:00
|
|
|
if (err)
|
|
|
|
fail(err, 'connect');
|
2013-02-08 04:13:26 +01:00
|
|
|
|
|
|
|
connectReq.oncomplete = function() {
|
|
|
|
var bytes = 0;
|
2013-07-19 23:59:41 +02:00
|
|
|
clientHandle.onread = function(nread, buffer) {
|
2013-02-08 04:13:26 +01:00
|
|
|
// we're not expecting to ever get an EOF from the client.
|
|
|
|
// just lots of data forever.
|
2013-07-19 23:59:41 +02:00
|
|
|
if (nread < 0)
|
|
|
|
fail(nread, 'read');
|
2013-02-08 04:13:26 +01:00
|
|
|
|
|
|
|
// don't slice the buffer. the point of this is to isolate, not
|
|
|
|
// simulate real traffic.
|
2013-07-09 01:53:41 +02:00
|
|
|
bytes += buffer.length;
|
2013-02-08 04:13:26 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
clientHandle.readStart();
|
|
|
|
|
|
|
|
// the meat of the benchmark is right here:
|
|
|
|
bench.start();
|
|
|
|
|
|
|
|
setTimeout(function() {
|
|
|
|
// report in Gb/sec
|
|
|
|
bench.end((bytes * 8) / (1024 * 1024 * 1024));
|
2016-05-31 21:49:16 +02:00
|
|
|
process.exit(0);
|
2013-02-08 04:13:26 +01:00
|
|
|
}, dur * 1000);
|
|
|
|
};
|
|
|
|
}
|