1
import * as fs from "node:fs"
2
import { execSync, exec } from "node:child_process"
4
import { argv } from 'process'
5
import path from "node:path";
6
import { exit } from "node:process";
12
for (let i = 2; i < argv.length; ++i) {
35
console.log(`runPerf isArm64 = ${isArm64}`)
36
console.log(`runPerf isFull = ${isFull}`)
40
if (!isArm64) arch = `arm32`
41
if (!isFull) mode = `subset`
43
const platform = os.platform();
45
const perfDir = "ohos-app/api_perf"
47
if (!fs.existsSync(perfDir)) {
48
console.log(`There is no perf app ...`)
52
const thirdToolsDir = `3rdtools`
53
const signToolsDir = `${thirdToolsDir}/oh_sign`
54
const unsignedHapName = `entry-default-unsigned.hap`
55
const signedHapName = `entry-release-signed.hap`
56
const unsignedHapPathInProject = `${perfDir}/entry/build/default/outputs/default/${unsignedHapName}`
57
const unsignedHapPath = `${signToolsDir}/${unsignedHapName}`
58
const signedHapPath = `${signToolsDir}/${signedHapName}`
59
const packageName = `com.example.api_perf`
60
const tmplog = `.tmplog`
62
let deviceLibDir = `lib64`
63
if (!isArm64) deviceLibDir = `lib`
64
const libaceMockName = `libace_compatible_mock.so`
65
const libaceMockHostPath = path.resolve(`native/${libaceMockName}`)
66
const libaceMockDevicePath = `/system/${deviceLibDir}/module/${libaceMockName}`
68
let hvigorw = `.\\hvigorw`
69
let signRelease = `.\\sign_release`
70
if (!(platform === 'win32')) {
72
signRelease = `./sign_release`
76
if (isMock && !fs.existsSync(libaceMockHostPath)) {
77
console.error(`${libaceMockHostPath} is not exsit`)
84
execSync(`hdc shell mount -o rw,remount /`, {stdio: 'inherit'})
88
const rmAceCmd = `hdc shell rm -f ${libaceMockDevicePath}`
89
console.log(`${rmAceCmd}`)
90
execSync(`${rmAceCmd}`, { cwd: '.', stdio: 'inherit', timeout: 2000 })
93
function resolveDependencies() {
94
let pushDeps4OhosPerfAppCmd = `node ./ohos-app/getDependencies4ohos.mjs ${mode} ${arch}`
95
console.log(`${pushDeps4OhosPerfAppCmd}`)
96
execSync(pushDeps4OhosPerfAppCmd, { cwd: '.', stdio: 'inherit'})
98
const pushAceCmd = `hdc file send ${libaceMockHostPath} ${libaceMockDevicePath}`
99
console.log(`${pushAceCmd}`)
100
execSync(`${pushAceCmd}`, { cwd: '.', stdio: 'inherit'})
103
function buildPerfProject() {
104
const buildCmd = `${hvigorw} --mode module -p module=entry@default -p product=default -p buildMode=release assembleHap --analyze --parallel --incremental --daemon`
105
console.log(buildCmd)
106
execSync(`${buildCmd}`, { cwd: perfDir, stdio: 'inherit'})
110
if (!fs.existsSync(signToolsDir)) {
111
console.log(`get 3rdtools for oh_sign tools`)
112
if (fs.existsSync(thirdToolsDir)) fs.rmdirSync(thirdToolsDir)
113
let downloadCmd = `node ./download-3rdtools.mjs ${arch}`
114
execSync(downloadCmd, { cwd: '.', stdio: 'inherit' })
116
console.log(`copy ${unsignedHapPathInProject} to ${signToolsDir}`)
117
fs.copyFileSync(unsignedHapPathInProject, unsignedHapPath)
118
console.log(`sign ${unsignedHapPath}`)
119
execSync(signRelease, { cwd: signToolsDir, stdio: 'inherit'})
122
function executeCommandWithTimeout(command, timeout, print) {
123
const childProcess = exec(command)
126
execSync(print, { stdio: 'inherit' })
128
console.log(`${error}`)
131
console.log(`kill hdc`)
133
console.log(`kill childProcess.pid(${childProcess.pid}) process.pid(${process.pid})`)
135
process.kill(process.pid)
142
if (!(platform === 'win32')) {
144
killCommand = `ps aux | grep '${processName}' | awk '{print$2}' | xargs kill`
145
execSync(killCommand, { stdio: 'inherit'})
147
processName = 'hdc.exe'
148
killCommand = `taskkill /F /IM ${processName}`
149
execSync(killCommand, { stdio: 'inherit'})
153
function RemoveTmpLog() {
155
if (!(platform === 'win32')) {
156
rmLogCmd = `rm -f ${tmplog}`
157
execSync(rmLogCmd, { stdio: 'inherit'})
159
rmLogCmd = `del ${tmplog}`
160
execSync(rmLogCmd, { stdio: 'inherit'})
164
function runHap(hapPath, packageName) {
165
console.log(`stop ${packageName}`)
166
execSync(`hdc shell aa force-stop ${packageName}`, { stdio:'inherit', timeout: 2000 })
167
console.log(`uninstall ${packageName}`)
168
execSync(`hdc uninstall ${packageName}`, { stdio:'inherit', timeout: 2000 })
169
console.log(`install ${hapPath}`, { stdio:'inherit', timeout: 2000 })
170
execSync(`hdc install ${hapPath}`, { stdio:'inherit', timeout: 3000 })
172
console.log(`clear logs`)
173
execSync(`hdc shell hilog -r`, { stdio: 'inherit'})
178
const command = `hdc shell hilog > ${tmplog}`
179
const printCmd = `grep trace_name ${tmplog}`
180
const timeout = isArm64 ? 4000 : 10000;
181
executeCommandWithTimeout(command, timeout, printCmd)
183
console.log(`startup ${packageName}`)
184
execSync(`hdc shell aa start -a EntryAbility -b ${packageName}`, { stdio:'inherit', timeout: 2000 })
187
if (!checkEnv()) exit()
194
if (fs.existsSync(signedHapPath)) {
195
console.log(`Run perf app`)
196
runHap(signedHapPath, packageName)