perfrunner-reporters
Version:
Home of the perfrunner reporters
54 lines (53 loc) • 3.12 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.defaultHtmlReporter = void 0;
const path_1 = require("path");
const mustache_1 = require("mustache");
const utils_1 = require("../../utils");
const charts_1 = require("./charts");
const fs_1 = require("../../utils/fs");
const json_1 = require("../../utils/json");
const groupByPerfConditions = (performanceRuns) => (0, utils_1.groupBy)(performanceRuns, ({ runParams: { useCache, network, throttlingRate } }) => `${network.downloadThroughput}_${network.uploadThroughput}_${network.latency}_${throttlingRate}_${useCache ? 1 : 0}`);
const getPageMetadata = (pageName, isActive) => ({
pageName,
contentId: `x_${(0, utils_1.hash)(pageName)}`,
pageClass: isActive ? 'active' : '',
tabClass: isActive ? 'active show' : '',
});
function getTabName(perfResult, i) {
var _a;
const options = perfResult[0].runParams;
const networkName = (_a = options.network.name) !== null && _a !== void 0 ? _a : `#${i}`;
const throttling = options.throttlingRate === 0 ? `no throttling` : `throttling: ${options.throttlingRate}`;
const cache = !!options.useCache ? `with cache` : `no cache`;
return `${networkName}, ${throttling}, ${cache}`;
}
const generateHtmlReport = (data, args) => __awaiter(void 0, void 0, void 0, function* () {
const templatePath = (0, path_1.join)(__dirname, 'index.html');
const href = (0, utils_1.isAllSame)(data.map((x) => x.runParams.url)) ? data[0].runParams.url : 'Various';
const groupedData = groupByPerfConditions(data);
const allReporters = (0, charts_1.getReporterRegistry)();
const reporters = ((args === null || args === void 0 ? void 0 : args.length) ? args : charts_1.defaultReporterNames).filter((x) => allReporters[x.toLowerCase()]);
const template = yield (0, fs_1.readFileAsync)(templatePath);
const result = (0, mustache_1.render)(template, {
href: href.length < 43 ? href : href.substr(0, 42),
pages: groupedData.map((d, i) => getPageMetadata(getTabName(d, i), i === 0)),
reporters: reporters,
payload: (0, json_1.sanitizeJson)(JSON.stringify(groupedData)),
arguments: (0, json_1.sanitizeJson)(JSON.stringify(reporters)),
});
return result;
});
exports.defaultHtmlReporter = {
defaultReportName: 'default-report.html',
generateReport: generateHtmlReport,
};