Skip to content

Commit bb24396

Browse files
authored
feat: pick commit from 3.x (#166)
* feat: add timing data for loader (#160) * refactor: add timing to extend (#162) * fix: Loader can be instantiated without timing (#161) * fix: convert symbol to string (#163) * fix: timing can be started multiple times (#164)
1 parent a43dcb9 commit bb24396

42 files changed

Lines changed: 425 additions & 34 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.eslintignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ node_modules
22
coverage
33
benchmark
44
fixtures
5-
.vscode
5+
.vscode

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ coverage
44
npm-debug.log
55
.vscode
66
.DS_Store
7-
yarn.lock
7+
yarn.lock
8+
test/fixtures/egg/node_modules/egg-core

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ sudo: false
22
language: node_js
33
node_js:
44
- '8'
5-
- '9'
5+
- '10'
66
install:
77
- npm i npminstall && npminstall
88
script:

appveyor.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
environment:
22
matrix:
33
- nodejs_version: '8'
4-
- nodejs_version: '9'
4+
- nodejs_version: '10'
55

66
install:
77
- ps: Install-Product node $env:nodejs_version

lib/egg.js

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const co = require('co');
1010
const BaseContextClass = require('./utils/base_context_class');
1111
const utils = require('./utils');
1212
const Router = require('./utils/router');
13-
13+
const Timing = require('./utils/timing');
1414

1515
const DEPRECATE = Symbol('EggCore#deprecate');
1616
const CLOSESET = Symbol('EggCore#closeSet');
@@ -19,7 +19,7 @@ const CLOSE_PROMISE = Symbol('EggCore#closePromise');
1919
const ROUTER = Symbol('EggCore#router');
2020
const EGG_LOADER = Symbol.for('egg#loader');
2121
const INIT_READY = Symbol('EggCore#initReady');
22-
const EGG_READY_TIMEOUT_ENV = Symbol('EggCore#eggReadyTimeoutEnv');
22+
2323

2424
class EggCore extends KoaApplication {
2525

@@ -43,16 +43,17 @@ class EggCore extends KoaApplication {
4343

4444
super();
4545

46+
this.timing = new Timing();
47+
4648
// register a close set
4749
this[CLOSESET] = new Set();
4850
// cache deprecate object by file
4951
this[DEPRECATE] = new Map();
5052

51-
// get app timeout from env or use default timeout 10 second
52-
const eggReadyTimeoutEnv = process.env.EGG_READY_TIMEOUT_ENV;
53-
this[EGG_READY_TIMEOUT_ENV] = Number.parseInt(eggReadyTimeoutEnv || 10000);
53+
this[INIT_READY]();
5454

55-
assert(Number.isInteger(this[EGG_READY_TIMEOUT_ENV]), `process.env.EGG_READY_TIMEOUT_ENV ${eggReadyTimeoutEnv} should be able to parseInt.`);
55+
this.timing.start('Application Start');
56+
this.ready(() => this.timing.end('Application Start'));
5657

5758
/**
5859
* @member {Object} EggCore#options
@@ -124,8 +125,6 @@ class EggCore extends KoaApplication {
124125
logger: this.console,
125126
serverScope: options.serverScope,
126127
});
127-
128-
this[INIT_READY]();
129128
}
130129

131130
/**
@@ -216,16 +215,26 @@ class EggCore extends KoaApplication {
216215

217216
// get filename from stack
218217
const name = utils.getCalleeFromStack(true);
218+
const timingkey = 'Before Start in ' + utils.getResolvedFilename(name, this.options.baseDir);
219+
220+
this.timing.start(timingkey);
221+
219222
const done = this.readyCallback(name);
220223

221224
// ensure scope executes after load completed
222225
process.nextTick(() => {
223-
utils.callFn(scope).then(() => done(), done);
226+
utils.callFn(scope).then(() => {
227+
done();
228+
this.timing.end(timingkey);
229+
}, err => {
230+
done(err);
231+
this.timing.end(timingkey);
232+
});
224233
});
225234
}
226235

227236
/**
228-
* Close all, it will close
237+
* Close all, it wisll close
229238
* - callbacks registered by beforeClose
230239
* - emit `close` event
231240
* - remove add listeners
@@ -281,6 +290,10 @@ class EggCore extends KoaApplication {
281290
* });
282291
*/
283292

293+
// get app timeout from env or use default timeout 10 second
294+
const eggReadyTimeoutEnv = Number.parseInt(process.env.EGG_READY_TIMEOUT_ENV || 10000);
295+
assert(Number.isInteger(eggReadyTimeoutEnv), `process.env.EGG_READY_TIMEOUT_ENV ${process.env.EGG_READY_TIMEOUT_ENV} should be able to parseInt.`);
296+
284297
/**
285298
* If a client starts asynchronously, you can register `readyCallback`,
286299
* then the application will wait for the callback to ready
@@ -294,15 +307,13 @@ class EggCore extends KoaApplication {
294307
* const done = app.readyCallback('mysql');
295308
* mysql.ready(done);
296309
*/
297-
require('ready-callback')({ timeout: this[EGG_READY_TIMEOUT_ENV] }).mixin(this);
310+
require('ready-callback')({ timeout: eggReadyTimeoutEnv }).mixin(this);
298311

299312
this.on('ready_stat', data => {
300313
this.console.info('[egg:core:ready_stat] end ready task %s, remain %j', data.id, data.remain);
301314
}).on('ready_timeout', id => {
302-
this.console.warn('[egg:core:ready_timeout] %s seconds later %s was still unable to finish.', this[EGG_READY_TIMEOUT_ENV] / 1000, id);
315+
this.console.warn('[egg:core:ready_timeout] %s seconds later %s was still unable to finish.', eggReadyTimeoutEnv / 1000, id);
303316
});
304-
305-
this.ready(() => debug('egg emit ready, application started'));
306317
}
307318

308319
/**

lib/loader/egg_loader.js

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ const FileLoader = require('./file_loader');
1010
const ContextLoader = require('./context_loader');
1111
const utility = require('utility');
1212
const utils = require('../utils');
13+
const Timing = require('../utils/timing');
14+
15+
const REQUIRE_COUNT = Symbol('EggLoader#requireCount');
1316

1417

1518
class EggLoader {
@@ -30,6 +33,8 @@ class EggLoader {
3033
assert(this.options.logger, 'options.logger is required');
3134

3235
this.app = this.options.app;
36+
this.timing = this.app.timing || new Timing();
37+
this[REQUIRE_COUNT] = 0;
3338

3439
/**
3540
* @member {Object} EggLoader#pkg
@@ -286,10 +291,25 @@ class EggLoader {
286291
return null;
287292
}
288293

289-
const ret = utils.loadFile(filepath);
290294
// function(arg1, args, ...) {}
291295
if (inject.length === 0) inject = [ this.app ];
292-
return isFunction(ret) ? ret(...inject) : ret;
296+
297+
let ret = this.requireFile(filepath);
298+
ret = isFunction(ret) ? ret(...inject) : ret;
299+
return ret;
300+
}
301+
302+
/**
303+
* @param {String} filepath - fullpath
304+
* @return {Object} exports
305+
* @private
306+
*/
307+
requireFile(filepath) {
308+
const timingKey = `Require(${this[REQUIRE_COUNT]++}) ${utils.getResolvedFilename(filepath, this.options.baseDir)}`;
309+
this.timing.start(timingKey);
310+
const ret = utils.loadFile(filepath);
311+
this.timing.end(timingKey);
312+
return ret;
293313
}
294314

295315
/**
@@ -355,7 +375,11 @@ class EggLoader {
355375
target,
356376
inject: this.app,
357377
}, opt);
378+
379+
const timingKey = `Load "${String(property)}" to Application`;
380+
this.timing.start(timingKey);
358381
new FileLoader(opt).load();
382+
this.timing.end(timingKey);
359383
}
360384

361385
/**
@@ -371,7 +395,11 @@ class EggLoader {
371395
property,
372396
inject: this.app,
373397
}, opt);
398+
399+
const timingKey = `Load "${String(property)}" to Context`;
400+
this.timing.start(timingKey);
374401
new ContextLoader(opt).load();
402+
this.timing.end(timingKey);
375403
}
376404

377405
/**

lib/loader/mixin/config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const path = require('path');
55
const extend = require('extend2');
66
const assert = require('assert');
77

8+
89
const SET_CONFIG_META = Symbol('Loader#setConfigMeta');
910

1011
module.exports = {
@@ -18,6 +19,7 @@ module.exports = {
1819
* @since 1.0.0
1920
*/
2021
loadConfig() {
22+
this.timing.start('Load Config');
2123
this.configMeta = {};
2224

2325
const target = {};
@@ -50,6 +52,7 @@ module.exports = {
5052
target.appMiddleware = target.appMiddlewares = target.middleware || [];
5153

5254
this.config = target;
55+
this.timing.end('Load Config');
5356
},
5457

5558
_preloadAppConfig() {

lib/loader/mixin/controller.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const utility = require('utility');
66
const utils = require('../../utils');
77
const FULLPATH = require('../file_loader').FULLPATH;
88

9+
910
module.exports = {
1011

1112
/**
@@ -14,6 +15,7 @@ module.exports = {
1415
* @since 1.0.0
1516
*/
1617
loadController(opt) {
18+
this.timing.start('Load Controller');
1719
opt = Object.assign({
1820
caseStyle: 'lower',
1921
directory: path.join(this.options.baseDir, 'app/controller'),
@@ -46,6 +48,7 @@ module.exports = {
4648

4749
this.loadToApp(controllerBase, 'controller', opt);
4850
this.options.logger.info('[egg:loader] Controller loaded: %s', controllerBase);
51+
this.timing.end('Load Controller');
4952
},
5053

5154
};

lib/loader/mixin/custom.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const path = require('path');
44

5+
56
module.exports = {
67

78
/**
@@ -21,16 +22,20 @@ module.exports = {
2122
* @since 1.0.0
2223
*/
2324
loadCustomApp() {
25+
this.timing.start('Load app.js');
2426
this.getLoadUnits()
2527
.forEach(unit => this.loadFile(this.resolveModule(path.join(unit.path, 'app'))));
28+
this.timing.end('Load app.js');
2629
},
2730

2831
/**
2932
* Load agent.js, same as {@link EggLoader#loadCustomApp}
3033
*/
3134
loadCustomAgent() {
35+
this.timing.start('Load agent.js');
3236
this.getLoadUnits()
3337
.forEach(unit => this.loadFile(this.resolveModule(path.join(unit.path, 'agent'))));
38+
this.timing.end('Load agent.js');
3439
},
3540

3641
};

lib/loader/mixin/extend.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ module.exports = {
8989
* @since 1.0.0
9090
*/
9191
loadExtend(name, proto) {
92+
this.timing.start(`Load extend/${name}.js`);
9293
// All extend files
9394
const filepaths = this.getExtendFilePaths(name);
9495
// if use mm.env and serverEnv is not unittest
@@ -109,7 +110,7 @@ module.exports = {
109110
deprecate(`app/extend/${name}/index.js is deprecated, use app/extend/${name}.js instead`);
110111
}
111112

112-
const ext = this.loadFile(filepath);
113+
const ext = this.requireFile(filepath);
113114

114115
const properties = Object.getOwnPropertyNames(ext)
115116
.concat(Object.getOwnPropertySymbols(ext));
@@ -145,5 +146,6 @@ module.exports = {
145146
}
146147
debug('merge %j to %s from %s', Object.keys(ext), name, filepath);
147148
}
149+
this.timing.end(`Load extend/${name}.js`);
148150
},
149151
};

0 commit comments

Comments
 (0)