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: [4, 8, 16, 32, 64, 128, 512, 1024],
101cb0ef41Sopenharmony_ci  type: ['buf'],
111cb0ef41Sopenharmony_ci  dur: [5],
121cb0ef41Sopenharmony_ci});
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_cilet chunk;
151cb0ef41Sopenharmony_cilet encoding;
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_cifunction main({ dur, len, type }) {
181cb0ef41Sopenharmony_ci  switch (type) {
191cb0ef41Sopenharmony_ci    case 'buf':
201cb0ef41Sopenharmony_ci      chunk = Buffer.alloc(len, 'x');
211cb0ef41Sopenharmony_ci      break;
221cb0ef41Sopenharmony_ci    case 'utf':
231cb0ef41Sopenharmony_ci      encoding = 'utf8';
241cb0ef41Sopenharmony_ci      chunk = 'ü'.repeat(len / 2);
251cb0ef41Sopenharmony_ci      break;
261cb0ef41Sopenharmony_ci    case 'asc':
271cb0ef41Sopenharmony_ci      encoding = 'ascii';
281cb0ef41Sopenharmony_ci      chunk = 'x'.repeat(len);
291cb0ef41Sopenharmony_ci      break;
301cb0ef41Sopenharmony_ci    default:
311cb0ef41Sopenharmony_ci      throw new Error(`invalid type: ${type}`);
321cb0ef41Sopenharmony_ci  }
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ci  const writer = new Writer();
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ci  // The actual benchmark.
371cb0ef41Sopenharmony_ci  const server = net.createServer((socket) => {
381cb0ef41Sopenharmony_ci    socket.pipe(writer);
391cb0ef41Sopenharmony_ci  });
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_ci  server.listen(PORT, () => {
421cb0ef41Sopenharmony_ci    const socket = net.connect(PORT);
431cb0ef41Sopenharmony_ci    socket.on('connect', () => {
441cb0ef41Sopenharmony_ci      bench.start();
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ci      socket.on('drain', send);
471cb0ef41Sopenharmony_ci      send();
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_ci      setTimeout(() => {
501cb0ef41Sopenharmony_ci        const bytes = writer.received;
511cb0ef41Sopenharmony_ci        const gbits = (bytes * 8) / (1024 * 1024 * 1024);
521cb0ef41Sopenharmony_ci        bench.end(gbits);
531cb0ef41Sopenharmony_ci        process.exit(0);
541cb0ef41Sopenharmony_ci      }, dur * 1000);
551cb0ef41Sopenharmony_ci
561cb0ef41Sopenharmony_ci      function send() {
571cb0ef41Sopenharmony_ci        socket.cork();
581cb0ef41Sopenharmony_ci        while (socket.write(chunk, encoding));
591cb0ef41Sopenharmony_ci        socket.uncork();
601cb0ef41Sopenharmony_ci      }
611cb0ef41Sopenharmony_ci    });
621cb0ef41Sopenharmony_ci  });
631cb0ef41Sopenharmony_ci}
641cb0ef41Sopenharmony_ci
651cb0ef41Sopenharmony_cifunction Writer() {
661cb0ef41Sopenharmony_ci  this.received = 0;
671cb0ef41Sopenharmony_ci  this.writable = true;
681cb0ef41Sopenharmony_ci}
691cb0ef41Sopenharmony_ci
701cb0ef41Sopenharmony_ciWriter.prototype.write = function(chunk, encoding, cb) {
711cb0ef41Sopenharmony_ci  this.received += chunk.length;
721cb0ef41Sopenharmony_ci
731cb0ef41Sopenharmony_ci  if (typeof encoding === 'function')
741cb0ef41Sopenharmony_ci    encoding();
751cb0ef41Sopenharmony_ci  else if (typeof cb === 'function')
761cb0ef41Sopenharmony_ci    cb();
771cb0ef41Sopenharmony_ci
781cb0ef41Sopenharmony_ci  return true;
791cb0ef41Sopenharmony_ci};
801cb0ef41Sopenharmony_ci
811cb0ef41Sopenharmony_ci// Doesn't matter, never emits anything.
821cb0ef41Sopenharmony_ciWriter.prototype.on = function() {};
831cb0ef41Sopenharmony_ciWriter.prototype.once = function() {};
841cb0ef41Sopenharmony_ciWriter.prototype.emit = function() {};
851cb0ef41Sopenharmony_ciWriter.prototype.prependListener = function() {};
86