Skip to content

Commit 965f840

Browse files
authored
Feat/perf improvements (#241)
* add perf to hooks * Add perf to Page, Elder, Plugins. * Remove perf from what is passed into Page. * Finish modularizing hooks. * rename bootstrap to startup. * get tests passing
1 parent e9b80fe commit 965f840

File tree

17 files changed

+211
-23
lines changed

17 files changed

+211
-23
lines changed

src/Elder.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import workerBuild from './workerBuild';
3636
import { inlineSvelteComponent } from './partialHydration/inlineSvelteComponent';
3737
import elderJsShortcodes from './shortcodes';
3838
import prepareRouter from './routes/prepareRouter';
39+
import perf, { displayPerfTimings } from './utils/perf';
3940

4041
class 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
}

src/__tests__/Elder.spec.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ describe('#Elder', () => {
5858
for (const pluginHook of page.hooks) {
5959
if (pluginHook.$$meta.type === 'plugin') {
6060
// eslint-disable-next-line
61-
await pluginHook.run({});
61+
await pluginHook.run({});
6262
}
6363
}
6464
}
@@ -111,6 +111,7 @@ describe('#Elder', () => {
111111
const elder = await new Elder({ context: 'server', worker: false });
112112
await elder.bootstrap();
113113
await elder.worker([]);
114+
delete elder.perf.timings;
114115
expect(normalizeSnapshot(elder)).toMatchSnapshot();
115116
});
116117

@@ -200,6 +201,8 @@ describe('#Elder', () => {
200201
const { Elder } = require(`..${sep}index`);
201202
const elder = await new Elder({ context: 'server', worker: false });
202203
await elder.bootstrap();
204+
205+
delete elder.perf.timings;
203206
expect(normalizeSnapshot(elder)).toMatchSnapshot();
204207
});
205208
});

0 commit comments

Comments
 (0)