Skip to content

Commit

Permalink
add web SSR timing reporting events (#1672)
Browse files Browse the repository at this point in the history
  • Loading branch information
divyakarippath authored Jun 16, 2020
1 parent 6c38df5 commit 6d8eaf9
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 2 deletions.
6 changes: 6 additions & 0 deletions packages/subapp-server/lib/fastify-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ module.exports = {

const { routes, topOpts } = searchRoutesFromFile(srcDir, pluginOpts);

const reporting = _.get(topOpts, "reporting", {});
if (!reporting.enable || !reporting.reporter) {
// eslint-disable-next-line
console.warn(`Warning: Metric reporting for ssr not enabled or no reporter specified.`);
}

const subApps = await subAppUtil.scanSubAppsFromDir(srcDir);
const subAppsByPath = subAppUtil.getSubAppByPathMap(subApps);

Expand Down
12 changes: 12 additions & 0 deletions packages/subapp-server/lib/setup-hapi-routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,12 @@ function searchRoutesFromFile(srcDir, pluginOpts) {
async function setupRoutesFromFile(srcDir, server, pluginOpts) {
const { routes, topOpts } = searchRoutesFromFile(srcDir, pluginOpts);

const reporting = _.get(topOpts, "reporting", {});
if (!reporting.enable || !reporting.reporter) {
// eslint-disable-next-line
console.warn(`Warning: Metric reporting for ssr not enabled or no reporter specified.`);
}

await handleFavIcon(server, topOpts);

// invoke setup callback
Expand All @@ -268,6 +274,12 @@ async function setupRoutesFromDir(server, pluginOpts, fromDir) {

const topOpts = _.merge(getDefaultRouteOptions(), fromDir.options, pluginOpts);

const reporting = _.get(topOpts, "reporting", {});
if (!reporting.enable || !reporting.reporter) {
// eslint-disable-next-line
console.warn(`Warning: Metric reporting for ssr not enabled or no reporter specified.`);
}

topOpts.routes = _.merge({}, routes, topOpts.routes);

updateFullTemplate(fromDir.dir, topOpts);
Expand Down
3 changes: 2 additions & 1 deletion packages/subapp-server/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ function getDefaultRouteOptions() {
devBundleBase: "/js",
cspNonceValue: undefined,
templateFile: Path.join(__dirname, "..", "resources", "index-page"),
cdn: {}
cdn: {},
reporting: { enable: false }
};
}

Expand Down
7 changes: 7 additions & 0 deletions packages/subapp-web/lib/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const Fs = require("fs");
const Path = require("path");
const util = require("./util");
const subappUtil = require("subapp-util");
const _ = require("lodash");

module.exports = function setup(setupContext) {
const distDir = process.env.NODE_ENV === "production" ? "../dist/min" : "../dist/dev";
Expand All @@ -16,6 +17,8 @@ module.exports = function setup(setupContext) {
// TODO: in webpack dev mode, we need to reload stats after there's a change
//

const metricReport = _.get(setupContext, "routeOptions.reporting", {});

const { assets } = util.loadAssetsFromStats(setupContext.routeOptions.stats);
setupContext.routeOptions.__internals.assets = assets;

Expand Down Expand Up @@ -68,6 +71,10 @@ ${inlineRuntimeJS}
runtimeEntryPoints.forEach(ep => {
context.user.includedBundles[ep] = true;
});

if (metricReport.enable && metricReport.reporter) {
context.user.xarcSSREmitter = util.getEventEmiiter(metricReport.reporter);
}
// invoke the initialize method of subapp's server code
if (subAppServers.length > 0) {
for (const server of subAppServers) {
Expand Down
3 changes: 3 additions & 0 deletions packages/subapp-web/lib/load.js
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,9 @@ ${stack}`,
}

if (props.serverSideRendering) {
if (!context.user[`prepare-grp-${props.group}`]) {
context.user[`prepare-grp-${props.group}`] = Date.now();
}
const lib = (ssrInfo.lib = util.getFramework(ref));
ssrInfo.awaitData = lib.handlePrepare();

Expand Down
35 changes: 34 additions & 1 deletion packages/subapp-web/lib/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const xaa = require("xaa");
module.exports = function setup() {
return {
process: (context, { props: { concurrency } }) => {
const { xarcSubappSSR } = context.user;
const { xarcSubappSSR, xarcSSREmitter } = context.user;
const startMsg = `
<!-- subapp start -->
<script>window.xarcV1.start();</script>
Expand Down Expand Up @@ -63,6 +63,7 @@ module.exports = function setup() {

// default group _ subapps should all run independently
if (xarcSubappSSR._) {
const startTime = Date.now();
xaa
.map(
xarcSubappSSR._.queue,
Expand All @@ -76,6 +77,12 @@ module.exports = function setup() {
context.voidStop(err);
xaa.map(xarcSubappSSR._.queue, async info => info.done(), { concurrency });
});
if (xarcSSREmitter) {
xarcSSREmitter.emit("web_ssr", {
action: `subapps-ssr`,
duration: Date.now() - startTime
});
}
}

xaa
Expand All @@ -85,6 +92,8 @@ module.exports = function setup() {
if (group !== "_") {
mapCtx.assertNoFailure();

const loadStartTime = Date.now();

// first ensure everyone in the queue finish preparing
await xaa.map(
queue,
Expand All @@ -95,10 +104,19 @@ module.exports = function setup() {
},
{ concurrency }
);
if (xarcSSREmitter) {
xarcSSREmitter.emit("web_ssr", {
action: `prepare-group`,
labels: ["group"],
group,
duration: Date.now() - context.user[`prepare-grp-${group}`]
});
}

mapCtx.assertNoFailure();

// and then kick off rendering for every subapp in the group
const renderStartTime = Date.now();
await xaa.map(
queue,
async (v, ix2, ctx2) => {
Expand All @@ -108,6 +126,21 @@ module.exports = function setup() {
},
{ concurrency }
);
if (xarcSSREmitter) {
const now = Date.now();
xarcSSREmitter.emit("web_ssr", {
action: `render-group`,
group,
labels: ["group"],
duration: now - renderStartTime
});
xarcSSREmitter.emit("web_ssr", {
action: `group-ssr-total`,
group,
labels: ["group"],
duration: now - loadStartTime
});
}
}
},
{ concurrency }
Expand Down
19 changes: 19 additions & 0 deletions packages/subapp-web/lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const assert = require("assert");
const Path = require("path");
const _ = require("lodash");
const { tryThrowOriginalSubappRegisterError } = require("subapp-util");
const EventEmitter = require("events");

let CDN_ASSETS;
let CDN_JS_BUNDLES;
Expand Down Expand Up @@ -341,6 +342,24 @@ ${ignoreMsg}`
assets.chunks = stats.chunks;

return { assets, stats };
},
getEventEmiiter(reporter) {
const emitter = new EventEmitter();
const groupEvents = {};

emitter.on("web_ssr", (data = {}) => {
const group = data.group || "_";
groupEvents[group] = groupEvents[group] || [];
groupEvents[group].push(data);

if (data.action === "group-ssr-total" || data.action === "subapps-ssr") {
const events = groupEvents[group];
events.forEach((event) => reporter(event));
delete groupEvents[group];
}
});

return emitter;
}
};

Expand Down

0 comments on commit 6d8eaf9

Please sign in to comment.