Skip to content

Add --debug-performance=PATH performance summary and dump - #14737

Merged
alies-dev merged 31 commits into
vimeo:masterfrom
alies-dev:feat/debug-performance
Nov 23, 2026
Merged

alies-dev merged 31 commits into
vimeo:masterfrom
alies-dev:feat/debug-performance

Conversation

@alies-dev

@alies-dev alies-dev commented Nov 23, 2026 •

Copy link
Copy Markdown
Collaborator

--debug-performance now prints where a run spent its time and memory: stages, workers, the slowest files, the hottest statements and function-likes, and the time spent in plugin and provider handlers. --debug-performance=PATH also writes all of it to a JSON file, which https://psalm.dev/perf/ shows as charts and tables in the browser.

WordPress wp-includes/html-api with #14653 reverted, psalm --threads=1 --debug-performance=perf.json:

Stages
  bootstrap             0.49 s wall    0.44 s cpu   1.1%
    preload                 0.44 s wall   1.0%
  scan.files            0.24 s wall    0.24 s cpu   0.6%
  analysis.files       43.23 s wall   43.23 s cpu  98.2%
  …

Peak memory 183.4 MB. Counts: 214 class-likes, 190 functions, 1385 methods, 35 file storages, 35 parses, 16 parser cache hits

Slowest files by CPU
    40.10 s cpu   40.10 s wall 128.0 MB peak growth  analysis src/wp-includes/html-api/class-wp-html-doctype-info.php
     1.95 s cpu    1.95 s wall   0.0 MB peak growth  analysis src/wp-includes/html-api/class-wp-html-processor.php
  …

Hottest statements (own time)
    40.07 s  src/wp-includes/html-api/class-wp-html-doctype-info.php:271 Stmt_If  2 passes
     0.55 s  src/wp-includes/html-api/class-wp-html-processor.php:4857 Stmt_If
  …

Hottest functions and methods (own time, and with the functions analysed meanwhile)
    20.28 s own    20.28 s total  WP_HTML_Doctype_Info::__construct (initializations)  src/wp-includes/html-api/class-wp-html-doctype-info.php:176
    19.81 s own    19.81 s total  WP_HTML_Doctype_Info::__construct (body)  src/wp-includes/html-api/class-wp-html-doctype-info.php:176
  …

and on stderr: Performance dump written to perf.json, open it in https://psalm.dev/perf/.

The JSON has the top-level keys schema, env, totals, stages, steps, plugins, pools (with their workers), files, hot, hooks, counts, parser and taint; docs/running_psalm/performance.md describes them. The old "Slow-to-analyze functions" block (ms per node) is replaced. Without the option nothing is collected.

--debug-performance takes an optional value. Without it, the run prints the
stages, the slowest files and the hottest statements and function-likes in
absolute time. With =PATH it also writes the measurements as one JSON
document for every output format.

The in-memory Perf\Collector is a static, dependency-free class that is
loaded before the autoloader. Forked workers hand their records to the main
process in the result of their Shutdown task. Off, each hook is one static
bool test and nothing is recorded, sent or written.

Removes the ms-per-node timing (function_timings, NodeCounterVisitor) that
the summary replaces.
Add a page with jq examples for the dump, a Chrome trace filter and the
privacy note, and link it from the command line usage page.
@alies-dev alies-dev added the release:feature The PR will be included in 'Features' section of the release notes label Nov 23, 2026
The fixture handler ignored its parameter, which self-analysis reports as
PossiblyUnusedParam.
@alies-dev alies-dev self-assigned this Nov 23, 2026
@alies-dev
alies-dev marked this pull request as ready for review November 23, 2026 18:50

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit 32bf4a4. Configure here.

Comment thread src/Psalm/Internal/Cli/Psalm.php Resolved
With an output format other than the console ones, the run printed no
statistics at all (vimeo#9789). Now the timing and memory line, the type inference
summary, the --stats output and the --debug-performance summary go to stderr
through the progress, as plain text, so stdout stays the report and
--no-progress keeps stderr empty. The memory peak the dump holds is still the
one printed.
@github-actions

github-actions Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

✅ Performance: unchanged · Types: unchanged

Project Memory (MB) Wall (s) Issues Weighted (types) Taint Dead code
✅ laravel 1 772 80.0 $\color{gray}{\textsf{+1.9\%}}$ 25 792 53 089 743 0
✅ symfony 3 619 162.9 $\color{gray}{\textsf{+0.3\%}}$ 51 774 100 636 1 582 0
✅ doctrine-orm 381 15.7 $\color{gray}{\textsf{+3.7\%}}$ 3 458 6 215 0 0
✅ psl 618 19.6 $\color{gray}{\textsf{-1.0\%}}$ 1 387 5 600 1 0
✅ phpunit 456 15.0 $\color{gray}{\textsf{-0.1\%}}$ 1 735 3 418 18 0
✅ php-parser 226 10.0 $\color{gray}{\textsf{-0.9\%}}$ 17 116 21 187 0 0
✅ tempest 898 29.1 $\color{gray}{\textsf{-2.3\%}}$ 5 094 10 556 275 0
✅ amphp-amp 48 1.5 $\color{gray}{\textsf{-0.7\%}}$ 29 49 0 0
✅ webmozarts-assert 277 $\color{gray}{\textsf{+1.6\%}}$ 4.7 $\color{gray}{\textsf{-0.6\%}}$ 575 2 714 0 0
✅ guzzle-promises 46 $\color{gray}{\textsf{+0.1\%}}$ 1.7 $\color{gray}{\textsf{+0.6\%}}$ 155 433 2 0
✅ monolog 136 5.7 $\color{gray}{\textsf{-0.5\%}}$ 918 2 491 11 0
✅ drupal 4 847 241.3 $\color{gray}{\textsf{-2.0\%}}$ 97 949 169 915 575 0
✅ wordpress 2 061 108.4 $\color{gray}{\textsf{-0.6\%}}$ 143 633 678 503 505 1
Total 15 386 695.5 $\color{gray}{\textsf{-0.6\%}}$ 349 615 1 054 806 3 712 1
How to read this table
  • Memory: Psalm's own peak figure, threads=1. More than +5 % fails, unless the PR changes composer.json.
  • Wall: base and head run on the same runner; ±10 % is noise. Above +10 % warns, never fails. re-run: a reversed second run was taken and the faster kept.
  • Issues: head count over all categories, then $\color{red}{\textsf{+added}}$ $\color{green}{\textsf{-removed}}$ against base.
  • Weighted (types): type issues weighted by error level (levels 1–8 as is, always-errors 10, strictness checks 1). Taint and dead-code issues are not weighed. The projects' own code is pinned and their dependencies resolve identically for base and head, so a change means Psalm reports more, fewer or differently serious issues. A rise is not necessarily a regression.
  • Taint: head count of Tainted* issues, then $\color{orange}{\textsf{+added}}$ $\color{orange}{\textsf{-removed}}$ against base. Both are orange as either direction needs review: more can be new detection or false positives, fewer can be fixed false positives or lost detection.
  • Dead code: head count of unused-code issues, shown only when a run reported some (findUnusedCode). More is red, fewer is green.
  • Total: sum over the projects compared on both sides. Its wall time warns above +5 % (±5 % is noise); the other thresholds apply per project.
  • Colors: $\color{red}{\textsf{red}}$ past a fail or warn threshold (or more issues), $\color{green}{\textsf{green}}$ an improvement past noise (or fewer issues), $\color{orange}{\textsf{orange}}$ needs review, $\color{gray}{\textsf{gray}}$ noise.
  • Performance: regressed if a head run crashed or timed out or a memory or wall delta is red, else improved if one is green, else unchanged. Its icon is the check result: ❌ fail · ⚠️ warn · ✅ pass; the icon next to each project is that project's result, 💥 = not comparable: base crashed or timed out (counts as ⚠️).
  • Types (heading): direction of the total weighted change, increased · decreased · unchanged. Taint and Dead code (heading): added and removed counts, present only when non-zero. Informational, never fails the check.

head e36455a · base 9def453 (merge-base) · workflow run

@alies-dev

Copy link
Copy Markdown
Collaborator Author

Overhead on the M1 (WordPress 6.8.3, taint on, --threads=1 --no-cache, CPU seconds, 3 replicates in rotated order, master ca21991e6b vs this branch at e8a3279c77; later commits don't touch the hot paths):

master option off --debug-performance=PATH
CPU, s (mean) 100.91 101.04 102.32
Δ +0.13 % +1.40 %
  • Off is within run-to-run spread (master's own replicates span 0.3 s; other rounds of the same comparison gave −0.06 % and +0.10 %). Issue fingerprints are identical in all runs.
  • On: about 0.8 % of the 1.4 % is the per-call timing of the two built-in taint handlers (AddTaints/RemoveTaints, ~3 M calls of ~0.25 µs each); without the EventDispatcher timing, on costs +0.50 %.
  • --threads=8 (WordPress, Symfony): CPU within noise; issue differences match the known nondeterminism of base at more than one thread (WordPress wp-includes/blocks, Symfony DeprecationErrorHandler.php:208), master vs master shows the same.
  • Report escaped values placed where their escaping doesn't protect them #14400 check: with Perf: fix exponential clause growth in || chains of str_starts_with() #14653's Algebra.php hunk reverted, wp-includes/html-api at --threads=1 names class-wp-html-doctype-info.php (40.1 s of 43.2 s), line 271 twice, and WP_HTML_Doctype_Info::__construct initializations 20.2 s and body 19.8 s.

@alies-dev alies-dev changed the title Add --debug-performance=PATH performance summary and dump Add --debug-performance=PATH performance summary and dump Nov 23, 2026
…handlers without per-call cost off

- Statement executions and function-like passes of 1 ms or more are
  aggregated per place with their number of passes; hot and top come from
  the aggregates.
- The dump is a Trace Event Format object (traceEvents), which Perfetto
  opens.
- Provider handlers are wrapped once at registration while collecting, and
  EventDispatcher keeps its original loops.
- origin is psalm only under Psalm\Internal; the startup collision check is
  a realpath of the directory plus the file name, with the write-time inode
  guard kept.
- The dump is written to PATH.tmp and renamed; a failed write warns on
  stderr even with --no-progress.
…th Psalm writes

The temporary file of the dump is made by tempnam() next to it and gets the
mode of the dump it replaces, so a user file named PATH.tmp is not touched.
The write-time guard checks the baseline of --set-baseline and every report
path, and the start-up check leaves a .. after a symlink to the file system.
Cut the tests to one end-to-end case per promise of the option and the unit
cases of the collector. The stage assertion pins the shape of the sequence:
the number of scan rounds depends on the PHP version, as the stubs are
preloaded below PHP 8.6.
- Write the dump directly to PATH; drop the temporary file, the mode copy and
  the whole path-collision guard (Psalm guards none of its other outputs).
- EventDispatcher times a callable that the analyser checks, through one
  Collector::timed(); the accounting of handler time is in one place.
- worker records are merged through recordStatement()/recordFunction().
- hot and top are one list of the 50 largest records per kind.
- traceEvents args hold only the path; the wall time of a file is the
  difference of its rounded endpoints.
- taint.resolver_tasks is resolver_rounds; the parser counts are not
  dropped by the counts of the storages.
- Keep only the tests of critical and simple parts.
A function-like pass's own time is its wall time minus the passes over other
function-likes that ran inside it (closures, parent constructors, methods
analysed on demand). hot.functions is ranked by it and the console shows it
before the total.
@alies-dev

Copy link
Copy Markdown
Collaborator Author

Overhead re-measured after the merge of master, at 2341759ff2 against its merge base 9def453996 (M1, WordPress 6.8.3, taint on, --threads=1 --no-cache, CPU seconds, 3 rotated replicates):

master option off --debug-performance=PATH
CPU, s (mean) 101.31 101.46 102.66
Δ +0.15 % +1.33 %

Issues are identical in all nine runs (strict_cmp: 0 only-base, 0 only-branch). On a Laravel project with the Laravel and PHPUnit plugins (IxDF, M4, one run each) the option costs about 6 %: its plugins hook into every expression (40 M handler calls), and each call is timed. The docs say so.

… dump

steps[] names the parts of a stage (config, preload, scan.stubs,
scan.project, taint.resolve, output, ...), the console shows those that take
1 % of the run under their stage, and the trace has lanes for stages, steps,
pools and, in each worker, when it worked and when it waited.
…t honestly

- restart is restart.check, and env.restarted says whether this is the process Psalm restarted.
- checkPaths() has the scan.setup steps of check().
- Steps that span a whole stage alone are gone; the scan rounds keep their names.
- The console leaves out statements and functions under 10 ms of their own time.
- One ranking helper, wrap() through timed(), and a unit test of the merge of worker data.
- Remove traceEvents (the viewer exports a trace), files[].mem_bytes, totals.children_max_rss_bytes and the parser counts from counts.
- php_bytes is php_end_bytes; workers report php_peak_growth_bytes; the payload size of a pool is a plain int, absent when not measured.
- Add pools[].cpu_us, parser {parses, cache_hits}, hot.min_us and hot.limit, a PluginInit hook per plugin and the phar name in the paths of files in other phars.
… of scanned files

- The scan pool is released before its CPU time is read, so the workers are reaped and their time is the pool's.
- scan.populate has the steps populate.classlikes, .methods, .files and .dependents, and taint has taint.prepare and taint.resolve.
- A scan record has the size of the file contents the scanner read.
- performance.md says what may change between releases.
@alies-dev

Copy link
Copy Markdown
Collaborator Author

Final overhead numbers at 33ba0cc40c against its merge base 9def453996 (M1, --no-cache, CPU seconds; later commits only touch the dump's shape and the console text):

WordPress 6.8.3, taint on, --threads=1, 3 replicates in rotated order:

master option off --debug-performance=PATH
CPU, s (mean) 101.45 101.27 102.79
Δ −0.17 % +1.33 %

Issues are identical in all nine runs (strict_cmp: nothing only on one side).

--threads=8, one run each:

master option off option on
WordPress CPU / wall, s 122.3 / 22.7 122.6 / 22.5 124.2 / 22.9
Symfony (no taint) CPU / wall, s 111.7 / 22.0 111.0 / 21.1 111.6 / 21.3

At 8 threads the issue lists differ between runs only where master differs between its own runs (WordPress wp-includes/blocks and sodium_compat, Symfony HttpClientTrait.php:225 and DeprecationErrorHandler.php:208).

- plugins[] holds the initialisation time of each plugin; env.plugins and the PluginInit hook rows are gone.
- Workers report peak_growth_bytes, scan records size_bytes, env has the pid.
- The summary prints the viewer link once, lists plugin initialisation on its own, and omits empty hot lists.
- A stubs step covers the visit of the configured stubs.
alies-dev added a commit to psalm/psalm.dev that referenced this pull request Nov 23, 2026
* feat: add the performance report viewer at /perf/

Static page that renders the JSON file written by `psalm --debug-performance=PATH` (vimeo/psalm#14737): timeline of stages, steps, pools and workers, hot code, plugins, files and memory. The file is read in the browser and never uploaded.

* fix: escape dump values and enforce no network access with a CSP
@alies-dev
alies-dev merged commit 96d0bda into vimeo:master Nov 23, 2026
71 checks passed
@alies-dev
alies-dev deleted the feat/debug-performance branch November 23, 2026 22:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release:feature The PR will be included in 'Features' section of the release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant