Skip to content

Commit

Permalink
New: Configurable List Size For Per-Rule Performance Metrics (#13812)
Browse files Browse the repository at this point in the history
  • Loading branch information
bmish committed Nov 7, 2020
1 parent 6c3c710 commit 254e00f
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 2 deletions.
2 changes: 2 additions & 0 deletions docs/developer-guide/working-with-rules.md
Expand Up @@ -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:
Expand Down
25 changes: 23 additions & 2 deletions lib/linter/timing.js
Expand Up @@ -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
Expand All @@ -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)}%`);
Expand Down Expand Up @@ -133,7 +153,8 @@ module.exports = (function() {

return {
time,
enabled
enabled,
getListSize
};

}());
51 changes: 51 additions & 0 deletions 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);
});
});
});

0 comments on commit 254e00f

Please sign in to comment.