marjovanlier / xhprof-trace
A library for tracing XHProf performance data in PHP applications.
Requires
- php: ~8.4.0 || ~8.5.0 || ~8.6.0
- ext-xhprof: *
- symfony/console: ^v5|^v6|^v7|^v8
Requires (Dev)
- ext-ast: *
- ext-pcntl: *
- ext-xdebug: *
- carthage-software/mago: ^1.61
- infection/infection: ^0.35
- laravel/pint: ^1.14
- phan/phan: ^6.0.12 || v6.x-dev
- php-parallel-lint/php-console-highlighter: ^1
- php-parallel-lint/php-parallel-lint: ^1
- phpmd/phpmd: ^3
- phpstan/phpstan: ^2
- phpstan/phpstan-deprecation-rules: ^2
- phpstan/phpstan-phpunit: ^2
- phpstan/phpstan-strict-rules: ^2
- phpunit/phpunit: ^13
- psalm/plugin-phpunit: ^1
- rector/rector: ^2
- vimeo/psalm: ^7
Suggests
None
Provides
None
Conflicts
None
Replaces
None
- dev-main
- v2.0.3
- v2.0.2
- v2.0.1
- v2.0.0
- v1.0.30
- v1.0.29
- v1.0.28
- v1.0.27
- v1.0.26
- v1.0.25
- v1.0.24
- v1.0.23
- v1.0.22
- v1.0.21
- v1.0.20
- v1.0.19
- v1.0.18
- v1.0.17
- v1.0.16
- v1.0.15
- v1.0.14
- v1.0.13
- v1.0.12
- v1.0.11
- v1.0.10
- v1.0.9
- v1.0.8
- v1.0.7
- v1.0.6
- v1.0.5
- v1.0.4
- v1.0.3
- dev-Add-Tests
- dev-sweep/sweep_gha_fix_the_github_actions_run_fai_9dbcc
- dev-sweep/sweep_gha_fix_the_github_actions_run_fai
- dev-sweep/update_the_readme
- dev-sweep/the_calculaterank_function_in_the_tracep_71719
- dev-sweep/the_calculaterank_function_in_the_tracep_8d9fd
This package is auto-updated.
Last update: 2026-11-29 14:52:24 UTC
README
XhprofTrace is a PHP library that provides an interface for the XHProf profiling tool. It allows you to enable and disable profiling, and generate and display a report of the profiling data.
Requirements
- PHP 8.4, 8.5 or 8.6
- XHProf PHP extension
Installation
Before installing the XhprofTrace library, you need to ensure that the XHProf PHP extension is installed and enabled. If you are using Docker, you can add the following lines to your Dockerfile:
RUN pecl install xhprof && docker-php-ext-enable xhprof
The PHP 8.6 images ship without pecl; there,
install-php-extensions installs xhprof:
install-php-extensions xhprof.
composer require marjovanlier/xhprof-trace
Usage
Here is a basic example of how to use the library:
use MarjovanLier\XhprofTrace\Trace; $trace = new Trace(); // Enable XHProf profiling $trace->enable(); // Your application code here... // Disable XHProf profiling and save the profile $trace->disable(); // Display the profiling report in the console $trace->displayReport();
Documentation
Trace has the following methods:
new Trace(?string $profilesDir = null): Sets the folder for the profile files. The default isxhprof-trace/in the system temp directory (sys_get_temp_dir());''means the current folder. Versions up to v1.0.29 used/var/www/html/profiles/; pass that path to keep reading profiles saved there. On Linux other users share the temp directory, so the default folder is used only when it belongs to the user PHP runs as, is not a symlink, and no other user may read, write or enter it (mode 0700); otherwise a warning says so, and nothing is saved or read there. A folder you pass is not checked.enable(): Enables XHProf profiling.disable(): Disables XHProf profiling and saves the profiling data to a file in the profiles folder. It creates the folder when it does not exist, open only to the user PHP runs as; when the folder cannot be created, a warning names it and nothing is saved. It saves nothing when thisTracedid not enable profiling, and nothing a second time.displayReport(OutputInterface $output = new ConsoleOutput()): Displays a table of every profiled function, summed over all saved runs: the number of calls, the inclusive and exclusive wall time and the exclusive CPU time in milliseconds, the exclusive memory change in kilobytes, and the number of runs. "Exclusive" is the function's own share, without the functions it called. The memory change is negative when a function frees more memory than it takes. The function with the highest exclusive wall time is the last row. A recursive function keeps the names xhprof gives each depth (fib,fib@1,fib@2), so it has one row per depth. The table goes to the console; pass a SymfonyOutputInterface, such as a command's output or aBufferedOutput, to write it somewhere else.
The report adds up every profile file in the folder, also those from earlier sessions. Empty the folder to compare a new set of runs on its own. Files that hold no profile data are skipped.
Upgrading from version 1
Version 2 replaces the static methods with an object and needs PHP 8.4 or newer:
| Version 1 | Version 2 |
|---|---|
Trace::setProfilesDir($path) |
new Trace($path) |
Trace::enableXhprof() |
$trace->enable() |
Trace::disableXhprof() |
$trace->disable() |
Trace::displayReportCLI($output) |
$trace->displayReport($output) |
Call disable() on the same Trace whose enable() started the profile.
Profile files saved by version 1 are read as before.
License
This project is licensed under the MIT License. See the LICENSE file for details.