@@ -36,6 +36,7 @@ import workerBuild from './workerBuild';
3636import { inlineSvelteComponent } from './partialHydration/inlineSvelteComponent' ;
3737import elderJsShortcodes from './shortcodes' ;
3838import prepareRouter from './routes/prepareRouter' ;
39+ import perf , { displayPerfTimings } from './utils/perf' ;
3940
4041class Elder {
4142 bootstrapComplete : Promise < any > ;
@@ -70,13 +71,18 @@ class Elder {
7071
7172 shortcodes : ShortcodeDefs ;
7273
74+ perf : any ;
75+
76+ uid : string ;
77+
7378 router : ( any ) => any;
7479
7580 constructor ( initializationOptions : InitializationOptions = { } ) {
7681 const initialOptions = { ...initializationOptions } ;
7782 this . bootstrapComplete = new Promise ( ( resolve ) => {
7883 this . markBootstrapComplete = resolve ;
7984 } ) ;
85+ this . uid = 'startup' ;
8086
8187 // merge the given config with the project and defaults;
8288 this . settings = getConfig ( initializationOptions ) ;
@@ -96,8 +102,13 @@ class Elder {
96102 this . server = prepareServer ( { bootstrapComplete : this . bootstrapComplete } ) ;
97103 }
98104
105+ perf ( this , true ) ;
106+
107+ this . perf . start ( 'startup' ) ;
108+
99109 // plugins are run first as they have routes, hooks, and shortcodes.
100110 plugins ( this ) . then ( async ( { pluginRoutes, pluginHooks, pluginShortcodes } ) => {
111+ this . perf . start ( 'startup.validations' ) ;
101112 /**
102113 * Finalize Routes
103114 * Add in user routes
@@ -203,6 +214,8 @@ class Elder {
203214 . map ( ( shortcode ) => validateShortcode ( shortcode ) )
204215 . filter ( Boolean as any as ExcludesFalse ) ;
205216
217+ this . perf . end ( 'startup.validations' ) ;
218+
206219 /**
207220 *
208221 * Almost ready for customize hooks and bootstrap
@@ -243,16 +256,21 @@ class Elder {
243256 await this . runHook ( 'bootstrap' , this ) ;
244257
245258 // collect all of our requests
259+ this . perf . start ( 'startup.routes' ) ;
246260 await asyncForEach ( Object . keys ( this . routes ) , async ( routeName ) => {
261+ this . perf . start ( `startup.routes.${ routeName } ` ) ;
247262 const route = this . routes [ routeName ] ;
248263 let allRequestsForRoute = [ ] ;
249264 if ( typeof route . all === 'function' ) {
265+ this . perf . start ( `startup.routes.${ routeName } ` ) ;
250266 allRequestsForRoute = await route . all ( {
251267 settings : createReadOnlyProxy ( this . settings , 'settings' , `${ routeName } all function` ) ,
252268 query : createReadOnlyProxy ( this . query , 'query' , `${ routeName } all function` ) ,
253269 helpers : createReadOnlyProxy ( this . helpers , 'helpers' , `${ routeName } all function` ) ,
254270 data : createReadOnlyProxy ( this . data , 'data' , `${ routeName } all function` ) ,
271+ perf : this . perf . prefix ( `startup.routes.${ routeName } .all` ) ,
255272 } ) ;
273+ this . perf . end ( `startup.routes.${ routeName } ` ) ;
256274 } else if ( Array . isArray ( route . all ) ) {
257275 allRequestsForRoute = route . all ;
258276 }
@@ -270,10 +288,14 @@ class Elder {
270288 return out ;
271289 } , [ ] ) ;
272290 this . allRequests = this . allRequests . concat ( allRequestsForRoute ) ;
291+ this . perf . end ( `startup.routes.${ routeName } ` ) ;
273292 } ) ;
274293
294+ this . perf . end ( `startup.routes` ) ;
295+
275296 await this . runHook ( 'allRequests' , this ) ;
276297
298+ this . perf . start ( `startup.setPermalinks` ) ;
277299 await asyncForEach ( this . allRequests , async ( request ) => {
278300 if ( ! this . routes [ request . route ] || ! this . routes [ request . route ] . permalink ) {
279301 if ( ! request . route ) {
@@ -303,6 +325,9 @@ class Elder {
303325 this . serverLookupObject [ request . permalink ] = request ;
304326 }
305327 } ) ;
328+ this . perf . end ( `startup.setPermalinks` ) ;
329+
330+ this . perf . start ( `startup.validatePermalinks` ) ;
306331
307332 if ( this . allRequests . length !== new Set ( this . allRequests . map ( ( r ) => r . permalink ) ) . size ) {
308333 // useful error logging for when there are duplicate permalinks.
@@ -318,10 +343,26 @@ class Elder {
318343 }
319344 }
320345 }
346+ this . perf . end ( `startup.validatePermalinks` ) ;
321347
348+ this . perf . start ( `startup.prepareRouter` ) ;
322349 this . router = prepareRouter ( this ) ;
350+ this . perf . end ( `startup.prepareRouter` ) ;
323351
324352 this . markBootstrapComplete ( this ) ;
353+
354+ this . perf . end ( 'startup' ) ;
355+ this . perf . stop ( ) ;
356+
357+ const t = this . perf . timings . slice ( - 1 ) [ 0 ] && Math . round ( this . perf . timings . slice ( - 1 ) [ 0 ] . duration * 10 ) / 10 ;
358+ if ( t && t > 0 ) {
359+ console . log (
360+ `Elder.js Startup: ${ t } ms. ${ t > 5000 ? `For details set debug.performance: true in elder.config.js` : '' } ` ,
361+ ) ;
362+ if ( this . settings . debug . performance ) {
363+ displayPerfTimings ( [ ...this . perf . timings ] ) ;
364+ }
365+ }
325366 } ) ;
326367 } ) ;
327368 }
0 commit comments