|
| 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 | +})() |
0 commit comments