Introduction to php-spx: A Simple PHP Profiling Tool
What is php-spx?
php-spx, short for Simple Profiling eXtension, is a straightforward and efficient profiling tool designed for PHP developers. It distinguishes itself by being completely free and self-contained, meaning it operates solely within your infrastructure without sending any data to third-party services. This ensures data privacy and security.
Key Features
-
Ease of Use: php-spx is incredibly user-friendly. To profile PHP scripts, you only need to set an environment variable via the command line or check a radio button during a web request. There is no need for manual code instrumentation or special browser extensions.
-
Metric-rich: php-spx supports 22 different metrics, covering a wide range of performance data such as time, memory usage, included files, object usage, and I/O operations.
-
Context Preservation: Unlike some other profilers like Xhprof, php-spx maintains complete context. It keeps the entire call stack intact for detailed analysis, which allows for timeline views or Flamegraph analysis.
-
Built-in Web UI: php-spx comes with its own web user interface, enabling users to:
- Activate and configure profiling for the current browsing session.
- Browse through and select detailed script reports.
- Analyze reports with interactive views such as timelines and Flamegraphs.
System Requirements
- Supports the following architectures: x86-64 or ARM64.
- Compatible with GNU/Linux, macOS, and FreeBSD operating systems.
- Requires the zlib development package and PHP versions from 5.4 to 8.4.
Installation Process
To get started with php-spx, the following steps are necessary:
-
Prerequisites: Ensure the PHP development package and zlib development package are installed on your system.
-
Building the Extension: Clone the repository, prepare the build environment, and compile the extension:
git clone https://github.com/NoiseByNorthwest/php-spx.git cd php-spx git checkout release/latest phpize ./configure make sudo make install
-
Configuration: Add
extension=spx.so
to your php.ini file or a separate spx.ini file.
Profiling Usage
Web Requests
To profile a web request during development:
- Access your application URL with a special query string to open the control panel:
http://localhost/?SPX_KEY=dev&SPX_UI_URI=/
. - Enable profiling by toggling the switch in the web UI.
Command Line Scripts
- Instant Flat Profile: Simply prepend your command with
SPX_ENABLED=1
to see the execution profile. - Generate Full Reports: To generate reports viewable in the web UI, set
SPX_REPORT=full
when running your script.
Special Cases
php-spx also allows for profiling long-running processes with user-defined profiling spans. This is done using the built-in functions spx_profiler_start()
and spx_profiler_stop()
in your PHP code.
Advanced Features
php-spx provides extensive configurations, allows custom metadata in reports, and provides a comprehensive list of metrics and report types for both web and command-line profiling.
Conclusion
While still experimental, php-spx is a robust tool for PHP profiling in non-production environments. It provides insights without altering the existing codebase, making it an ideal choice for developers looking to optimize PHP applications efficiently and privately. Contributions to its development are welcome, with a focus on non-production usages to ensure reliability during this phase.