11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ciconst common = require('../common.js'); 41cb0ef41Sopenharmony_ci 51cb0ef41Sopenharmony_ciconst bench = common.createBenchmark(main, { 61cb0ef41Sopenharmony_ci aligned: ['true', 'false'], 71cb0ef41Sopenharmony_ci method: ['swap16', 'swap32', 'swap64'/* , 'htons', 'htonl', 'htonll' */], 81cb0ef41Sopenharmony_ci len: [64, 256, 768, 1024, 2056, 8192], 91cb0ef41Sopenharmony_ci n: [1e6], 101cb0ef41Sopenharmony_ci}, { 111cb0ef41Sopenharmony_ci test: { len: 16 }, 121cb0ef41Sopenharmony_ci}); 131cb0ef41Sopenharmony_ci 141cb0ef41Sopenharmony_ci// The htons and htonl methods below are used to benchmark the 151cb0ef41Sopenharmony_ci// performance difference between doing the byteswap in pure 161cb0ef41Sopenharmony_ci// javascript regardless of Buffer size as opposed to dropping 171cb0ef41Sopenharmony_ci// down to the native layer for larger Buffer sizes. Commented 181cb0ef41Sopenharmony_ci// out by default because they are slow for big buffers. If 191cb0ef41Sopenharmony_ci// re-evaluating the crossover point, uncomment those methods 201cb0ef41Sopenharmony_ci// and comment out their implementations in lib/buffer.js so 211cb0ef41Sopenharmony_ci// C++ version will always be used. 221cb0ef41Sopenharmony_ci 231cb0ef41Sopenharmony_cifunction swap(b, n, m) { 241cb0ef41Sopenharmony_ci const i = b[n]; 251cb0ef41Sopenharmony_ci b[n] = b[m]; 261cb0ef41Sopenharmony_ci b[m] = i; 271cb0ef41Sopenharmony_ci} 281cb0ef41Sopenharmony_ci 291cb0ef41Sopenharmony_ciBuffer.prototype.htons = function htons() { 301cb0ef41Sopenharmony_ci if (this.length % 2 !== 0) 311cb0ef41Sopenharmony_ci throw new RangeError(); 321cb0ef41Sopenharmony_ci for (let i = 0; i < this.length; i += 2) { 331cb0ef41Sopenharmony_ci swap(this, i, i + 1); 341cb0ef41Sopenharmony_ci } 351cb0ef41Sopenharmony_ci return this; 361cb0ef41Sopenharmony_ci}; 371cb0ef41Sopenharmony_ci 381cb0ef41Sopenharmony_ciBuffer.prototype.htonl = function htonl() { 391cb0ef41Sopenharmony_ci if (this.length % 4 !== 0) 401cb0ef41Sopenharmony_ci throw new RangeError(); 411cb0ef41Sopenharmony_ci for (let i = 0; i < this.length; i += 4) { 421cb0ef41Sopenharmony_ci swap(this, i, i + 3); 431cb0ef41Sopenharmony_ci swap(this, i + 1, i + 2); 441cb0ef41Sopenharmony_ci } 451cb0ef41Sopenharmony_ci return this; 461cb0ef41Sopenharmony_ci}; 471cb0ef41Sopenharmony_ci 481cb0ef41Sopenharmony_ciBuffer.prototype.htonll = function htonll() { 491cb0ef41Sopenharmony_ci if (this.length % 8 !== 0) 501cb0ef41Sopenharmony_ci throw new RangeError(); 511cb0ef41Sopenharmony_ci for (let i = 0; i < this.length; i += 8) { 521cb0ef41Sopenharmony_ci swap(this, i, i + 7); 531cb0ef41Sopenharmony_ci swap(this, i + 1, i + 6); 541cb0ef41Sopenharmony_ci swap(this, i + 2, i + 5); 551cb0ef41Sopenharmony_ci swap(this, i + 3, i + 4); 561cb0ef41Sopenharmony_ci } 571cb0ef41Sopenharmony_ci return this; 581cb0ef41Sopenharmony_ci}; 591cb0ef41Sopenharmony_ci 601cb0ef41Sopenharmony_cifunction createBuffer(len, aligned) { 611cb0ef41Sopenharmony_ci len += aligned ? 0 : 1; 621cb0ef41Sopenharmony_ci const buf = Buffer.allocUnsafe(len); 631cb0ef41Sopenharmony_ci for (let i = 1; i <= len; i++) 641cb0ef41Sopenharmony_ci buf[i - 1] = i; 651cb0ef41Sopenharmony_ci return aligned ? buf : buf.slice(1); 661cb0ef41Sopenharmony_ci} 671cb0ef41Sopenharmony_ci 681cb0ef41Sopenharmony_cifunction genMethod(method) { 691cb0ef41Sopenharmony_ci const fnString = ` 701cb0ef41Sopenharmony_ci return function ${method}(n, buf) { 711cb0ef41Sopenharmony_ci for (let i = 0; i <= n; i++) 721cb0ef41Sopenharmony_ci buf.${method}(); 731cb0ef41Sopenharmony_ci }`; 741cb0ef41Sopenharmony_ci return (new Function(fnString))(); 751cb0ef41Sopenharmony_ci} 761cb0ef41Sopenharmony_ci 771cb0ef41Sopenharmony_cifunction main({ method, len, n, aligned = 'true' }) { 781cb0ef41Sopenharmony_ci const buf = createBuffer(len, aligned === 'true'); 791cb0ef41Sopenharmony_ci const bufferSwap = genMethod(method); 801cb0ef41Sopenharmony_ci 811cb0ef41Sopenharmony_ci bufferSwap(n, buf); 821cb0ef41Sopenharmony_ci bench.start(); 831cb0ef41Sopenharmony_ci bufferSwap(n, buf); 841cb0ef41Sopenharmony_ci bench.end(n); 851cb0ef41Sopenharmony_ci} 86