Skip to content

Commit

Permalink
👌 IMPROVE: Print warnning log when boot action takes more than 5000ms (…
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Oct 31, 2022
1 parent d820b73 commit 3a941d6
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 10 deletions.
5 changes: 1 addition & 4 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
{
"extends": "eslint-config-egg",
"parserOptions": {
"ecmaVersion": 2017
}
"extends": "eslint-config-egg"
}
4 changes: 4 additions & 0 deletions config/config.default.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ module.exports = appInfo => {
// ignore any key contains "secret" keyword
/secret/i,
]),
timing: {
// if boot action >= slowBootActionMinDuration, egg core will print it to warnning log
slowBootActionMinDuration: 5000,
},
},

/**
Expand Down
2 changes: 0 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
'use strict';

/**
* @namespace Egg
*/
Expand Down
13 changes: 9 additions & 4 deletions lib/egg.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
'use strict';

const { performance } = require('perf_hooks');
const path = require('path');
const fs = require('fs');
Expand Down Expand Up @@ -412,12 +410,19 @@ class EggApplication extends EggCore {

dumpTiming() {
try {
const json = this.timing.toJSON();
const items = this.timing.toJSON();
const rundir = this.config.rundir;
const dumpFile = path.join(rundir, `${this.type}_timing_${process.pid}.json`);
fs.writeFileSync(dumpFile, CircularJSON.stringify(json, null, 2));
fs.writeFileSync(dumpFile, CircularJSON.stringify(items, null, 2));
// only disable, not clear bootstrap timing data.
this.timing.disable();
// show duration >= ${slowBootActionMinDuration}ms action to warnning log
for (const item of items) {
if (item.duration >= this.config.dump.timing.slowBootActionMinDuration) {
this.coreLogger.warn('[egg:core][slow-boot-action] #%d %dms, name: %s',
item.index, item.duration, item.name);
}
}
} catch (err) {
this.coreLogger.warn(`dumpTiming error: ${err.message}`);
}
Expand Down
15 changes: 15 additions & 0 deletions test/fixtures/apps/dumptiming-slowBootActionMinDuration/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module.exports = class {
constructor(app) {
this.app = app;
}

async didLoad() {
this.app.coreLogger.info('start doing sth in didLoad');
return new Promise(resolve => {
setTimeout(() => {
this.app.coreLogger.info('end doing sth in didLoad');
resolve();
}, 150);
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
exports.keys = 'test key';
exports.dump = {
timing: {
slowBootActionMinDuration: 100,
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exports.static = false;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "dumptiming-slowBootActionMinDuration"
}
10 changes: 10 additions & 0 deletions test/lib/egg.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,16 @@ describe('test/lib/egg.test.js', () => {
assertFile(path.join(baseDir, `run/application_timing_${process.pid}.json`));
assertFile(path.join(baseDir, 'logs/dumptiming-timeout/common-error.log'), /unfinished timing item: {"name":"Did Load in app.js:didLoad"/);
});

it('should dump slow-boot-action warnning log', async () => {
const baseDir = utils.getFilepath('apps/dumptiming-slowBootActionMinDuration');
await rimraf(path.join(baseDir, 'run'));
await rimraf(path.join(baseDir, 'logs'));
const app = utils.app(baseDir);
await app.ready();
await sleep(100);
assertFile(path.join(baseDir, 'logs/dumptiming-slowBootActionMinDuration/egg-web.log'), /\[egg:core]\[slow-boot-action] #\d+ \d+ms, name: Did Load in app\.js:didLoad/);
});
});

describe('dump disabled plugin', () => {
Expand Down

0 comments on commit 3a941d6

Please sign in to comment.