跳到正文
cccskills
免费在 GitHub 公开中文介绍

py-code-insight-counters

在测试中用 PyCodeInsightCounters 统计 Python 类型推断引擎的工作量,并通过 PyPerfProbe 比较操作次数、内存分配、AST 加载和耗时,以评估性能变化或回归。

原文查看原文说明

Count Python code insight work in tests with PyCodeInsightCounters.

JetBrains/intellij-community星标 20,684(GitHub 20,684 + cccskills 0)

实测评分 未实测

查看安装方法

适用场景

  • 在测试中检查类型推断次数
  • 用测试固定性能回归
  • 比较不同输入规模的工作量
  • 测量代码高亮场景

中文介绍

功能

本技能介绍如何在测试中使用 PyCodeInsightCounters 统计 Python 类型推断引擎执行的工作。计数器涵盖类型获取、缓存、类型匹配和控制流构建,可用于比较修改前后的工作量,或观察工作量如何随输入规模变化。使用 PyPerfProbe 时,还可以测量不同场景的耗时、内存分配和 AST 加载,并以 PYPERF 行报告。技能也说明如何通过稳定的操作计数编写性能回归测试,避免只依赖可能波动的运行时间。

适用场景

当您想确认类型引擎的修改是否增加了不必要的工作,或想把性能问题固定为回归测试时,可以使用本技能。它也适用于比较代码量变化后的工作量增长,以及测量代码高亮或文件编辑后的处理场景。

使用示例

  • “请在测试中检查这段类型获取逻辑的评估次数。”
  • “请比较不同输入规模下类型引擎的工作量。”
  • “请用 PyPerfProbe 测量这个代码高亮场景。”

注意事项

计数器默认处于关闭状态。countOnCurrentThread 只统计调用线程;对所有线程做快照时,差值也会包含 IDE 的后台工作。性能测试在普通测试运行中会被跳过。

本介绍由 AI(OpenAI Codex)根据公开的 SKILL.md 撰写。准确的规格请查看下方原文。

文档检查

AI 根据 SKILL.md 的内容检查了需要注意的操作和使用前提。评分只依据实际运行的结果(实测对比)。

安全性发送数据、删除文件、执行命令等是否有说明
A良好资料说明常规运行会跳过性能测试,跨线程差值含后台工作。
安装难易度所需工具和 API 密钥是否少且有说明
B部分不足资料依赖项目测试环境,但没有完整说明环境安装步骤。

不计入评分。 由 AI(OpenAI Codex)于 2026年10月28日 评估。仅供参考,安装前请同时查看原文。

如果您认为评估有误,请申请复核

请写明哪一项有误以及原因。无需登录。我们会对照 SKILL.md 重新评估,并在此处更新结果。

此表单受 reCAPTCHA 保护,适用 Google 的隐私权政策和服务条款。

包含的文件(1)

  • SKILL.md4.9 KB

SKILL.md(原文)

安装前可以查看交给 AI 代理的指令内容。

<!-- Generated by community/.ai/render-guides.mjs; edit community/.agents/skills/py-code-insight-counters/SKILL.md -->

Python code insight counters

PyCodeInsightCounters (python-psi-impl, package com.jetbrains.python.codeInsight) counts the work of the native Python type engine. PyPerfProbe (community/python/testFramework, package com.intellij.python.community.testFramework.performance) measures a scenario with these counters, the wall time, the allocations and the AST loads.

Use them to show that a change does less work, to show how the work grows with the input, or to pin a performance defect in a test. A work count does not flake like a time.

Counters

CounterCountsHook
GET_TYPE_CALLSeach getType callTypeEvalContextImpl.getType
GET_TYPE_CACHE_HITSthe calls that the context cache answersTypeEvalContextImpl.getType
GET_TYPE_EVALUATIONSthe calls that evaluate a typeTypeEvalContextImpl.getType
CONTEXTS_CONSTRUCTEDeach type context, also the library and assumption contextsTypeEvalContextImpl constructor
CONTEXT_LOOKUP_MISSESthe context lookups that store a new contextTypeEvalContextCacheImpl
ASSUME_TYPE_CALLSthe narrowing assumptions that create a contextTypeEvalContextImpl.assumeType
MATCH_STEPSthe steps of the type matchPyTypeChecker.match
OVERLOAD_CANDIDATES_CHECKEDthe overload candidates that the argument types checkPyCallExpressionHelper.matchesByArgumentTypes
CFG_BUILDS, CFG_INSTRUCTIONSthe control flow builds and their instructionsPyControlFlowBuilder.buildControlFlow

A cache hit and an evaluation do not add up to the calls. The rest are library delegations and the requests that the recursion guard stops.

Count in a test

The counting is off by default. For the work of the calling thread only:

val counts = PyCodeInsightCounters.countOnCurrentThread { context.getType(expression) }
assertEquals(1L, counts.getValue(Counter.GET_TYPE_EVALUATIONS))

The highlighting passes run on other threads, so countOnCurrentThread does not see their work. For all threads, take two snapshots:

PyCodeInsightCounters.enable(testRootDisposable)
val before = PyCodeInsightCounters.snapshot()
myFixture.doHighlighting()
val delta = PyCodeInsightCounters.snapshot() - before
assertTrue(delta[Counter.CFG_BUILDS] >= 1)

The delta also contains the background work of the IDE in that time. Assert an exact count only for the calling thread. Otherwise assert a bound or a ratio.

Measure a scenario

  1. Implement PyPerfProbe.Editor over the test fixture. FixtureEditor in PyNativeEngineCountersPerformanceTest is an example.
  2. Create the probe with a disposable. The probe turns the counting on, turns off the RecursionManager test checks and sets the registry keys of PyPerfProbe.PINNED_REGISTRY to their IDE values.
  3. Call one of these:
    • measure(scenario) { action } runs 3 warm-up attempts and 10 attempts. Before each attempt, setup runs. The default setup is cold().
    • measureEditorFile(name, inspections) measures a cold highlighting, a highlighting after a one-character edit, and one inferAll pass.
    • measureScaled(scenario) { action } uses fewer attempts when the first run takes more than 10 seconds.
  4. Call report(result). It prints PYPERF lines: the time quantiles, the allocations, the AST loads of the first attempt, and each counter with its median, its range and the value of the first attempt.

Mark the test class with @PerformanceUnitTest. The usual test runs skip it. tests.cmd runs it when you give its fully qualified name.

The system property pyperf.out names a file that also gets the PYPERF lines. The system property pyperf.astload.stacks=true prints the stack of the first three AST loads of each file.

Read the result

  • cold() drops the PSI caches and the type contexts. The PSI, the stubs, the control flow and the soft caches stay. So the counts of one scenario change between attempts. For a cold highlighting of pandas_examples.py, the range of type.getType.evaluations is about 20 % of the median.
  • Compare medians, and read the range= value before you claim a difference.
  • For the complexity of an algorithm, measure the input at several sizes (n, 2n, 4n) and compare the growth of a counter. Do not compare single times.

Add a temporary counter

  1. Add an entry to PyCodeInsightCounters.Counter with a stable dotted id, for example type.foo.calls.
  2. At the hook, call PyCodeInsightCounters.inc(Counter.FOO_CALLS) or add(counter, value). When the counting is off, a call costs one volatile read. Put other work at the hook behind PyCodeInsightCounters.isEnabled.
  3. Keep a counter in the merge request only when a test asserts on it. Remove the others before the merge.

评价

还没有评价。欢迎分享你的使用感受。

评论

无需登录即可发表。欢迎分享使用感受、提问,或说明在哪些环境下无法运行。

还没有评论。来写第一条吧。

提交后会立即公开。请勿填写邮箱、电话等个人信息。URL 最多 2 个。

0 / 1000 字

此表单受 reCAPTCHA 保护,适用 Google 的隐私权政策和服务条款。

同一仓库的技能

actions

免费有简介

实现或修改 IntelliJ `AnAction`,并在 `plugin.xml` 中注册。将显示名称和说明写入消息资源文件,按需设置图标,并通过事件协程作用域启动挂起任务,避免同步处理阻塞 IDE。

  • 为 IDE 菜单添加操作
  • 修改操作注册或图标
  • 整理操作名称和说明
JetBrains/intellij-community2.1万2026年10月29日 更新

bazel-test-migration

免费有简介

将 IntelliJ JPS 测试模块迁移到 Bazel,并排查仅在 Bazel 中出现的失败。根据症状检查依赖、插件、测试数据和 runfiles,重新生成并验证目标。

  • 将 JPS 测试模块迁移到 Bazel
  • 排查仅在 Bazel 中失败的测试
  • 检查测试依赖和数据配置
JetBrains/intellij-community2.1万2026年10月29日 更新

code-style

免费有简介

按 IntelliJ 项目规范编写或审查 Kotlin 和 Java 代码,涵盖语言选择、格式、密封类型、线程注解及控制流异常处理。

  • 按 IntelliJ 规范编写 Kotlin
  • 审查 Java 代码
  • 检查异常处理和密封类型分支
JetBrains/intellij-community2.1万2026年10月29日 更新

commits

免费有简介

本指南帮助您按 IntelliJ 项目规则拆分更改并撰写提交信息:有工单时使用编号和变更说明,无工单时使用标签与子系统名,并在需要时说明原因和决定。

  • 按项目格式编写带工单号的提交信息
  • 整理测试或文档提交并选择合适标签
  • 按规则修改已有提交信息
JetBrains/intellij-community2.1万2026年10月29日 更新

使用多个 Python 类型检查器比较代码片段或文件的类型推断和报错判断,并汇总各工具的结论、耗时与诊断信息,可保存为 Markdown 报告。

  • 比较不同检查器的类型推断
  • 确认代码是否会被判为类型错误
  • 查看多个工具的诊断差异
JetBrains/intellij-community2.1万2026年10月29日 更新

整理 Compose Desktop 界面中的状态和副作用,让可组合函数负责绘制,由展示器或协调器处理应用决策。了解状态归属、Effect、可复用组件接口、延迟列表键、动画和性能,适用于单项界面修改。

  • 分离界面绘制与展示器决策
  • 整理界面状态的归属
  • 检查 Effect 键和协程用法
JetBrains/intellij-community2.1万2026年10月29日 更新

查看 JetBrains 的全部技能

报告此技能的问题