summaryrefslogtreecommitdiff
path: root/deps/node/benchmark/net/tcp-raw-pipe.js
diff options
context:
space:
mode:
authorFlorian Dold <florian.dold@gmail.com>2019-04-03 15:43:32 +0200
committerFlorian Dold <florian.dold@gmail.com>2019-04-03 15:45:57 +0200
commit71e285b94c7edaa43aa8115965cf5a36b8e0f80a (patch)
tree7d4aa9d0d5aff686b106cd5da72ba77960c4af43 /deps/node/benchmark/net/tcp-raw-pipe.js
parent7dadf9356b4f3f4137ce982ea5bb960283116e9a (diff)
downloadakono-71e285b94c7edaa43aa8115965cf5a36b8e0f80a.tar.gz
akono-71e285b94c7edaa43aa8115965cf5a36b8e0f80a.tar.bz2
akono-71e285b94c7edaa43aa8115965cf5a36b8e0f80a.zip
Node.js v11.13.0
Diffstat (limited to 'deps/node/benchmark/net/tcp-raw-pipe.js')
-rw-r--r--deps/node/benchmark/net/tcp-raw-pipe.js145
1 files changed, 145 insertions, 0 deletions
diff --git a/deps/node/benchmark/net/tcp-raw-pipe.js b/deps/node/benchmark/net/tcp-raw-pipe.js
new file mode 100644
index 00000000..89db42dc
--- /dev/null
+++ b/deps/node/benchmark/net/tcp-raw-pipe.js
@@ -0,0 +1,145 @@
+// In this benchmark, we connect a client to the server, and write
+// as many bytes as we can in the specified time (default = 10s)
+'use strict';
+
+const common = require('../common.js');
+const util = require('util');
+
+// 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.
+const bench = common.createBenchmark(main, {
+ len: [102400, 1024 * 1024 * 16],
+ type: ['utf', 'asc', 'buf'],
+ dur: [5]
+}, {
+ flags: [ '--expose-internals', '--no-warnings' ]
+});
+
+function main({ dur, len, type }) {
+ const {
+ TCP,
+ TCPConnectWrap,
+ constants: TCPConstants
+ } = common.binding('tcp_wrap');
+ const { WriteWrap } = common.binding('stream_wrap');
+ const PORT = common.PORT;
+
+ function fail(err, syscall) {
+ throw util._errnoException(err, syscall);
+ }
+
+ // Server
+ const serverHandle = new TCP(TCPConstants.SERVER);
+ var err = serverHandle.bind('127.0.0.1', PORT);
+ if (err)
+ fail(err, 'bind');
+
+ err = serverHandle.listen(511);
+ if (err)
+ fail(err, 'listen');
+
+ serverHandle.onconnection = function(err, clientHandle) {
+ if (err)
+ fail(err, 'connect');
+
+ clientHandle.onread = function(buffer) {
+ // We're not expecting to ever get an EOF from the client.
+ // Just lots of data forever.
+ if (!buffer)
+ fail('read');
+
+ const writeReq = new WriteWrap();
+ writeReq.async = false;
+ err = clientHandle.writeBuffer(writeReq, Buffer.from(buffer));
+
+ if (err)
+ fail(err, 'write');
+
+ writeReq.oncomplete = function(status, handle, err) {
+ if (err)
+ fail(err, 'write');
+ };
+ };
+
+ clientHandle.readStart();
+ };
+
+ // Client
+ var chunk;
+ switch (type) {
+ case 'buf':
+ chunk = Buffer.alloc(len, 'x');
+ break;
+ case 'utf':
+ chunk = 'ΓΌ'.repeat(len / 2);
+ break;
+ case 'asc':
+ chunk = 'x'.repeat(len);
+ break;
+ default:
+ throw new Error(`invalid type: ${type}`);
+ }
+
+ const clientHandle = new TCP(TCPConstants.SOCKET);
+ const connectReq = new TCPConnectWrap();
+ var bytes = 0;
+
+ err = clientHandle.connect(connectReq, '127.0.0.1', PORT);
+ if (err)
+ fail(err, 'connect');
+
+ clientHandle.onread = function(buffer) {
+ if (!buffer)
+ fail('read');
+
+ bytes += buffer.byteLength;
+ };
+
+ connectReq.oncomplete = function(err) {
+ if (err)
+ fail(err, 'connect');
+
+ bench.start();
+
+ clientHandle.readStart();
+
+ setTimeout(() => {
+ // Multiply by 2 since we're sending it first one way
+ // then then back again.
+ bench.end(2 * (bytes * 8) / (1024 * 1024 * 1024));
+ process.exit(0);
+ }, dur * 1000);
+
+ while (clientHandle.writeQueueSize === 0)
+ write();
+ };
+
+ function write() {
+ const writeReq = new WriteWrap();
+ writeReq.oncomplete = afterWrite;
+ var err;
+ switch (type) {
+ case 'buf':
+ err = clientHandle.writeBuffer(writeReq, chunk);
+ break;
+ case 'utf':
+ err = clientHandle.writeUtf8String(writeReq, chunk);
+ break;
+ case 'asc':
+ err = clientHandle.writeAsciiString(writeReq, chunk);
+ break;
+ }
+
+ if (err)
+ fail(err, 'write');
+ }
+
+ function afterWrite(err, handle) {
+ if (err)
+ fail(err, 'write');
+
+ while (clientHandle.writeQueueSize === 0)
+ write();
+ }
+}