统计数据

虽然可以通过设置 TIMING 环境变量来对 ESLint 运行的总体规则性能进行分析,但有时获取更精细的计时数据(每个规则每个文件的 lint 时间)或收集其他感兴趣的度量可能很有用。特别是在开发新的 自定义插件 以及评估/基准测试新语言或规则集时。对于这些用例,你可以选择从 ESLint 收集运行时统计信息。

¥While an analysis of the overall rule performance for an ESLint run can be carried out by setting the TIMING environment variable, it can sometimes be useful to acquire more granular timing data (lint time per file per rule) or collect other measures of interest. In particular, when developing new custom plugins and evaluating/benchmarking new languages or rule sets. For these use cases, you can optionally collect runtime statistics from ESLint.

启用统计信息收集

¥Enable stats collection

要启用统计信息收集,你可以:

¥To enable collection of statistics, you can either:

  1. 使用 --stats CLI 选项。这会将统计数据传递到用于从 ESLint 输出结果的格式化程序中。(注意:并非所有格式化程序都会输出统计数据。)

    ¥Use the --stats CLI option. This will pass the stats data into the formatter used to output results from ESLint. (Note: not all formatters output stats data.)

  2. stats: true 设置为 ESLint 构造函数上的选项。

    ¥Set stats: true as an option on the ESLint constructor.

启用统计数据会向每个 LintResult 对象添加一个新的 stats 键,其中包含每个规则的解析时间、修复时间、lint 时间等数据。

¥Enabling stats data adds a new stats key to each LintResult object containing data such as parse times, fix times, lint times per rule.

因此,它无法通过 stdout 获得,但可以通过使用 CLI 的格式化程序或通过 Node.js API 轻松获取,以满足你的特定需求。

¥As such, it is not available via stdout but made easily ingestible via a formatter using the CLI or via the Node.js API to cater to your specific needs.

◆ 统计类型

¥◆ Stats type

Stats 值是每次 lint 运行的计时信息。LintResult 类型的 stats 属性包含它。它具有以下属性:

¥The Stats value is the timing information of each lint run. The stats property of the LintResult type contains it. It has the following properties:

  • fixPasses (number)
    ESLint 在 linting 后应用至少一个修复的次数。

    ¥fixPasses (number)
    The number of times ESLint has applied at least one fix after linting.

  • times ({ passes: TimePass[] })
    在(解析、修复、检查)文件上花费的时间,其中检查是指每个规则的计时信息。

    ¥times ({ passes: TimePass[] })
    The times spent on (parsing, fixing, linting) a file, where the linting refers to the timing information for each rule.

    • TimePass ({ parse: ParseTime, rules?: Record<string, RuleTime>, fix: FixTime, total: number })
      包含花费在(解析、修复、linting)上的时间的对象

      ¥TimePass ({ parse: ParseTime, rules?: Record<string, RuleTime>, fix: FixTime, total: number })
      An object containing the times spent on (parsing, fixing, linting)

      • ParseTime ({ total: number })
        解析文件所花费的总时间。

        ¥ParseTime ({ total: number })
        The total time that is spent when parsing a file.

      • RuleTime ({ total: number }) 规则所花费的总时间。

        ¥RuleTime ({ total: number }) The total time that is spent on a rule.

      • FixTime ({ total: number }) 对代码应用修复所花费的总时间。

        ¥FixTime ({ total: number }) The total time that is spent on applying fixes to the code.

CLI 用法

¥CLI usage

让我们考虑以下示例:

¥Let’s consider the following example:

/*eslint no-regex-spaces: "error", wrap-regex: "error"*/

function a() {
    return / foo/.test("bar");
}

使用 --stats 运行 ESLint 并通过内置 json 格式化程序 输出到 JSON:

¥Run ESLint with --stats and output to JSON via the built-in json formatter:

npx eslint file-to-fix.js --fix --stats -f json

这将产生以下 stats 条目作为格式化 lint 结果对象的一部分:

¥This yields the following stats entry as part of the formatted lint results object:

{
    "times": {
        "passes": [
            {
                "parse": {
                    "total": 3.975959
                },
                "rules": {
                    "no-regex-spaces": {
                        "total": 0.160792
                    },
                    "wrap-regex": {
                        "total": 0.422626
                    }
                },
                "fix": {
                    "total": 0.080208
                },
                "total": 12.765959
            },
            {
                "parse": {
                    "total": 0.623542
                },
                "rules": {
                    "no-regex-spaces": {
                        "total": 0.043084
                    },
                    "wrap-regex": {
                        "total": 0.007959
                    }
                },
                "fix": {
                    "total": 0
                },
                "total": 1.148875
            }
        ]
    },
    "fixPasses": 1
}

请注意,对于上面的简单示例,所有规则时间的总和应直接与 TIMING 输出的第一列进行比较。使用 TIMING=all 运行相同的命令,你可以验证这一点:

¥Note, that for the simple example above, the sum of all rule times should be directly comparable to the first column of the TIMING output. Running the same command with TIMING=all, you can verify this:

$ TIMING=all npx eslint file-to-fix.js --fix --stats -f json
...
Rule            | Time (ms) | Relative
:---------------|----------:|--------:
wrap-regex      |     0.431 |    67.9%
no-regex-spaces |     0.204 |    32.1%

API 用法

¥API Usage

你可以使用 Node.js API 通过将 stats: true 作为选项传递给 ESLint 构造函数来实现相同的效果。例如:

¥You can achieve the same thing using the Node.js API by passingstats: true as an option to the ESLint constructor. For example:

const { ESLint } = require("eslint");

(async function main() {
    // 1. Create an instance.
    const eslint = new ESLint({ stats: true, fix: true });

    // 2. Lint files.
    const results = await eslint.lintFiles(["file-to-fix.js"]);

    // 3. Format the results.
    const formatter = await eslint.loadFormatter("json");
    const resultText = formatter.format(results);

    // 4. Output it.
    console.log(resultText);
})().catch((error) => {
    process.exitCode = 1;
    console.error(error);
});
ESLint 中文网
粤ICP备13048890号