1/** 2 * Copyright (c) 2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16const helper = requireNapiPreview('libinterop_test_helper.so', false); 17 18let initBenchmarks = require('./caller.js').initBenchmarks; 19let JSCallee = require('./callee.js'); 20 21function initEtsVM() { 22 const gtestAbcPath = helper.getEnvironmentVar('ARK_ETS_INTEROP_JS_GTEST_ABC_PATH'); 23 const stdlibPath = helper.getEnvironmentVar('ARK_ETS_STDLIB_PATH'); 24 25 let etsVm = requireNapiPreview('ets_interop_js_napi.so', false); 26 const etsOpts = { // NB: Consider setting compiler-enable-jit=false for local run 27 'log-level': 'info', 28 'log-components': 'ets_interop_js', 29 'panda-files': gtestAbcPath, 30 'boot-panda-files': `${stdlibPath}:${gtestAbcPath}`, 31 'gc-trigger-type': 'heap-trigger', 32 'compiler-enable-jit': 'false', 33 'run-gc-in-place': 'true', 34 }; 35 36 if (!etsVm.createRuntime(etsOpts)) { 37 throw Error('Cannot create ETS runtime'); 38 } 39 return etsVm; 40} 41 42function runTest() { 43 print('Running test ' + bench + ' (' + callerSite + '->' + calleeSite + ')'); 44 45 let benchmarks; 46 if (callerSite === 'JS') { 47 benchmarks = initBenchmarks(etsVM); 48 } else if (callerSite === 'STS') { 49 let initializer = etsVM.getFunction('Lcaller/ETSGLOBAL;', 'initBenchmarks'); 50 benchmarks = initializer(callerSite !== calleeSite); 51 } 52 53 let caller = benchmarks[bench]; 54 if (!caller) { 55 throw 'Benchmark not found: \'' + bench + '\' (' + callerSite + '->' + calleeSite + ')'; 56 } 57 const MS2NS = 1000000; 58 59 // Warmup: 60 let start = Date.now(); 61 caller(warmup); 62 let timeNs = (Date.now() - start) * MS2NS; 63 print('warmup ' + bench + ': ' + timeNs / warmup + ' ns/iter, iters: ' + warmup); 64 65 start = Date.now(); 66 caller(iters); 67 timeNs = (Date.now() - start) * MS2NS; 68 print('iters ' + bench + ': ' + timeNs / iters + ' ns/iter, iters: ' + iters); 69 70 return null; 71} 72 73globalThis.require = require; 74globalThis.jsCallee = JSCallee; 75 76let args = helper.getArgv(); 77if (args.length !== 10) { 78 throw Error('Expected <test name> <caller-site(JS/STS)> <callee-site(JS/STS)> <warmup iters> <bench iters>'); 79} 80let bench = args[5]; 81let callerSite = args[6]; 82let calleeSite = args[7]; 83 84if (callerSite !== 'STS' && callerSite !== 'JS') { 85 throw 'Caller site should be \'STS\' or \'JS\', got \'' + callerSite + '\'.'; 86} 87if (calleeSite !== 'STS' && calleeSite !== 'JS') { 88 throw 'Callee site should be \'STS\' or \'JS\', got \'' + calleeSite + '\'.'; 89} 90let etsVM = (callerSite === 'STS' || calleeSite === 'STS') ? initEtsVM() : undefined; 91let warmup = parseInt(args[8], 10); 92let iters = parseInt(args[9], 10); 93 94runTest(); 95