Skip to content

Instantly share code, notes, and snippets.

@amitsingh-007
Last active October 20, 2020 13:22
Show Gist options
  • Save amitsingh-007/a67f4d101ad225b8d14c1f05e1cfad7f to your computer and use it in GitHub Desktop.
Save amitsingh-007/a67f4d101ad225b8d14c1f05e1cfad7f to your computer and use it in GitHub Desktop.
Get mean data from multiple lighthouse reports
const glob = require("glob");
const path = require("path");
const configs = [];
glob.sync("./lh-reports/*.json").forEach(file => {
configs.push(require(path.resolve(file)));
});
const stats = {
FCP: { value: 0, unit: "s" },
TTI: { value: 0, unit: "s" },
TBT: { value: 0, unit: "ms" },
SI: { value: 0, unit: "s" },
InitialServerResponseTime: { value: 0, unit: "ms" },
ScriptEvaluation: { value: 0, unit: "ms" },
ScriptParsingAndCompilation: { value: 0, unit: "ms" },
};
const printStats = result => {
for (let [key, value] of Object.entries(result)) {
console.log(
`${key} : ${(value.value / configs.length).toFixed(2)} ${value.unit}`
);
}
};
const getStats = () => {
configs.reduce((acc, config) => {
const { audits } = config;
const mainThreadInfo = audits["mainthread-work-breakdown"].details.items;
acc.FCP.value += audits["first-contentful-paint"].numericValue / 1000;
acc.TTI.value += audits["interactive"].numericValue / 1000;
acc.TBT.value += audits["total-blocking-time"].numericValue;
acc.InitialServerResponseTime.value +=
audits["server-response-time"].numericValue;
acc.SI.value += audits["speed-index"].numericValue;
mainThreadInfo.forEach(info => {
if (info.group === "scriptEvaluation") {
acc.ScriptEvaluation.value += info.duration;
} else if (info.group === "scriptParseCompile") {
acc.ScriptParsingAndCompilation.value += info.duration;
}
});
return acc;
}, stats);
printStats(stats);
};
getStats();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment