Py code insight counters

From JetBrains/intellij-community · View interactive version

Py code insight counters is an AI agent skill from the GitHub repository JetBrains/intellij-community. Count Python code insight work in tests with PyCodeInsightCounters.

How to use this skill

Skill file

<!-- 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 Python

code insight. 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.

Counters

| Counter | Counts | Hook |

| --- | --- | --- |

| GET_TYPE_CALLS | each getType call | TypeEvalContextImpl.getType |

| GET_TYPE_CACHE_HITS | the calls that the context cache answers | TypeEvalContextImpl.getType |

| GET_TYPE_EVALUATIONS | the calls that evaluate a type | TypeEvalContextImpl.getType |

| CONTEXTS_CONSTRUCTED | each type context, also the library and assumption contexts | TypeEvalContextImpl constructor |

| CONTEXT_LOOKUP_MISSES | the context lookups that store a new context | TypeEvalContextCacheImpl |

| ASSUME_TYPE_CALLS | the narrowing assumptions that create a context | TypeEvalContextImpl.assumeType |

| MATCH_STEPS | the steps of the type match | PyTypeChecker.match |

| TYPE_TRAVERSAL_VISITS | the types that a recursive type walk visits | PyRecursiveTypeVisitor.visitPyType |

| OVERLOAD_CANDIDATES_CHECKED | the overload candidates that the argument types check | PyCallExpressionHelper.matchesByArgumentTypes |

| CFG_BUILDS, CFG_INSTRUCTIONS | the control flow builds and their instructions | PyControlFlowBuilder.buildControlFlow |

| SCOPE_GLOBALS_VISITS | the scopes that the index of nested global names visits | ScopeImpl.collectNestedGlobals |

| COMPLETION_LOOKUP_ITEMS | the lookup items of the Python completion | CompletionVariantsProcessor.setupItem |

| PATH_CACHE_CLEARS | the clears of a non-empty resolve path cache | PythonPathCache.clearCache |

| DJANGO_TAG_LIBRARY_SCANS | the scans of a Django tag library for its registered tags and filters | DjangoTagLibrary.getRegistrations |

| DJANGO_TEMPLATE_LOAD_SCANS | the scans of a Django template for the tags and filters that it loads | DjangoTagLibrary.getLoadedTags |

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. testDisposable is a @TestDisposable field of the test class:

PyCodeInsightCounters.enable(testDisposable)
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.

Gate a scenario

A gate is a normal test that fails on a performance regression. It runs on each Safe Push, so it must be fast and

must not depend on the machine.

  1. In a PyCodeInsightTestCase, register the probe extension:

@JvmField @RegisterExtension val perf = PyPerfProbeExtension { myFixture }. It gives each test its own probe.

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.

  1. perf.probe.work { action } runs the action once after cold(). It returns the counters of the calling thread, the

counters of all threads and the AST loads.

  1. Check the growth of a counter with assertGrowth(scenario, sizes, PyGrowth.LINEAR) { size -> ... }, or a fact

with assertExpectation. Use small sizes (n, 2n, 4n).

  1. When an open issue makes the expectation fail, give PyKnownDefect(issue, growth). The gate then fails when the

work is worse than the defect, and also when the expectation holds.

PyCodeInsightScalingPerformanceTest and PyEditorFeaturesPerformanceTest are examples.

Benchmark a scenario

A benchmark is a @PerformanceUnitTest method. The usual test runs skip it, and the performance test builds of

TeamCity run it. tests.cmd runs it when you give its fully qualified name.

  1. Call one of these:

The default setup is cold().

and one inferAll pass.

  1. measure runs the attempts as the IJ Perf Benchmark subtest scenario of the calling test method. Call it from

the test method, not from another thread. The benchmark publishes the time and the median counters, allocations and

AST loads (counter.<id>, alloc.*.kb, astLoads.firstAttempt.*). The IJ Perf degradation detector watches the

time of each benchmark on master.

  1. Call report(result) after measure or measureScaled. It prints the result as PYPERF lines.

measureEditorFile calls report itself.

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

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.

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). Put other work at the

hook behind PyCodeInsightCounters.isEnabled.

  1. Keep a counter in the merge request only when a test asserts on it.
Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.