Skip to content

Commit af32565

Browse files
committed
test: add benchmark.js
Signed-off-by: JayFate <[email protected]>
1 parent 700cd64 commit af32565

File tree

5 files changed

+117
-1
lines changed

5 files changed

+117
-1
lines changed

benchmark/index.js

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
const path = require('path')
2+
const cp = require('child_process')
3+
const fs = require('fs-extra')
4+
5+
const toolkitRoot = path.resolve(__dirname, '..')
6+
7+
const run = require(path.resolve(toolkitRoot, 'packages/hap-dev-utils/src/index.js')).run
8+
const { compile, stopWatch } = require(path.resolve(
9+
toolkitRoot,
10+
'packages/hap-toolkit/lib/commands/compile.js'
11+
))
12+
13+
let hapBin = path.resolve(toolkitRoot, 'packages/hap-toolkit/bin/index.js')
14+
15+
const cwd = path.resolve(toolkitRoot, 'examples/sample')
16+
17+
cp.execSync('rm -rf ./node_modules/.cache', { cwd })
18+
let sum = 30
19+
20+
async function runBuild(disableCache) {
21+
const timeArr = []
22+
for (let i = 0; i < sum + 1; i++) {
23+
console.log(`\n\n${disableCache ? 'disableCache, ' : 'enableCache, '}i: ${i}\n\n`)
24+
const start = Date.now()
25+
await run('node', [hapBin, 'build', disableCache ? '--disableCache' : ''], [], {
26+
cwd
27+
})
28+
const time = Date.now() - start
29+
timeArr.push(time)
30+
}
31+
32+
timeArr.shift()
33+
const totlaTime = parseFloat(timeArr.reduce((sum, curr) => sum + curr, 0).toFixed(2))
34+
const averageTime = parseFloat((totlaTime / sum).toFixed(2))
35+
console.log(`totlaTime:`, totlaTime)
36+
console.log(`averageTime:`, averageTime)
37+
return [averageTime, totlaTime]
38+
}
39+
40+
const homeUxFile = path.resolve(cwd, './src/home/index.ux')
41+
const content = fs.readFileSync(homeUxFile, 'utf-8')
42+
43+
async function runWatch(disableCache) {
44+
let start
45+
let timeArr = []
46+
// const params = { action: 'progress', percentage }
47+
function callback(params) {
48+
if (params.action === 'progress') {
49+
if (params.percentage === 0) {
50+
start = Date.now()
51+
console.log(`new start`, start)
52+
}
53+
if (params.percentage === 1 && start) {
54+
// watch 模式才会进入
55+
const duration = Date.now() - start
56+
start = undefined
57+
timeArr.push(duration)
58+
console.log(`new end, duration`, duration)
59+
}
60+
}
61+
}
62+
63+
function waitFor(num) {
64+
return new Promise((resolve, reject) => {
65+
const id = setInterval(() => {
66+
if (timeArr.length === num) {
67+
resolve()
68+
clearInterval(id)
69+
}
70+
}, 50)
71+
})
72+
}
73+
74+
await compile('native', 'dev', true, { cwd, callback, disableCache }, undefined, undefined)
75+
for (let i = 0; i < sum + 1; i++) {
76+
await fs.writeFile(
77+
homeUxFile,
78+
content.replace('// benchmark-placeholder', `console.log(${i})`),
79+
'utf-8'
80+
)
81+
await waitFor(i)
82+
}
83+
84+
await stopWatch()
85+
timeArr.shift()
86+
const totlaTime = parseFloat(timeArr.reduce((sum, curr) => sum + curr, 0).toFixed(2))
87+
const averageTime = parseFloat((totlaTime / sum).toFixed(2))
88+
console.log(`totlaTime:`, totlaTime)
89+
console.log(`averageTime:`, averageTime)
90+
return [averageTime, totlaTime]
91+
}
92+
93+
;(async () => {
94+
const [noCacheAverageTime, noCacheTotlaTime] = await runBuild(true)
95+
const [cachedAverageTime, cachedTotlaTime] = await runBuild()
96+
const multiple = parseFloat((noCacheAverageTime / cachedAverageTime).toFixed(2))
97+
98+
const [noCacheAverageTime1, noCacheTotlaTime1] = await runWatch(true)
99+
await fs.writeFile(homeUxFile, content, 'utf-8')
100+
const [cachedAverageTime1, cachedTotlaTime1] = await runWatch()
101+
await fs.writeFile(homeUxFile, content, 'utf-8')
102+
const multiple1 = parseFloat((noCacheAverageTime1 / cachedAverageTime1).toFixed(2))
103+
104+
console.log(`\n\n`)
105+
console.log(`----------run build----------`)
106+
console.table({ noCacheAverageTime, cachedAverageTime, multiple })
107+
108+
console.log(`----------run watch----------`)
109+
console.table({ noCacheAverageTime1, cachedAverageTime1, multiple1 })
110+
})()

examples/sample/src/home/index.ux

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@
126126
import router from '@system.router'
127127
import { tabsData } from './data'
128128

129+
// benchmark-placeholder
130+
129131
export default {
130132
/**
131133
* @desc

packages/hap-dev-utils/src/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@ function run(cmd, args = [], dialogs = [], opts = {}) {
106106
proc.kill('SIGTERM')
107107
}
108108
process.on('SIGINT', end)
109-
process.on('SIGKILL', end)
109+
// SIGKILL 信号,node 18 报错 Error: uv_signal_start EINVAL
110+
// process.on('SIGKILL', end)
110111
process.on('SIGTERM', end)
111112
process.on('beforeExit', end)
112113
process.on('exit', end)

packages/hap-shared-utils/src/compilation-config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ const compileOptionsMeta = {
3737
}
3838

3939
const compileOptionsObject = {
40+
// 默认不禁用 cache
41+
disableCache: false,
4042
// 工具默认输出card和app的所有路由,可定制为单纯输出card或app
4143
target: 'all',
4244
// 打包来源,ide|cmd

packages/hap-toolkit/bin/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ program
7474
'--enable-diagnosis [value]',
7575
'proxy console object, send log to server, write into project/logs'
7676
)
77+
.option('--disableCache', 'disable cache')
7778
.option(
7879
'--build-name-format <build-name-format>',
7980
'custom output rpk file name',

0 commit comments

Comments
 (0)