11cb0ef41Sopenharmony_ci// Show the difference between calling a V8 binding C++ function 21cb0ef41Sopenharmony_ci// relative to a comparable N-API C++ function, 31cb0ef41Sopenharmony_ci// in various types/numbers of arguments. 41cb0ef41Sopenharmony_ci// Reports n of calls per second. 51cb0ef41Sopenharmony_ci'use strict'; 61cb0ef41Sopenharmony_ci 71cb0ef41Sopenharmony_ciconst common = require('../../common.js'); 81cb0ef41Sopenharmony_ci 91cb0ef41Sopenharmony_cilet v8; 101cb0ef41Sopenharmony_cilet napi; 111cb0ef41Sopenharmony_ci 121cb0ef41Sopenharmony_citry { 131cb0ef41Sopenharmony_ci v8 = require(`./build/${common.buildType}/binding`); 141cb0ef41Sopenharmony_ci} catch { 151cb0ef41Sopenharmony_ci console.error(`${__filename}: V8 Binding failed to load`); 161cb0ef41Sopenharmony_ci process.exit(0); 171cb0ef41Sopenharmony_ci} 181cb0ef41Sopenharmony_ci 191cb0ef41Sopenharmony_citry { 201cb0ef41Sopenharmony_ci napi = require(`./build/${common.buildType}/napi_binding`); 211cb0ef41Sopenharmony_ci} catch { 221cb0ef41Sopenharmony_ci console.error(`${__filename}: NAPI-Binding failed to load`); 231cb0ef41Sopenharmony_ci process.exit(0); 241cb0ef41Sopenharmony_ci} 251cb0ef41Sopenharmony_ci 261cb0ef41Sopenharmony_ciconst argsTypes = ['String', 'Number', 'Object', 'Array', 'Typedarray', 271cb0ef41Sopenharmony_ci '10Numbers', '100Numbers', '1000Numbers']; 281cb0ef41Sopenharmony_ci 291cb0ef41Sopenharmony_ciconst generateArgs = (argType) => { 301cb0ef41Sopenharmony_ci let args = []; 311cb0ef41Sopenharmony_ci 321cb0ef41Sopenharmony_ci if (argType === 'String') { 331cb0ef41Sopenharmony_ci args.push('The quick brown fox jumps over the lazy dog'); 341cb0ef41Sopenharmony_ci } else if (argType === 'LongString') { 351cb0ef41Sopenharmony_ci args.push(Buffer.alloc(32768, '42').toString()); 361cb0ef41Sopenharmony_ci } else if (argType === 'Number') { 371cb0ef41Sopenharmony_ci args.push(Math.floor(314158964 * Math.random())); 381cb0ef41Sopenharmony_ci } else if (argType === 'Object') { 391cb0ef41Sopenharmony_ci args.push({ 401cb0ef41Sopenharmony_ci map: 'add', 411cb0ef41Sopenharmony_ci operand: 10, 421cb0ef41Sopenharmony_ci data: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 431cb0ef41Sopenharmony_ci reduce: 'add', 441cb0ef41Sopenharmony_ci }); 451cb0ef41Sopenharmony_ci } else if (argType === 'Array') { 461cb0ef41Sopenharmony_ci const arr = []; 471cb0ef41Sopenharmony_ci for (let i = 0; i < 50; ++i) { 481cb0ef41Sopenharmony_ci arr.push(Math.random() * 10e9); 491cb0ef41Sopenharmony_ci } 501cb0ef41Sopenharmony_ci args.push(arr); 511cb0ef41Sopenharmony_ci } else if (argType === 'Typedarray') { 521cb0ef41Sopenharmony_ci const arr = new Uint32Array(1000); 531cb0ef41Sopenharmony_ci for (let i = 0; i < 1000; ++i) { 541cb0ef41Sopenharmony_ci arr[i] = Math.random() * 4294967296; 551cb0ef41Sopenharmony_ci } 561cb0ef41Sopenharmony_ci args.push(arr); 571cb0ef41Sopenharmony_ci } else if (argType === '10Numbers') { 581cb0ef41Sopenharmony_ci args.push(10); 591cb0ef41Sopenharmony_ci for (let i = 0; i < 9; ++i) { 601cb0ef41Sopenharmony_ci args = [...args, ...generateArgs('Number')]; 611cb0ef41Sopenharmony_ci } 621cb0ef41Sopenharmony_ci } else if (argType === '100Numbers') { 631cb0ef41Sopenharmony_ci args.push(100); 641cb0ef41Sopenharmony_ci for (let i = 0; i < 99; ++i) { 651cb0ef41Sopenharmony_ci args = [...args, ...generateArgs('Number')]; 661cb0ef41Sopenharmony_ci } 671cb0ef41Sopenharmony_ci } else if (argType === '1000Numbers') { 681cb0ef41Sopenharmony_ci args.push(1000); 691cb0ef41Sopenharmony_ci for (let i = 0; i < 999; ++i) { 701cb0ef41Sopenharmony_ci args = [...args, ...generateArgs('Number')]; 711cb0ef41Sopenharmony_ci } 721cb0ef41Sopenharmony_ci } 731cb0ef41Sopenharmony_ci 741cb0ef41Sopenharmony_ci return args; 751cb0ef41Sopenharmony_ci}; 761cb0ef41Sopenharmony_ci 771cb0ef41Sopenharmony_ciconst bench = common.createBenchmark(main, { 781cb0ef41Sopenharmony_ci type: argsTypes, 791cb0ef41Sopenharmony_ci engine: ['v8', 'napi'], 801cb0ef41Sopenharmony_ci n: [1, 1e1, 1e2, 1e3, 1e4, 1e5], 811cb0ef41Sopenharmony_ci}); 821cb0ef41Sopenharmony_ci 831cb0ef41Sopenharmony_cifunction main({ n, engine, type }) { 841cb0ef41Sopenharmony_ci const bindings = engine === 'v8' ? v8 : napi; 851cb0ef41Sopenharmony_ci const methodName = 'callWith' + type; 861cb0ef41Sopenharmony_ci const fn = bindings[methodName]; 871cb0ef41Sopenharmony_ci 881cb0ef41Sopenharmony_ci if (fn) { 891cb0ef41Sopenharmony_ci const args = generateArgs(type); 901cb0ef41Sopenharmony_ci 911cb0ef41Sopenharmony_ci bench.start(); 921cb0ef41Sopenharmony_ci for (let i = 0; i < n; i++) { 931cb0ef41Sopenharmony_ci fn.apply(null, args); 941cb0ef41Sopenharmony_ci } 951cb0ef41Sopenharmony_ci bench.end(n); 961cb0ef41Sopenharmony_ci } 971cb0ef41Sopenharmony_ci} 98