-
-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(benchmark): rewritten script (#4405)
- Loading branch information
Showing
5 changed files
with
172 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,4 +5,5 @@ tmp/ | |
.idea/ | ||
yarn.lock | ||
package-lock.json | ||
.nyc_output/ | ||
.nyc_output/ | ||
.tmp* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
'use strict'; | ||
|
||
const { performance, PerformanceObserver } = require('perf_hooks'); | ||
const { spawnSync, spawn } = require('child_process'); | ||
const os = require('os'); | ||
const fs = require('hexo-fs'); | ||
const path = require('path'); | ||
const chalk = require('chalk'); | ||
let npmScript = 'npm'; | ||
let npxScript = 'npx'; | ||
const testDir = path.resolve('.tmp-hexo-theme-unit-test'); | ||
const hooks = [ | ||
{ regex: /Hexo version/, tag: 'hexo-begin'}, | ||
{ regex: /Start processing/, tag: 'processing'}, | ||
{ regex: /Files loaded/, tag: 'file-loaded'}, | ||
{ regex: /generated in/, tag: 'generated'}, | ||
{ regex: /Database saved/, tag: 'database-saved'} | ||
]; | ||
|
||
if (os.platform() === 'win32') { | ||
npmScript = 'npm.cmd'; | ||
npxScript = 'npx.cmd'; | ||
} | ||
|
||
(async () => { | ||
init(); | ||
// make sure no cache files | ||
cleanup(); | ||
await run_benchmark('Cold processing'); | ||
await run_benchmark('Hot processing'); | ||
cleanup(); | ||
await run_benchmark('Another Cold processing'); | ||
cleanup(); | ||
})(); | ||
|
||
async function run_benchmark(name) { | ||
return new Promise(resolve => { | ||
const result = {}; | ||
const obs = new PerformanceObserver(list => { | ||
const { name, duration: _duration } = list.getEntries()[0]; | ||
const duration = _duration / 1000; | ||
result[name] = { | ||
'Cost time (s)': `${duration.toFixed(2)}s` | ||
}; | ||
if (duration > 20) { | ||
console.log(chalk.red('!! Performance regression detected !!')); | ||
} | ||
}); | ||
obs.observe({ entryTypes: ['measure'] }); | ||
|
||
const npx = spawn(npxScript, ['--no-install', 'hexo', 'g', '--debug'], { cwd: testDir }); | ||
hooks.forEach(({ regex, tag}) => { | ||
npx.stdout.on('data', function listener(data) { | ||
const string = data.toString('utf-8'); | ||
if (regex.test(string)) { | ||
performance.mark(tag); | ||
npx.stdout.removeListener('data', listener); | ||
} | ||
}); | ||
}); | ||
|
||
npx.on('close', () => { | ||
performance.measure('Load Plugin/Scripts/Database', 'hexo-begin', 'processing'); | ||
performance.measure('Process Source', 'processing', 'file-loaded'); | ||
performance.measure('Render Files', 'file-loaded', 'generated'); | ||
performance.measure('Save Database', 'generated', 'database-saved'); | ||
performance.measure('Total time', 'hexo-begin', 'database-saved'); | ||
console.log(name); | ||
console.table(result); | ||
obs.disconnect(); | ||
resolve(); | ||
}); | ||
}); | ||
} | ||
|
||
function cleanup() { | ||
spawnSync(npxScript, ['--no-install', 'hexo', 'clean'], { cwd: testDir }); | ||
} | ||
|
||
function init() { | ||
spawnSync('git', | ||
[ | ||
'clone', | ||
'https://github.com/hexojs/hexo-theme-unit-test.git', | ||
testDir, | ||
'--depth=1']); | ||
spawnSync('git', | ||
[ | ||
'clone', | ||
'https://github.com/hexojs/hexo-theme-landscape', | ||
path.resolve(testDir, 'themes', 'landscape'), | ||
'--depth=1' | ||
]); | ||
spawnSync('git', | ||
[ | ||
'clone', | ||
'https://github.com/SukkaLab/hexo-many-posts.git', | ||
path.resolve(testDir, 'source', '_posts', 'hexo-many-posts'), | ||
'--depth=1' | ||
]); | ||
spawnSync(npmScript, ['install', '--silent'], { cwd: testDir }); | ||
fs.rmdirSync(path.resolve(testDir, 'node_modules', 'hexo')); | ||
|
||
if (os.platform() === 'win32') { | ||
spawnSync('cmd', [ | ||
'/s', '/c', | ||
'mklink', | ||
'/D', | ||
path.resolve(testDir, 'node_modules', 'hexo'), | ||
path.resolve(__dirname, '..') | ||
]); | ||
spawnSync('cmd', [ | ||
'/s', '/c', | ||
'mklink', | ||
'/D', | ||
path.resolve(testDir, 'node_modules', 'hexo-cli'), | ||
path.resolve(__dirname, '..', 'node_modules', 'hexo-cli') | ||
]); | ||
} else { | ||
spawnSync('ln', [ | ||
'-sf', | ||
path.resolve(__dirname, '..'), | ||
path.resolve(testDir, 'node_modules', 'hexo') | ||
]); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#!/bin/sh | ||
|
||
echo "- Set up dummy Hexo site" | ||
cd $TRAVIS_BUILD_DIR | ||
cd .. | ||
git clone https://github.com/hexojs/hexo-theme-unit-test.git --depth=1 --quiet | ||
cd hexo-theme-unit-test | ||
|
||
echo "- Install hexo-theme-landscape" | ||
git clone https://github.com/hexojs/hexo-theme-landscape --depth=1 --quiet themes/landscape | ||
|
||
echo "- npm install" | ||
npm install --silent | ||
|
||
echo "- Import 500 posts" | ||
git clone https://github.com/SukkaLab/hexo-many-posts.git source/_posts/hexo-many-posts --depth=1 --quiet | ||
rm -rf source/_posts/hexo-many-posts/.git/ | ||
|
||
echo "- Replace node_modules/hexo" | ||
rm -rf node_modules/hexo | ||
ln -sf $TRAVIS_BUILD_DIR node_modules/hexo | ||
|
||
echo "- Start test run" | ||
echo "" | ||
|
||
echo "--------------------------------------------" | ||
npx --no-install hexo clean > build.log | ||
|
||
echo "" | ||
echo "- Generating flamegraph..." | ||
|
||
npm install 0x --silent | ||
node ./node_modules/.bin/0x --output-dir "${TRAVIS_BUILD_DIR}/0x" -- node ./node_modules/.bin/hexo g > build.log 2>&1 ; | ||
|
||
echo "Flamegraph will be deployed to: https://${TRAVIS_COMMIT}-${TRAVIS_NODE_VERSION}-hexo.surge.sh/flamegraph.html" | ||
|
||
rm -rf build.log | ||
cd $TRAVIS_BUILD_DIR |