如何使用XHProf从命令行配置PHPUnit测试?

时间:2013-06-20 15:06:02

标签: php phpunit xhprof

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

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

3 个答案:

答案 0 :(得分:10)

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

通过Composer安装Listener,然后在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>

答案 1 :(得分:3)

补充@ Gordon关于xhprof的答案。

分为两部分:

  1. PECL extension
  2. user-space viewer
  3. 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
    

答案 2 :(得分:0)

确保已将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 = /路径/到/ OUTPUT_DIR     xdebug.profiler_output_name = cachegrind.out。%R.%叔     

修改 糟糕,你是对的,我正在考虑使用cachegrind。要添加的行将在下面,但我的主要观点是相同的。请务必查看cli php.ini。

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