xref: /third_party/node/benchmark/net/net-c2s.js (revision 1cb0ef41)
11cb0ef41Sopenharmony_ci// Test the speed of .pipe() with sockets
21cb0ef41Sopenharmony_ci'use strict';
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ciconst common = require('../common.js');
51cb0ef41Sopenharmony_ciconst net = require('net');
61cb0ef41Sopenharmony_ciconst PORT = common.PORT;
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ciconst bench = common.createBenchmark(main, {
91cb0ef41Sopenharmony_ci  len: [64, 102400, 1024 * 1024 * 16],
101cb0ef41Sopenharmony_ci  type: ['utf', 'asc', 'buf'],
111cb0ef41Sopenharmony_ci  dur: [5],
121cb0ef41Sopenharmony_ci}, {
131cb0ef41Sopenharmony_ci  test: { len: 1024 },
141cb0ef41Sopenharmony_ci});
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_cilet chunk;
171cb0ef41Sopenharmony_cilet encoding;
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_cifunction main({ dur, len, type }) {
201cb0ef41Sopenharmony_ci  switch (type) {
211cb0ef41Sopenharmony_ci    case 'buf':
221cb0ef41Sopenharmony_ci      chunk = Buffer.alloc(len, 'x');
231cb0ef41Sopenharmony_ci      break;
241cb0ef41Sopenharmony_ci    case 'utf':
251cb0ef41Sopenharmony_ci      encoding = 'utf8';
261cb0ef41Sopenharmony_ci      chunk = 'ü'.repeat(len / 2);
271cb0ef41Sopenharmony_ci      break;
281cb0ef41Sopenharmony_ci    case 'asc':
291cb0ef41Sopenharmony_ci      encoding = 'ascii';
301cb0ef41Sopenharmony_ci      chunk = 'x'.repeat(len);
311cb0ef41Sopenharmony_ci      break;
321cb0ef41Sopenharmony_ci    default:
331cb0ef41Sopenharmony_ci      throw new Error(`invalid type: ${type}`);
341cb0ef41Sopenharmony_ci  }
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ci  const reader = new Reader();
371cb0ef41Sopenharmony_ci  const writer = new Writer();
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ci  // The actual benchmark.
401cb0ef41Sopenharmony_ci  const server = net.createServer((socket) => {
411cb0ef41Sopenharmony_ci    socket.pipe(writer);
421cb0ef41Sopenharmony_ci  });
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_ci  server.listen(PORT, () => {
451cb0ef41Sopenharmony_ci    const socket = net.connect(PORT);
461cb0ef41Sopenharmony_ci    socket.on('connect', () => {
471cb0ef41Sopenharmony_ci      bench.start();
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_ci      reader.pipe(socket);
501cb0ef41Sopenharmony_ci
511cb0ef41Sopenharmony_ci      setTimeout(() => {
521cb0ef41Sopenharmony_ci        const bytes = writer.received;
531cb0ef41Sopenharmony_ci        const gbits = (bytes * 8) / (1024 * 1024 * 1024);
541cb0ef41Sopenharmony_ci        bench.end(gbits);
551cb0ef41Sopenharmony_ci        process.exit(0);
561cb0ef41Sopenharmony_ci      }, dur * 1000);
571cb0ef41Sopenharmony_ci    });
581cb0ef41Sopenharmony_ci  });
591cb0ef41Sopenharmony_ci}
601cb0ef41Sopenharmony_ci
611cb0ef41Sopenharmony_cifunction Writer() {
621cb0ef41Sopenharmony_ci  this.received = 0;
631cb0ef41Sopenharmony_ci  this.writable = true;
641cb0ef41Sopenharmony_ci}
651cb0ef41Sopenharmony_ci
661cb0ef41Sopenharmony_ciWriter.prototype.write = function(chunk, encoding, cb) {
671cb0ef41Sopenharmony_ci  this.received += chunk.length;
681cb0ef41Sopenharmony_ci
691cb0ef41Sopenharmony_ci  if (typeof encoding === 'function')
701cb0ef41Sopenharmony_ci    encoding();
711cb0ef41Sopenharmony_ci  else if (typeof cb === 'function')
721cb0ef41Sopenharmony_ci    cb();
731cb0ef41Sopenharmony_ci
741cb0ef41Sopenharmony_ci  return true;
751cb0ef41Sopenharmony_ci};
761cb0ef41Sopenharmony_ci
771cb0ef41Sopenharmony_ci// Doesn't matter, never emits anything.
781cb0ef41Sopenharmony_ciWriter.prototype.on = function() {};
791cb0ef41Sopenharmony_ciWriter.prototype.once = function() {};
801cb0ef41Sopenharmony_ciWriter.prototype.emit = function() {};
811cb0ef41Sopenharmony_ciWriter.prototype.prependListener = function() {};
821cb0ef41Sopenharmony_ci
831cb0ef41Sopenharmony_ci
841cb0ef41Sopenharmony_cifunction flow() {
851cb0ef41Sopenharmony_ci  const dest = this.dest;
861cb0ef41Sopenharmony_ci  const res = dest.write(chunk, encoding);
871cb0ef41Sopenharmony_ci  if (!res)
881cb0ef41Sopenharmony_ci    dest.once('drain', this.flow);
891cb0ef41Sopenharmony_ci  else
901cb0ef41Sopenharmony_ci    process.nextTick(this.flow);
911cb0ef41Sopenharmony_ci}
921cb0ef41Sopenharmony_ci
931cb0ef41Sopenharmony_cifunction Reader() {
941cb0ef41Sopenharmony_ci  this.flow = flow.bind(this);
951cb0ef41Sopenharmony_ci  this.readable = true;
961cb0ef41Sopenharmony_ci}
971cb0ef41Sopenharmony_ci
981cb0ef41Sopenharmony_ciReader.prototype.pipe = function(dest) {
991cb0ef41Sopenharmony_ci  this.dest = dest;
1001cb0ef41Sopenharmony_ci  this.flow();
1011cb0ef41Sopenharmony_ci  return dest;
1021cb0ef41Sopenharmony_ci};
103