Home php教程 php手册 使用XDebug调试及单元测试覆盖率分析

使用XDebug调试及单元测试覆盖率分析

Jun 21, 2016 am 08:55 AM
output pear phpunit xdebug

今天我就就自己对XDebug使用的一些体验做一小段分享。XDebug也是因为需要是用来生成覆盖率分析文件才安装的,刚接触不久,平时用的也不是很频繁,但是这个的确是一个好工具,如果想要依赖它来分析程序的性能还是需要自己亲手去试试。具体它有多好,请听我一一道来。

一、安装篇(XDebug PHPUnit)

A:安装XDebug:

Xdebug网下载xdebug dll文件,存放到php加载的ext目录下(可以选择重命名,比如php_xdebug.dll,然后打开php.ini文件,添加配置

[xdebug]

zend_extension_ts = php_xdebug.dll

xdebug.profiler_enable = on

xdebug.trace_output_dir = D:\PHPAPP\XDebug

xdebug.profiler_output_dir=D:\PHPAPP\XDebug

xdebug需要采用zend引擎加载的方式加载,所以用zend_extension_ts或是zend_extension, tsthread_safety, 目前5.3以上才支持zend_extension加载,的用zend_extension_ts加载(或者你可以查看phpinfo是否启用了zts),然后重启apache,查看phpinfo,就发现XDebug选项了。


可以看到xdebug.profiler_enablexdebug.profiler_output_dirxdebug.trace_output_dir这三个是我们刚刚配置的项,可以安装自己的需要配置剩余项,xdebug.profiler_enable:开启性能分析,

开启这个之后会在xdebug.profiler_output_dir这个目录下生成cachegrind.out.XXX的分析文件,如果指定了xdebug.profiler_output_name这个,则输出来的文件就会是指定的名字.xxx的格式(此方法不适合分析,原因稍后说明),

xdebug.trace_options:这个是开启跟踪项,

开启该项之后,会在xdebug.trace_output_dir下生产跟踪文件,注意该项会影响运行速度,因为他需要记录跟踪过程中的一系列事件。

关于XDebug还有一点需要注意的:XDebug是调试的,也就是说配置了XDebug那程序运行的就是一个Debug状态,所以这时候zend_optimizer就不能用了,总不能让程序即调试状态又处于优化状态哈。

更多关于XDebug的,大家去网上发掘吧~~然后一起探讨哈

友情提醒:对于使用集成环境的同学,比如Wamp,它拥有两个PHP.ini配置文件,一个在Apache下,一个在PHP下,Apache下是影响web的,而PHP下是影响命令行的,所以····(感谢俊哥的提醒)

B:安装PHPUnit:

关于PHPUnit的安装过程,网上也已经很多了,之前自己为了终于安装成功之后也小写了篇安装篇(http://user.qzone.qq.com/414073277/blog/1292122868),所以这里就不再赘述了,网上的一些安装教程大都能完成安装。就简单说下过程:

PHPUnit安装用的是pear的安装方式,所以如果机子没有安装过pear包需要先安装下,一般在php安装的时候在其安装目录下都有一个go-pear.bat文件,直接执行即可完成安装,【这里安装需要注意扩展exif,这个扩展会用到mbstring扩展中的方法,所以mbstring的加载顺序需要再exif之上。】完成pear的安装之后就可以执行pear,查看pear的命令参数,


然后依次执行:

pear channel-discover pear.phpunit.de

pear install –alldeps phpunit/PHPUnit

需要注意的是现在最新版的phpunit3.5的··但是这个只有pear的版本是1.6以上及PHP5.2.X才会安装得到最新版·否则是安装的是第一次的版本(不过不影响使用)。

安装好后就可以执行phpunit查看相关信息:


可以看到有—coverage-html –coverage-clover –coverage-source这三个选项·就是因为在XDebug安装成功之后才能用,这三个对于导出覆盖率文件可是很重要的选项。

二、PHPUnit 单元测试覆盖率分析

单元测试,最重要的指标之一就是覆盖率。这点虽然Zend Studio上可以很清晰的看出来,可是要导出成一分文档就不太好使了,所以这时候XDebug就又可以帮上忙了,使用的时候也挺简单,调用命令即可:

phpunit –coverage-html D:\PHPAPP\XDebug\ YouTest

这样就会将生成的覆盖文件(html格式的)放在D:\PHPAPP\XDebug\目录下了,


然后就可以在放置的目录下找到一堆的html文件,运行下index.html


就可以看到这样的图了,英文字母很简单··意思也明了就不解释了,说明一点:

测试结果说明:一共有5种,上图的例子出现了两种。

. :代表正确

F :代表断言错误

E :代表PHP程序错误或是异常

I 代表没有实现的方法

S : 代表是跳过的方法

生产的覆盖文件说明:

Classes: 只有一个类中的所有方法都被覆盖的时候,这个类才算是被测试完的

Functions/Methods :只有一个方法或是函数的所有有效的语句代码都被执行到了才算这个方法或是函数是被测试完的

Lines: 总行数将会不饱行注释,空行, 标签, 及类及方法的声明。

使用phpunit –coverage-clover D:\PHPAPP\XDebug\first.xml YouTest

生产的将会是一个xml格式的文件,这个格式的文件效果不明显,没有html格式的好,这边的效果被我删掉了所以么有效果图可看,第三个覆盖文件的参数我没测试过,大家有兴趣可以试试。

三、程序执行性能分析

XDebug除了有效的帮助分析单元测试覆盖率之外,还可以帮组分析程序执行的瓶颈所在,开启分析的功能:

xdebug.profiler_enable = on

xdebug.profiler_output_dir=D:\PHPAPP\XDebug

设置上面两项,然后执行你要分析的程序,就会在上面的目录下看到cachegrind.out.XXX的文件,这个文件直接打开就是一些记录,需要借助第三方的cachegrind查看工具才行,一个好用的工具是winCacheGrind,查看起来很方便。


可以看到执行的总时间,及每个函数执行的时间。

因为这个工具在打开文件的时候,只能打开cachegrind.out.*的文件,所以待分析的文件必须是这个名字的,所以为了省去重命名,这也就是为啥我说不建议修改xdebug.profiler_output_name这个选项的原因。

四、XDebug库函数

XDebug除了上述方式外,还有自己的一些函数库提供,可以在你程序的代码段任何地方执行,具体的请看http://xdebug.org/docs/all_functions

XDebug的功能,还不止这些,因为他的选项·好大的一页,所以还是去http://xdebug.org/docs/这里看看官方的说明。靠谱!



Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use PHPUnit for Mock testing in PHP development How to use PHPUnit for Mock testing in PHP development Jun 27, 2023 am 10:25 AM

In PHP development, testing is a very important link. Testing can greatly reduce the occurrence of errors and improve code quality. Mock testing is a form of testing that can simulate fake objects or data in order to test a specific function or scenario of our code. PHPUnit is a very popular testing framework in PHP, which supports Mock testing. In this article, we will explore how to use PHPUnit for mock testing. 1. What is Mock testing? Before we start, let’s come first

Test reporting tool in PHP Test reporting tool in PHP May 24, 2023 am 08:24 AM

PHP is a common open source programming language that is widely used in Web development. Its advantages are that it is easy to learn, easy to use, and highly scalable. As developers, in order to improve development efficiency while ensuring code quality, it is essential to use testing and test reports. In PHP development, there are many testing and test reporting tools, the most common of which is PHPUnit. However, although PHPUnit is simple and easy to use, it requires some basic knowledge of writing test cases. If you are not familiar with it, it is still difficult to use it.

Will enabling XDebug on a production server make PHP slower? Will enabling XDebug on a production server make PHP slower? Sep 22, 2023 pm 10:41 PM

Yes, debuggers like XDebug can slow down PHP server performance. This is why the debugger is not placed in a server environment. They are deployed in different environments to avoid unnecessary overhead. Debug messages cannot be displayed in applications that are already in production. When debugging behavior is added to the server, the debugging engine is attached to the PHP process. It starts receiving messages to stop at the breakpoint, but this is not required behavior as it would give a performance hit to other processes, thus stopping the PHP parser. On the other hand, when debuggers are installed, they tend to open ports in the server because they are not intended for use in a production environment. Opening a port in your server is just as bad as opening a door for hackers to snoop through.

How to check code convention and quality using PHP and PHPUnit How to check code convention and quality using PHP and PHPUnit Jun 25, 2023 pm 04:57 PM

In modern software development, code quality and specifications are extremely important factors. Not only can it make the code cleaner and easier to maintain, it can also improve the readability and scalability of the code. But how do you check the quality and specification of your code? This article will explain how to use PHP and PHPUnit to achieve this goal. Step 1: Check the code specification. In PHP development, there is a very popular code specification, which is called PSR (PHP Standard Specification). The purpose of the PSR specification is to make PHP code more readable and maintainable. in

Code inspection tools in PHP Code inspection tools in PHP May 24, 2023 pm 12:01 PM

Checking code quality is a task that every programmer must do, and there are many tools in PHP that can be used to check the quality and style of code, thereby improving the readability and maintainability of the code, and improving the reliability and security of the code. sex. This article will introduce several common PHP code inspection tools and conduct a simple comparison and evaluation of them. I hope it can help readers choose appropriate tools during the development process and improve code quality and efficiency. PHP_CodeSnifferPHP_CodeSniffer is a widely used

What are the common code quality tools in PHP programming? What are the common code quality tools in PHP programming? Jun 12, 2023 am 08:16 AM

What are the common code quality tools in PHP programming? In modern software development, code quality is very important. If the code quality is not good, it will not only reduce the readability of the code and increase the difficulty of maintenance, but also cause a series of problems such as security vulnerabilities. In PHP programming, we can use some code quality tools to check the quality of the code. This article will introduce some common PHP code quality tools. PHP_CodeSnifferPHP_CodeSniffer is a tool for static analysis of PHP code

How to use PHPUnit and Mockery for unit testing? How to use PHPUnit and Mockery for unit testing? May 31, 2023 pm 04:10 PM

In PHP project development, unit testing is a very important task. PHPUnit and Mockery are two very popular PHP unit testing frameworks. PHPUnit is a widely used unit testing tool, while Mockery is an object simulation tool that focuses on providing a unified and concise API to create and manage object mocks. By using PHPUnit and Mockery, developers can quickly and efficiently perform unit testing to ensure the correctness and stability of their code base

How to use PHPUnit for PHP unit testing How to use PHPUnit for PHP unit testing May 12, 2023 am 08:13 AM

With the development of the software development industry, testing has gradually become an indispensable part. As the most basic part of software testing, unit testing can not only improve code quality, but also speed up developers' development and maintenance of code. In the field of PHP, PHPUnit is a very popular unit testing framework that provides various functions to help us write high-quality test cases. In this article, we will cover how to use PHPUnit for PHP unit testing. Install PHPUnit and use PHPUnit

See all articles