From 254e00fea8745ff5a8bcc8cb874fcfd02996d81b Mon Sep 17 00:00:00 2001 From: Bryan Mishkin <698306+bmish@users.noreply.github.com> Date: Fri, 6 Nov 2020 20:45:48 -0500 Subject: [PATCH] New: Configurable List Size For Per-Rule Performance Metrics (#13812) Implements RFC: https://github.com/eslint/rfcs/tree/master/designs/2020-timing-list-size --- docs/developer-guide/working-with-rules.md | 2 + lib/linter/timing.js | 25 ++++++++++- tests/lib/linter/timing.js | 51 ++++++++++++++++++++++ 3 files changed, 76 insertions(+), 2 deletions(-) create mode 100644 tests/lib/linter/timing.js diff --git a/docs/developer-guide/working-with-rules.md b/docs/developer-guide/working-with-rules.md index 89d37c483a4..857a7314e55 100644 --- a/docs/developer-guide/working-with-rules.md +++ b/docs/developer-guide/working-with-rules.md @@ -720,6 +720,8 @@ Rule | Time (ms) | Relative quotes | 18.066 | 100.0% ``` +To see a longer list of results (more than 10), set the environment variable to another value such as `TIMING=50` or `TIMING=all`. + ## Rule Naming Conventions The rule naming conventions for ESLint are fairly simple: diff --git a/lib/linter/timing.js b/lib/linter/timing.js index 8396d9215b5..58230306855 100644 --- a/lib/linter/timing.js +++ b/lib/linter/timing.js @@ -44,6 +44,26 @@ const enabled = !!process.env.TIMING; const HEADERS = ["Rule", "Time (ms)", "Relative"]; const ALIGN = [alignLeft, alignRight, alignRight]; +/** + * Decide how many rules to show in the output list. + * @returns {number} the number of rules to show + */ +function getListSize() { + const MINIMUM_SIZE = 10; + + if (typeof process.env.TIMING !== "string") { + return MINIMUM_SIZE; + } + + if (process.env.TIMING.toLowerCase() === "all") { + return Number.POSITIVE_INFINITY; + } + + const TIMING_ENV_VAR_AS_INTEGER = Number.parseInt(process.env.TIMING, 10); + + return TIMING_ENV_VAR_AS_INTEGER > 10 ? TIMING_ENV_VAR_AS_INTEGER : MINIMUM_SIZE; +} + /* istanbul ignore next */ /** * display the data @@ -61,7 +81,7 @@ function display(data) { return [key, time]; }) .sort((a, b) => b[1] - a[1]) - .slice(0, 10); + .slice(0, getListSize()); rows.forEach(row => { row.push(`${(row[1] * 100 / total).toFixed(1)}%`); @@ -133,7 +153,8 @@ module.exports = (function() { return { time, - enabled + enabled, + getListSize }; }()); diff --git a/tests/lib/linter/timing.js b/tests/lib/linter/timing.js new file mode 100644 index 00000000000..e1052348a03 --- /dev/null +++ b/tests/lib/linter/timing.js @@ -0,0 +1,51 @@ +"use strict"; + +const { getListSize } = require("../../../lib/linter/timing"); +const assert = require("chai").assert; + +describe("timing", () => { + describe("getListSize()", () => { + after(() => { + delete process.env.TIMING; + }); + + it("returns minimum list size with small environment variable value", () => { + delete process.env.TIMING; // With no value. + assert.strictEqual(getListSize(), 10); + + process.env.TIMING = "true"; + assert.strictEqual(getListSize(), 10); + + process.env.TIMING = "foo"; + assert.strictEqual(getListSize(), 10); + + process.env.TIMING = "0"; + assert.strictEqual(getListSize(), 10); + + process.env.TIMING = "1"; + assert.strictEqual(getListSize(), 10); + + process.env.TIMING = "5"; + assert.strictEqual(getListSize(), 10); + + process.env.TIMING = "10"; + assert.strictEqual(getListSize(), 10); + }); + + it("returns longer list size with larger environment variable value", () => { + process.env.TIMING = "11"; + assert.strictEqual(getListSize(), 11); + + process.env.TIMING = "100"; + assert.strictEqual(getListSize(), 100); + }); + + it("returns maximum list size with environment variable value of 'all'", () => { + process.env.TIMING = "all"; + assert.strictEqual(getListSize(), Number.POSITIVE_INFINITY); + + process.env.TIMING = "ALL"; + assert.strictEqual(getListSize(), Number.POSITIVE_INFINITY); + }); + }); +});