统计数据
虽然可以通过设置 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:
- 使用
--statsCLI 选项。这将把统计数据传递到用于输出 ESLint 结果的格式化程序中。(注意:并非所有格式化程序都会输出统计数据。) - 将
stats: true设置为ESLint构造函数上的一个选项。
启用统计数据会在每个 LintResult 对象中添加一个新的 stats 键,其中包含解析时间、修复时间、每条规则的检查时间等数据。
🌐 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 在进行代码检查后至少应用一次修复的次数。times({ passes: TimePass[] })
处理一个文件所花费的时间(解析、修复、代码检查),其中代码检查指每条规则的时间信息。TimePass({ parse: ParseTime, rules?: Record<string, RuleTime>, fix: FixTime, total: number })
一个包含在(解析、修复、代码检查)上花费时间的对象ParseTime({ total: number })
解析文件时所花费的总时间。RuleTime({ total: number })
用于一条规则的总时间。FixTime({ total: number })
用于对代码进行修复的总时间。
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:
npm
npx eslint file-to-fix.js --fix --stats -f json
yarn
yarn dlx eslint file-to-fix.js --fix --stats -f json
pnpm
pnpm dlx eslint file-to-fix.js --fix --stats -f json
bun
bunx 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
你可以通过将 stats: true 作为选项传递给 ESLint 构造函数,使用 Node.js API 实现同样的功能。例如:
🌐 You can achieve the same thing using the Node.js API by passing stats: 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);
});