【问题标题】:How can I use XHProf to profile PHPUnit tests from the command line?如何使用 XHProf 从命令行分析 PHPUnit 测试?
【发布时间】:2013-06-20 15:06:01
【问题描述】:

我有一些非常慢的 PHPUnit 测试(43 次测试需要 8 分钟),我需要使用 XHProf 找出问题所在。

如何从命令行执行此操作?我在项目的 /vendor 目录中有 PHPUnit,通过 composer 加载。

【问题讨论】:

    标签: php phpunit xhprof


    【解决方案1】:

    要找出哪些测试运行缓慢,您可以使用

    通过 Composer 安装监听器,然后在你的 phpunit.xml 中启用它,例如

    <phpunit bootstrap="vendor/autoload.php">
    ...
        <listeners>
            <listener class="JohnKary\PHPUnit\Listener\SpeedTrapListener" />
        </listeners>
    </phpunit>
    

    要找出为什么测试运行缓慢,您可以使用

    可以在phpunit.xml中配置,例如

     <listeners>
      <listener class="PHPUnit_Util_Log_XHProf" file="PHPUnit/Util/Log/XHProf.php">
       <arguments>
        <array>
         <element key="xhprofLibFile">
          <string>/var/www/xhprof_lib/utils/xhprof_lib.php</string>
         </element>
         <element key="xhprofRunsFile">
          <string>/var/www/xhprof_lib/utils/xhprof_runs.php</string>
         </element>
         <element key="xhprofWeb">
          <string>http://localhost/xhprof_html/index.php</string>
         </element>
         <element key="appNamespace">
          <string>Doctrine2</string>
         </element>
         <element key="xhprofFlags">
          <string>XHPROF_FLAGS_CPU,XHPROF_FLAGS_MEMORY</string>
         </element>
         <element key="xhprofIgnore">
          <string>call_user_func,call_user_func_array</string>
         </element>
        </array>
       </arguments>
      </listener>
     </listeners>
    

    【讨论】:

    【解决方案2】:

    补充@Gordon 关于 xhprof 的回答。

    分为两部分:

    1. PECL extension
    2. user-space viewer

    PECL 扩展为 PHP 引擎添加了用于度量收集的方法。您必须安装此扩展程序。

    用户空间查看器提供了一个 Web 界面,用于了解度量收集的输出。你不需要这个,但你真的想要它。除非您喜欢查看原始指标数据。要安装和配置用户空间查看器,以便 PHPUnit 可以分析您的测试:

    (1) 将这些包添加到您的composer.json:

    composer require "facebook/xhprof:dev-master@dev" --dev
    composer require "phpunit/test-listener-xhprof:1.0.*@dev" --dev
    

    (2) 配置您的 Web 服务器以服务于 vendor/facebook/xhprof/xhprof_html/ 目录。记住网址。

    (3) 将您现有的 PHPUnit 配置调整为与此类似的 phpunit-xhprof.xml。确保更改“appNamespace”以匹配您的代码,并将“xhprofWeb”更改为第 2 步中的 URL:

    <phpunit>
      <testsuites>
        <testsuite name="All Tests">
          <directory suffix="Test.php">tests/</directory>
        </testsuite>
      </testsuites>
      <listeners>
        <listener class="PHPUnit\XHProfTestListener\XHProfTestListener" file="vendor/phpunit/test-listener-xhprof/src/XHProfTestListener.php">
         <arguments>
          <array>
           <element key="appNamespace">
            <string>App</string>
           </element>
           <element key="xhprofWeb">
            <string>http://localhost/vendor/facebook/xhprof/xhprof_html/index.php</string>
           </element>
           <element key="xhprofLibFile">
            <string>./vendor/facebook/xhprof/xhprof_lib/utils/xhprof_lib.php</string>
           </element>
           <element key="xhprofRunsFile">
            <string>./vendor/facebook/xhprof/xhprof_lib/utils/xhprof_runs.php</string>
           </element>
           <element key="xhprofFlags">
            <string>XHPROF_FLAGS_CPU,XHPROF_FLAGS_MEMORY</string>
           </element>
           <element key="xhprofIgnore">
            <string>call_user_func,call_user_func_array</string>
           </element>
          </array>
         </arguments>
        </listener>
       </listeners>
    </phpunit>
    

    (4) 运行PHP并收集统计信息:phpunit -c ./phpunit-xhprof.xml

    您将看到类似于以下内容的输出:

     * BishopB\Pattern\Exception\InvalidArgumentTest::test_hierarchy
       http://localhost/vendor/facebook/xhprof/xhprof_html/index.php?run=556e05cec844c&source=BishopB\Pattern
    

    这是您配置用于查看运行结果的 URL。如果您想查看原始指标数据,请在您的临时目录中找到运行密钥(在本例中为“556e05cec844c”):

    $ ls -l /tmp/556e05cec844c.BishopB\\Pattern.xhprof
    -rw-rw-r-- 1 bishop staff 16963 Jun  2 15:36 /tmp/556e05cec844c.BishopB\Pattern.xhprof
    

    【讨论】:

      【解决方案3】:

      确保您已将 xdebug 行添加到您的 cli php.ini,而不仅仅是 apache php.ini。在 Ubuntu 上,它位于 /etc/php5/cli/php.ini。您应该将这些行添加到文件中: zend_extension=/usr/lib/php5/20100525+lfs/xdebug.so xdebug.profiler_enable=1 xdebug.profiler_output_dir=/path/to/output_dir xdebug.profiler_output_name=cachegrind.out.%R.%t

      编辑: 哎呀,你是对的,我正在考虑使用 cachegrind。要添加的行将在下面,但我的主要观点是相同的。确保查看 cli php.ini。

      [xhprof]
      extension=xhprof.so
      xhprof.output_dir="/var/tmp/xhprof"
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-08-15
        • 1970-01-01
        • 2010-10-19
        • 1970-01-01
        • 2019-03-05
        • 2014-02-04
        • 2016-11-28
        • 2014-03-21
        相关资源
        最近更新 更多