---
name: jetbrains-py-code-insight-counters
description: "Count Python code insight work in tests with PyCodeInsightCounters."
license: "NOASSERTION"
metadata:
  decimalai-skill-id: "ca4938b4-0c5e-4d9d-b560-2bf98a5c933c"
  decimalai-version-id: "ca56fb68-11e1-4124-84ba-f45c4c4fea3b"
  decimalai-version: "8"
  decimalai-body-sha256: "20b7d09e0975ed107ab13f0dfb879de3222a6b85ca14dce634895549a04e90df"
  decimalai-name: "jetbrains/py-code-insight-counters"
  decimalai-format: "1"
  decimalai-invocation: "model"
  decimalai-category: "testing"
  decimalai-source: "https://app.decimal.ai/s/jetbrains-py-code-insight-counters@8/SKILL.md"
---
<!-- 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:

```kotlin
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:

```kotlin
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.
2. `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.
3. Check the growth of a counter with `assertGrowth(scenario, sizes, PyGrowth.LINEAR) { size -> ... }`, or a fact
   with `assertExpectation`. Use small sizes (n, 2n, 4n).
4. 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:
   - `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.
2. `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.
3. 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

- `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.

## 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`.
3. Keep a counter in the merge request only when a test asserts on it.
