Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New: Configurable List Size For Per-Rule Performance Metrics #13812

Merged
merged 1 commit into from Nov 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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") {
mdjermanovic marked this conversation as resolved.
Show resolved Hide resolved
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";
mdjermanovic marked this conversation as resolved.
Show resolved Hide resolved
assert.strictEqual(getListSize(), Number.POSITIVE_INFINITY);

process.env.TIMING = "ALL";
assert.strictEqual(getListSize(), Number.POSITIVE_INFINITY);
});
});
});