Merge pull request #13096 from Snuffleupagus/eslint-no-var-stats

Enable the ESLint `no-var` rule in the `test/stats/` folder
This commit is contained in:
Tim van der Meij 2021-03-14 11:34:06 +01:00 committed by GitHub
commit cc59c81fe6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,20 +1,12 @@
/* eslint-disable no-var */
"use strict"; "use strict";
var fs = require("fs"); const fs = require("fs");
const ttest = require("ttest");
try { const VALID_GROUP_BYS = ["browser", "pdf", "page", "round", "stat"];
var ttest = require("ttest");
} catch (e) {
console.log('\nttest is not installed -- to intall, run "npm install ttest"');
console.log("Continuing without significance test...\n");
}
var VALID_GROUP_BYS = ["browser", "pdf", "page", "round", "stat"];
function parseOptions() { function parseOptions() {
var yargs = require("yargs") const yargs = require("yargs")
.usage( .usage(
"Compare the results of two stats files.\n" + "Compare the results of two stats files.\n" +
"Usage:\n $0 <BASELINE> <CURRENT> [options]" "Usage:\n $0 <BASELINE> <CURRENT> [options]"
@ -27,7 +19,7 @@ function parseOptions() {
VALID_GROUP_BYS.join(" ") VALID_GROUP_BYS.join(" ")
) )
.default("groupBy", "browser,stat"); .default("groupBy", "browser,stat");
var result = yargs.argv; const result = yargs.argv;
result.baseline = result._[0]; result.baseline = result._[0];
result.current = result._[1]; result.current = result._[1];
if (result.groupBy) { if (result.groupBy) {
@ -37,14 +29,14 @@ function parseOptions() {
} }
function group(stats, groupBy) { function group(stats, groupBy) {
var vals = []; const vals = [];
for (var i = 0; i < stats.length; i++) { for (let i = 0; i < stats.length; i++) {
var curStat = stats[i]; const curStat = stats[i];
var keyArr = []; const keyArr = [];
for (var j = 0; j < groupBy.length; j++) { for (let j = 0; j < groupBy.length; j++) {
keyArr.push(curStat[groupBy[j]]); keyArr.push(curStat[groupBy[j]]);
} }
var key = keyArr.join(","); const key = keyArr.join(",");
if (vals[key] === undefined) { if (vals[key] === undefined) {
vals[key] = []; vals[key] = [];
} }
@ -58,7 +50,7 @@ function group(stats, groupBy) {
* Also, if results are not grouped by 'stat', keep only 'Overall' results. * Also, if results are not grouped by 'stat', keep only 'Overall' results.
*/ */
function flatten(stats) { function flatten(stats) {
var rows = []; let rows = [];
stats.forEach(function (curStat) { stats.forEach(function (curStat) {
curStat.stats.forEach(function (s) { curStat.stats.forEach(function (s) {
rows.push({ rows.push({
@ -82,7 +74,7 @@ function flatten(stats) {
function pad(s, length, dir /* default: 'right' */) { function pad(s, length, dir /* default: 'right' */) {
s = "" + s; s = "" + s;
var spaces = new Array(Math.max(0, length - s.length + 1)).join(" "); const spaces = new Array(Math.max(0, length - s.length + 1)).join(" ");
return dir === "left" ? spaces + s : s + spaces; return dir === "left" ? spaces + s : s + spaces;
} }
@ -97,11 +89,11 @@ function mean(array) {
function compareRow(a, b) { function compareRow(a, b) {
a = a.split(","); a = a.split(",");
b = b.split(","); b = b.split(",");
for (var i = 0; i < Math.min(a.length, b.length); i++) { for (let i = 0; i < Math.min(a.length, b.length); i++) {
var intA = parseInt(a[i], 10); const intA = parseInt(a[i], 10);
var intB = parseInt(b[i], 10); const intB = parseInt(b[i], 10);
var ai = isNaN(intA) ? a[i] : intA; const ai = isNaN(intA) ? a[i] : intA;
var bi = isNaN(intB) ? b[i] : intB; const bi = isNaN(intB) ? b[i] : intB;
if (ai < bi) { if (ai < bi) {
return -1; return -1;
} }
@ -125,30 +117,28 @@ function compareRow(a, b) {
* be implied. * be implied.
*/ */
function stat(baseline, current) { function stat(baseline, current) {
var baselineGroup = group(baseline, options.groupBy); const baselineGroup = group(baseline, options.groupBy);
var currentGroup = group(current, options.groupBy); const currentGroup = group(current, options.groupBy);
var keys = Object.keys(baselineGroup); const keys = Object.keys(baselineGroup);
keys.sort(compareRow); keys.sort(compareRow);
var labels = options.groupBy.slice(0); const labels = options.groupBy.slice(0);
labels.push("Count", "Baseline(ms)", "Current(ms)", "+/-", "% "); labels.push("Count", "Baseline(ms)", "Current(ms)", "+/-", "% ");
if (ttest) { if (ttest) {
labels.push("Result(P<.05)"); labels.push("Result(P<.05)");
} }
var i, const rows = [];
row,
rows = [];
// collect rows and measure column widths // collect rows and measure column widths
var width = labels.map(function (s) { const width = labels.map(function (s) {
return s.length; return s.length;
}); });
rows.push(labels); rows.push(labels);
for (var k = 0; k < keys.length; k++) { for (let k = 0; k < keys.length; k++) {
var key = keys[k]; const key = keys[k];
var baselineMean = mean(baselineGroup[key]); const baselineMean = mean(baselineGroup[key]);
var currentMean = mean(currentGroup[key]); const currentMean = mean(currentGroup[key]);
row = key.split(","); const row = key.split(",");
row.push( row.push(
"" + baselineGroup[key].length, "" + baselineGroup[key].length,
"" + Math.round(baselineMean), "" + Math.round(baselineMean),
@ -157,7 +147,7 @@ function stat(baseline, current) {
((100 * (currentMean - baselineMean)) / baselineMean).toFixed(2) ((100 * (currentMean - baselineMean)) / baselineMean).toFixed(2)
); );
if (ttest) { if (ttest) {
var p = const p =
baselineGroup[key].length < 2 baselineGroup[key].length < 2
? 1 ? 1
: ttest(baselineGroup[key], currentGroup[key]).pValue(); : ttest(baselineGroup[key], currentGroup[key]).pValue();
@ -167,24 +157,24 @@ function stat(baseline, current) {
row.push(""); row.push("");
} }
} }
for (i = 0; i < row.length; i++) { for (let i = 0; i < row.length; i++) {
width[i] = Math.max(width[i], row[i].length); width[i] = Math.max(width[i], row[i].length);
} }
rows.push(row); rows.push(row);
} }
// add horizontal line // add horizontal line
var hline = width.map(function (w) { const hline = width.map(function (w) {
return new Array(w + 1).join("-"); return new Array(w + 1).join("-");
}); });
rows.splice(1, 0, hline); rows.splice(1, 0, hline);
// print output // print output
console.log("-- Grouped By " + options.groupBy.join(", ") + " --"); console.log("-- Grouped By " + options.groupBy.join(", ") + " --");
var groupCount = options.groupBy.length; const groupCount = options.groupBy.length;
for (var r = 0; r < rows.length; r++) { for (let r = 0; r < rows.length; r++) {
row = rows[r]; const row = rows[r];
for (i = 0; i < row.length; i++) { for (let i = 0; i < row.length; i++) {
row[i] = pad(row[i], width[i], i < groupCount ? "right" : "left"); row[i] = pad(row[i], width[i], i < groupCount ? "right" : "left");
} }
console.log(row.join(" | ")); console.log(row.join(" | "));
@ -192,16 +182,16 @@ function stat(baseline, current) {
} }
function main() { function main() {
var baseline, current; let baseline, current;
try { try {
var baselineFile = fs.readFileSync(options.baseline).toString(); const baselineFile = fs.readFileSync(options.baseline).toString();
baseline = flatten(JSON.parse(baselineFile)); baseline = flatten(JSON.parse(baselineFile));
} catch (e) { } catch (e) {
console.log('Error reading file "' + options.baseline + '": ' + e); console.log('Error reading file "' + options.baseline + '": ' + e);
process.exit(0); process.exit(0);
} }
try { try {
var currentFile = fs.readFileSync(options.current).toString(); const currentFile = fs.readFileSync(options.current).toString();
current = flatten(JSON.parse(currentFile)); current = flatten(JSON.parse(currentFile));
} catch (e) { } catch (e) {
console.log('Error reading file "' + options.current + '": ' + e); console.log('Error reading file "' + options.current + '": ' + e);
@ -210,5 +200,5 @@ function main() {
stat(baseline, current); stat(baseline, current);
} }
var options = parseOptions(); const options = parseOptions();
main(); main();