idlize

Форк
0
/
runPerf.mjs 
197 строк · 5.9 Кб
1
import * as fs from "node:fs"
2
import { execSync, exec } from "node:child_process"
3
import os from "os";
4
import { argv } from 'process'
5
import path from "node:path";
6
import { exit } from "node:process";
7

8
let isFull = true
9
let isArm64 = true
10
let isMock = false
11

12
for (let i = 2; i < argv.length; ++i) {
13
    switch (argv[i]) {
14
        case "subset":
15
            isFull = false
16
            break;
17
        case "full":
18
            isFull = true
19
            break;
20
        case "arm64":
21
            isArm64 = true
22
            break;
23
        case "arm32":
24
        case "arm":
25
            isArm64 = false
26
            break;
27
        case "mock":
28
            isMock = true
29
            break;
30
        default:
31
            break;
32
    }
33
}
34

35
console.log(`runPerf isArm64 = ${isArm64}`)
36
console.log(`runPerf isFull = ${isFull}`)
37

38
let arch = `arm64`
39
let mode = `full`
40
if (!isArm64) arch = `arm32`
41
if (!isFull) mode = `subset`
42

43
const platform = os.platform();
44

45
const perfDir = "ohos-app/api_perf"
46

47
if (!fs.existsSync(perfDir)) {
48
    console.log(`There is no perf app ...`)
49
    process.exit(0)
50
}
51

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`
61

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}`
67

68
let hvigorw = `.\\hvigorw`
69
let signRelease  = `.\\sign_release`
70
if (!(platform === 'win32')) {
71
    hvigorw = `./hvigorw`
72
    signRelease = `./sign_release`
73
}
74

75
function checkEnv() {
76
    if (isMock && !fs.existsSync(libaceMockHostPath)) {
77
        console.error(`${libaceMockHostPath} is not exsit`)
78
        return false
79
    }
80
    return true
81
}
82

83
function mountRW() {
84
    execSync(`hdc shell mount -o rw,remount /`, {stdio: 'inherit'})
85
}
86

87
function cleanEnv() {
88
    const rmAceCmd = `hdc shell rm -f ${libaceMockDevicePath}`
89
    console.log(`${rmAceCmd}`)
90
    execSync(`${rmAceCmd}`, { cwd: '.', stdio: 'inherit', timeout: 2000 })
91
}
92

93
function resolveDependencies() {
94
    let pushDeps4OhosPerfAppCmd = `node ./ohos-app/getDependencies4ohos.mjs ${mode} ${arch}`
95
    console.log(`${pushDeps4OhosPerfAppCmd}`)
96
    execSync(pushDeps4OhosPerfAppCmd, { cwd: '.', stdio: 'inherit'})
97
    if (!isMock) return
98
    const pushAceCmd = `hdc file send ${libaceMockHostPath} ${libaceMockDevicePath}`
99
    console.log(`${pushAceCmd}`)
100
    execSync(`${pushAceCmd}`, { cwd: '.', stdio: 'inherit'})
101
}
102

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'})
107
}
108

109
function signHap() {
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' })
115
    }
116
    console.log(`copy ${unsignedHapPathInProject} to ${signToolsDir}`)
117
    fs.copyFileSync(unsignedHapPathInProject, unsignedHapPath)
118
    console.log(`sign ${unsignedHapPath}`)
119
    execSync(signRelease, { cwd: signToolsDir, stdio: 'inherit'})
120
}
121

122
function executeCommandWithTimeout(command, timeout, print) {
123
    const childProcess = exec(command)
124
    setTimeout(() => {
125
        try {
126
            execSync(print, { stdio: 'inherit' })
127
        } catch (error) {
128
            console.log(`${error}`)
129
        }
130

131
        console.log(`kill hdc`)
132
        killHdc()
133
        console.log(`kill childProcess.pid(${childProcess.pid}) process.pid(${process.pid})`)
134
        childProcess.kill()
135
        process.kill(process.pid)
136
    }, timeout)
137
}
138

139
function killHdc() {
140
    let processName
141
    let killCommand
142
    if (!(platform === 'win32')) {
143
        processName = 'hdc'
144
        killCommand = `ps aux | grep '${processName}' | awk '{print$2}' | xargs kill`
145
        execSync(killCommand, { stdio: 'inherit'})
146
    } else {
147
        processName = 'hdc.exe'
148
        killCommand = `taskkill /F /IM ${processName}`
149
        execSync(killCommand, { stdio: 'inherit'})
150
    }
151
}
152

153
function RemoveTmpLog() {
154
    let rmLogCmd
155
    if (!(platform === 'win32')) {
156
        rmLogCmd = `rm -f ${tmplog}`
157
        execSync(rmLogCmd, { stdio: 'inherit'})
158
    } else {
159
        rmLogCmd = `del ${tmplog}`
160
        execSync(rmLogCmd, { stdio: 'inherit'})
161
    }
162
}
163

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 })
171

172
    console.log(`clear logs`)
173
    execSync(`hdc shell hilog -r`, { stdio: 'inherit'})
174

175
    killHdc()
176
    RemoveTmpLog()
177

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)
182

183
    console.log(`startup ${packageName}`)
184
    execSync(`hdc shell aa start -a EntryAbility -b ${packageName}`, { stdio:'inherit', timeout: 2000 })
185
}
186

187
if (!checkEnv()) exit()
188
mountRW()
189
cleanEnv()
190
resolveDependencies()
191
buildPerfProject()
192
signHap()
193

194
if (fs.existsSync(signedHapPath)) {
195
    console.log(`Run perf app`)
196
    runHap(signedHapPath, packageName)
197
}
198

199

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.