xhprof-html 不会显示 运行 报告(没有在 URL 中指定的 XHProf 运行)

xhprof-html won't show Run Report (No XHProf runs specified in the URL)

如何使用 xhprof-html 显示给定 xhprof 运行 的 运行 报告?

我安装了tideways-xhprof

apt-get install php-dev
git clone "https://github.com/tideways/php-xhprof-extension.git"
cd php-xhprof-extension
phpize
./configure
make
make install

我在 php.ini

中启用了它
extension=/usr/lib/php/20190902/tideways_xhprof.so

configured wordpress to write-out xhprof runs 在我的 wp-config.php 文件中

# PHP profiling with tideways-xhprof 
#  * https://pressjitsu.com/blog/profiling-wordpress-performance/
if ( isset( $_GET['profile'] ) && $_GET['profile'] === 'secret-string' ) {
    tideways_xhprof_enable( TIDEWAYS_FLAGS_MEMORY + TIDEWAYS_FLAGS_CPU );
    register_shutdown_function( function() {
        $results = tideways_xhprof_disable();
        file_put_contents( '/path/to/my/xhprof/dir/' .date('Y-m-d\TH:i:s\Z', time() - date('Z')). '.xhprof' , serialize( $results ) );
    });
}

/** Sets up WordPress vars and included files. */
require_once(ABSPATH . 'wp-settings.php');

并且我通过访问 https://example.com/index.php?profile=secret-string

成功地导出了 xhprof 文件
root@host:/path/to/my/xhprof/dir/# du -sh *
1,6M    2022-05-25T18:15:45Z.xhprof
1,6M    2022-05-25T18:18:38Z.xhprof
root@host:/path/to/my/xhprof/dir/#

我还用 xhprof-html. And I configured it 创建了一个网站来查找上述 .xhprof 个文件

root@host:/var/www/xhprof/xhprof-html# diff index.orig.php index.php 
83c83
< $xhprof_runs_impl = new XHProfRuns_Default();
---
> $xhprof_runs_impl = new XHProfRuns_Default( '/path/to/my/xhprof/dir' );
root@host:/var/www/xhprof/xhprof-html#

现在我可以加载访问 xhprof-html/index.php 文件,它成功地在 https://xhprof.example.com/xhprof-html/index.php

显示了我的两个 .xhprof 文件
No XHProf runs specified in the URL.

Existing runs:

    2022-05-25T18:18:38Z.xhprof 2022-05-25 18:18:38
    2022-05-25T18:15:45Z.xhprof 2022-05-25 18:15:45

如果我点击其中任何一个,我将被重定向(如预期的那样)到以下任一页面:

但是,I would expect 上面的页面呈现实际 Run Report。但他们没有。相反,我只看到

No XHProf runs specified in the URL.

Existing runs:

    2022-05-25T18:18:38Z.xhprof 2022-05-25 18:18:38
    2022-05-25T18:15:45Z.xhprof 2022-05-25 18:15:45

喂? XHProf 运行在URL中明确指定。

如何让 xhprof-html 显示实际的 Run Report

Run Report 没有显示,因为它无法读取文件。

问题

您正在使用 tideways xhprof 生成文件并将序列化输出写入文件

$results = tideways_xhprof_disable();
file_put_contents( '/path/to/my/xhprof/dir/' .date('Y-m-d\TH:i:s\Z', time() - date('Z')). '.xhprof' , serialize( $results ) );

(source)

但随后,您切换到托管 xhprof 的较旧(不再维护)分支以在您的网络服务器的文档根目录xhprof_html中提供服务

Note: Of course you have to do this, and even the tideways docs link you to the phacility repo. The tideways repo itself curiously does not include the xhprof_html directory, possibly because tideways is a business that sells xhprof visualization SaaS -- so they stripped the "poor man's" xhprof_html from the fork of xhprof that they maintain

如果您想使用 XHProfRuns_Default() 读取 文件,那么您应该使用 XHProfRuns_Default() 写入 文件.

解决方案

wp-config.php 中的块更改为以下内容

# PHP profiling with tideways-xhprof 
#  * https://pressjitsu.com/blog/profiling-wordpress-performance/
if ( isset( $_GET['profile'] ) && $_GET['profile'] === 'secret-string' ) {
   tideways_xhprof_enable( TIDEWAYS_FLAGS_MEMORY + TIDEWAYS_FLAGS_CPU );
   register_shutdown_function( function() {
      $results = tideways_xhprof_disable();
      include_once( '/var/www/xhprof/xhprof_lib/utils/xhprof_runs.php' );
      $XHProfRuns = new XHProfRuns_Default( '/path/to/my/xhprof/dir' );
      $XHProfRuns->save_run( $results, date('Y-m-d\TH:i:s\Z', time() - date('Z')) );
   });
}

然后生成一个新的配置文件

您现在会看到一个名为 62906fb2c49b4.2022-05-27T06:29:06Z.xhprof

的新项目
No XHProf runs specified in the URL.

Existing runs:

    62906fb2c49b4.2022-05-27T06:29:06Z.xhprof 2022-05-27 06:29:06
    2022-05-25T18:18:38Z.xhprof 2022-05-25 18:18:38
    2022-05-25T18:15:45Z.xhprof 2022-05-25 18:15:45

点击这个新项目,你会看到 Run Report 现在它可以读取 xhprof 文件格式了。