Skip to content

Commit

Permalink
feat(benchmark): rewritten script (#4405)
Browse files Browse the repository at this point in the history
  • Loading branch information
himself65 authored Jul 6, 2020
1 parent 7520113 commit 639199e
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 103 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ tmp/
.idea/
yarn.lock
package-lock.json
.nyc_output/
.nyc_output/
.tmp*
12 changes: 6 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@ os: linux

language: node_js

addons:
apt:
packages:
- time

cache:
npm: true

Expand All @@ -15,8 +10,13 @@ node_js:
- "12"
- "14"

before_script:
- chmod +x ./test/profiling.sh

script:
- ./test/benchmark.sh;
- echo "============== Hexo Benchmark =============="
- node ./test/benchmark.js
- ./test/profiling.sh

deploy:
provider: surge
Expand Down
126 changes: 126 additions & 0 deletions test/benchmark.js
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')
]);
}
}
96 changes: 0 additions & 96 deletions test/benchmark.sh

This file was deleted.

38 changes: 38 additions & 0 deletions test/profiling.sh
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

0 comments on commit 639199e

Please sign in to comment.