How to Install and Use XHProf for PHP Performance Profiling

This guide explains what XHProf is, walks through installing it via PECL or source, configuring php.ini, verifying the installation, integrating it with a middleware class, and accessing the web UI to view detailed performance metrics and visualizations.

Open Source Tech Hub
Open Source Tech Hub
Open Source Tech Hub
How to Install and Use XHProf for PHP Performance Profiling

What Is XHProf?

XHProf is a lightweight hierarchical profiler for PHP that records call counts and timing data during execution, then presents the information in an HTML‑based UI showing call graphs, exclusive metrics such as wall‑time, CPU time, and memory usage.

Installation

PECL Installation

Refer to the PECL extension installation chapter in the manual; the package page https://pecl.php.net/package/xhprof provides version, download, source, maintainer, and changelog information.

Source Installation

wget https://pecl.php.net/get/xhprof-2.3.9.tgz
 tar -zxvf xhprof-2.3.9.tgz
 cd ./xhprof-2.3.9/extension
 phpize
 ./configure --with-php-config=/usr/local/php-7.4/bin/php-config
 make
 make install

php.ini Configuration

[xhprof]
extension=xhprof.so;
xhprof.output_dir=/tmp/xhprof;

Verify Installation

# php --ri xhprof

xhprof

xhprof support => enabled
Version => 2.3.9

Usage

Middleware Integration (XhprofMiddleware)

class XhprofMiddleware implements MiddlewareInterface
{
    /**
     * @desc process 描述
     * @param Request $request
     * @param callable $handler
     * @return Response
     */
    public function process(Request $request, callable $handler): Response
    {
        $xhprof = $request->get('xhprof', 0);
        $extension = extension_loaded('xhprof');
        if ($xhprof && $extension) {
            // include XHProf library files
            include_once public_path() . "/xhprof/xhprof_lib/utils/xhprof_lib.php";
            include_once public_path() . "/xhprof/xhprof_lib/utils/xhprof_runs.php";
            xhprof_enable(XHPROF_FLAGS_NO_BUILTINS + XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);
        }
        $response = $handler($request);
        if ($xhprof && $extension) {
            $data = xhprof_disable();
            $objXhprofRun = new \XHProfRuns_Default();
            $objXhprofRun->save_run($data, sprintf('%s', date("YmdHis")));
        }
        return $response;
    }
}

Viewing the Profiling Results

Access URL: http://webman.xhprof.com/xhprof/xhprof_html/index.php
Existing runs
Webman\App::Webman{closure}
Graphical display

Metric Definitions

Function Name: 方法名称。
Calls: 方法被调用的次数。
Calls %: 方法调用次数在同级方法总数调用次数中所占的百分比。
Incl.Wall Time (microsec): 方法执行花费的时间,包括子方法的执行时间。
IWall %: 方法执行花费的时间百分比。
Excl.Wall Time (microsec): 方法本身执行花费的时间,不包括子方法的执行时间。
EWall %: 方法本身执行花费的时间百分比。
Incl.CPU (microsecs): 方法执行花费的 CPU 时间,包括子方法的执行时间。
ICpu %: 方法执行花费的 CPU 时间百分比。
Excl.CPU (microsec): 方法本身执行花费的 CPU 时间,不包括子方法的执行时间。
ECPU %: 方法本身执行花费的 CPU 时间百分比。
Incl.MemUse (bytes): 方法执行占用的内存,包括子方法执行占用的内存。
IMemUse %: 方法执行占用的内存百分比。
Excl.MemUse (bytes): 方法本身执行占用的内存,不包括子方法执行占用的内存。
EMemUse %: 方法本身执行占用的内存百分比。
Incl.PeakMemUse (bytes): MemUse 峰值。
IPeakMemUse %: MemUse 峰值百分比。
Excl.PeakMemUse (bytes): MemUse 峰值(不含子方法)。
EPeakMemUse %: MemUse 峰值百分比。
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

middlewareperformance profilingPHPXHProf
Open Source Tech Hub
Written by

Open Source Tech Hub

Sharing cutting-edge internet technologies and practical AI resources.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.