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: [2, 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(socket); 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 socket.pipe(writer); 511cb0ef41Sopenharmony_ci 521cb0ef41Sopenharmony_ci setTimeout(() => { 531cb0ef41Sopenharmony_ci // Multiply by 2 since we're sending it first one way 541cb0ef41Sopenharmony_ci // then back again. 551cb0ef41Sopenharmony_ci const bytes = writer.received * 2; 561cb0ef41Sopenharmony_ci const gbits = (bytes * 8) / (1024 * 1024 * 1024); 571cb0ef41Sopenharmony_ci bench.end(gbits); 581cb0ef41Sopenharmony_ci process.exit(0); 591cb0ef41Sopenharmony_ci }, dur * 1000); 601cb0ef41Sopenharmony_ci }); 611cb0ef41Sopenharmony_ci }); 621cb0ef41Sopenharmony_ci} 631cb0ef41Sopenharmony_ci 641cb0ef41Sopenharmony_cifunction Writer() { 651cb0ef41Sopenharmony_ci this.received = 0; 661cb0ef41Sopenharmony_ci this.writable = true; 671cb0ef41Sopenharmony_ci} 681cb0ef41Sopenharmony_ci 691cb0ef41Sopenharmony_ciWriter.prototype.write = function(chunk, encoding, cb) { 701cb0ef41Sopenharmony_ci this.received += chunk.length; 711cb0ef41Sopenharmony_ci 721cb0ef41Sopenharmony_ci if (typeof encoding === 'function') 731cb0ef41Sopenharmony_ci encoding(); 741cb0ef41Sopenharmony_ci else if (typeof cb === 'function') 751cb0ef41Sopenharmony_ci cb(); 761cb0ef41Sopenharmony_ci 771cb0ef41Sopenharmony_ci return true; 781cb0ef41Sopenharmony_ci}; 791cb0ef41Sopenharmony_ci 801cb0ef41Sopenharmony_ci// Doesn't matter, never emits anything. 811cb0ef41Sopenharmony_ciWriter.prototype.on = function() {}; 821cb0ef41Sopenharmony_ciWriter.prototype.once = function() {}; 831cb0ef41Sopenharmony_ciWriter.prototype.emit = function() {}; 841cb0ef41Sopenharmony_ciWriter.prototype.prependListener = function() {}; 851cb0ef41Sopenharmony_ci 861cb0ef41Sopenharmony_ci 871cb0ef41Sopenharmony_cifunction flow() { 881cb0ef41Sopenharmony_ci const dest = this.dest; 891cb0ef41Sopenharmony_ci const res = dest.write(chunk, encoding); 901cb0ef41Sopenharmony_ci if (!res) 911cb0ef41Sopenharmony_ci dest.once('drain', this.flow); 921cb0ef41Sopenharmony_ci else 931cb0ef41Sopenharmony_ci process.nextTick(this.flow); 941cb0ef41Sopenharmony_ci} 951cb0ef41Sopenharmony_ci 961cb0ef41Sopenharmony_cifunction Reader() { 971cb0ef41Sopenharmony_ci this.flow = flow.bind(this); 981cb0ef41Sopenharmony_ci this.readable = true; 991cb0ef41Sopenharmony_ci} 1001cb0ef41Sopenharmony_ci 1011cb0ef41Sopenharmony_ciReader.prototype.pipe = function(dest) { 1021cb0ef41Sopenharmony_ci this.dest = dest; 1031cb0ef41Sopenharmony_ci this.flow(); 1041cb0ef41Sopenharmony_ci return dest; 1051cb0ef41Sopenharmony_ci}; 106