Skip to content

feat(profiler): receive the stack samples of phpspy and reli-prof, and collapsed stacks - #396

Merged
roxblnfk merged 1 commit into
masterfrom
feat/phpspy-traces
Nov 26, 2026
Merged

roxblnfk merged 1 commit into
masterfrom
feat/phpspy-traces

Conversation

@roxblnfk

@roxblnfk roxblnfk commented Nov 26, 2026 •

Copy link
Copy Markdown
Member

🔍 What was changed

  • POST /api/profiler/phpspy takes the text traces of phpspy and reli-prof (its phpspy and phpspy_with_opcode templates); POST /api/profiler/collapsed takes collapsed stacks frame;frame;frame count. Both take gzip, and the query metadata of the other profile endpoints; collapsed stacks also take clock.
  • ct counts the samples a function is on the stack in; with period (seconds) the profile gets the wall time too: neither tool writes its sampling rate into the trace.
  • The MCP format of the new profiles is Phpspy and Collapsed.
  • The gzip decompression of pprof moved to a shared Gzip helper.

How it works

  • phpspy writes a sample from the leaf, and splits one longer than its output buffer (-b, 4096 bytes by default: most framework stacks) into chunks closed by # trace_id = <trace>.<chunk>~|!, with other processes' samples in between; in single-line mode (-1) these trailers and all metadata sit on the line of the frames, as every record ends with the frame delimiter. Chunks are put together by trace id; a sample with a lost chunk, or cut off at the end of the trace, is dropped, as phpspy's own stackcollapse-phpspy.pl does.
  • <main> is file-scope code and becomes main(); same-named frames of different files (closures before PHP 8.4, the <main> of included files) are told apart by the file.
  • Directory watching is not part of it: a phpspy output file grows for as long as phpspy runs.

Checklist

  • Closes Receive phpspy and reli-prof sampling traces #395
  • How was this tested:
    • Unit tests added
    • Tested manually: a trace POSTed to a running Trap shows in the console and the UI API; a broken line gets a 400 naming it
    • Against real phpspy 0.8.0 output (PHP 8.5): the default, single-line and chunked traces in tests/Stub/phpspy give the same call tree as phpspy's own stackcollapse-phpspy.pl
    • Against real reli-prof 0.12.0 output (Docker, PHP 8.3): its phpspy and phpspy_with_opcode templates, --trace-var and --depth in tests/Stub/reli give the same call tree as stackcollapse-phpspy.pl

Documentation

docs/profiles.md (endpoints and a phpspy and reli-prof section), docs/mcp.md, skills/trap/references/profiling.md.

Review notes

  • A body within the size limits could take the whole server down, or hold the build lock for minutes: a single-line trace is walked record by record, not exploded, and a trace is limited to 10 000 frames a sample, 500 000 samples, 2 000 000 frames in all, 4 000 000 lines and 100 000 frames waiting in unfinished chunks; past them the request gets 400.
  • A kept profile takes about 2 KB a function, which NodeBudget doesn't count: the function limit is half the node budget, up to 50 000. Replayed at memory_limit=512M, repeated 45 000-function bodies are built until about 320 MB is in use, then rejected with 400. Below the floor of NodeBudget (5 000 functions here) many kept profiles still exhaust the memory, as of every format: the event store keeps them by count.
  • The mode (default or -1) is told by the first frame, not per line: in the default mode a metadata record of a peeked value (-D, -e) may end with a tab too.
  • profile_summary takes its format enum from the cases of the profile Type enum, so its output schema follows new formats.
  • Collapsed stacks take clock, and a .php path root as main(): trap()::profile()->toServer(..., Format::Collapsed) of Excimer sends both.

@roxblnfk
roxblnfk requested a review from a team November 26, 2026 10:53
@github-actions github-actions Bot added enhancement New feature or request tests Changes in the tests labels Nov 26, 2026
@codecov

codecov Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 95.46926% with 14 lines in your changes missing coverage. Please review.
✅ Project coverage is 82.40%. Comparing base (854c735) to head (bdeae55).

Files with missing lines Patch % Lines
src/Server/Command/Test.php 0.00% 12 Missing ⚠️
src/Module/Profiler/Gzip.php 93.75% 1 Missing ⚠️
src/Module/Profiler/Module.php 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #396      +/-   ##
==========================================
+ Coverage   82.20%   82.40%   +0.20%     
==========================================
  Files         485      489       +4     
  Lines       20431    20739     +308     
==========================================
+ Hits        16796    17091     +295     
- Misses       3635     3648      +13     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@roxblnfk
roxblnfk force-pushed the feat/phpspy-traces branch 8 times, most recently from c887130 to af8ba74 Compare November 26, 2026 13:38
…d collapsed stacks

phpspy and reli-prof sample a running PHP process from outside, with no extension or code change; their text traces went nowhere in Trap but through reli's converters. The sampling rate is in neither trace, so the wall time comes only with the `period` query parameter.

Assisted-By: Claude Opus 5.5 <noreply@anthropic.com>
@roxblnfk
roxblnfk merged commit c18e0a3 into master Nov 26, 2026
31 checks passed
@roxblnfk
roxblnfk deleted the feat/phpspy-traces branch November 26, 2026 15:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request tests Changes in the tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Receive phpspy and reli-prof sampling traces

1 participant