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 16function requireModule(name) { 17 let mod = globalThis.requireNapi(name); 18 if (mod) { 19 return mod; 20 } 21 mod = globalThis.ohosplugin; 22 name.split('.').foreach(element => { 23 if (mod) { 24 mod = mod[element]; 25 } 26 }); 27 if (mod) { 28 return mod; 29 } 30 mod = globalThis.systemplugin; 31 name.split('.').foreach(element => { 32 if (mod) { 33 mod = mod[element]; 34 } 35 }); 36 return mod; 37} 38 39function delayMs(ms) { 40 const startTime = Date.now(); 41 while (Date.now() - startTime < ms) {} 42} 43 44async function scheduleConnectionAsync(perftest) { 45 let procInfo = requireModule('process'); 46 if (procInfo == null) { 47 console.error('C03120 PerfTestKit_exporter: Failed to require process napi module'); 48 return; 49 } 50 let registry = requireModule('application.abilityDelegatorRegistry'); 51 if (registry == null) { 52 console.error('C03120 PerfTestKit_exporter: Failed to require AbilityDelegator napi module'); 53 return; 54 } 55 let delegator = registry.getAbilityDelegator(); 56 if (delegator == null) { 57 console.warn('C03120 PerfTestKit_exporter: Cannot get AbilityDelegator, perftest_daemon need to be pre-started'); 58 return; 59 } 60 let appContext = delegator.getAppContext(); 61 let connToken = `${appContext.applicationInfo.name}@${procInfo.pid}@${procInfo.uid}@${appContext.area}`; 62 console.info(`C03120 PerfTestKit_exporter: ScheduleProbeAndEstablishConnection, token=${connToken}`); 63 perftest.scheduleEstablishConnection(connToken); 64 console.info(`C03120 PerfTestKit_exporter command: perftest start-daemon ${connToken}`); 65 delegator.executeShellCommand(`perftest start-daemon ${connToken}`, 3).then((value) => { 66 console.info(`C03120 PerfTestKit_exporter: Start perftest_daemon finished: ${JSON.stringify(value)}`); 67 }).catch((error) => { 68 console.error(`C03120 PerfTestKit_exporter: Start perftest_daemon failed: ${JSON.stringify(error)}`); 69 }); 70} 71 72function loadPerfTest() { 73 let perftest = globalThis.requireInternal('test.PerfTest'); 74 if (perftest.perftestSetupCalled === true) { 75 return; 76 } 77 perftest.perftestSetupCalled = true; 78 // schedule startup server_daemon and establish connection 79 scheduleConnectionAsync(perftest).catch((error) => { 80 console.error(`C03120 PerfTestKit_exporter: ScheduleConnectionAsync failed: ${JSON.stringify(error)}`); 81 }); 82 let delayTime = 500; 83 delayMs(delayTime); 84 return perftest; 85} 86 87export default loadPerfTest(); 88