Speed up PHPUnit Code Coverage Analysis

Speed up PHPUnit Code Coverage AnalysisNicolas CabotBlockedUnblockFollowFollowingDec 19PHPUnit Code Coverage Analysis is often used with xdebug as the coverage data source..But it has one major drawback: it can become very slow.As a reference, here are the results of running the test suites of two sample projects:Impact of activating code coverage analysis with default xdebug setup.As you can see, activating the code coverage analysis on those projects multiplies the time needed to run the test suites..Those are just examples, and depending on the size of your project and the way your tests are written, you may obtain completely different results.Nevertheless, execution time will increase, sometimes to the point where your CI platform will time out before the end of the analysis.In this article, we will take a look at two simple ways of speeding up the code coverage collection: first by optimizing xdebug usage, and then by replacing it with phpdbg.1..Whitelist filtering with xdebugPHPUnit provides a whitelist configuration option to narrow down the coverage report to a set of files or directories..By default, PHPUnit handles this whitelist filter directly, but this task can also be delegated to xdebug itself (2.6+), resulting in a great improvement of the code coverage data collection.This can be done in two simple steps: first generate the xdebug filter script using PHPUnit, then include it using the prepend option of the test runner.Usage$ vendor/bin/phpunit –dump-xdebug-filter build/xdebug-filter.php$ vendor/bin/phpunit –prepend build/xdebug-filter.php –coverage-html build/coverage-reportResultsImpact of using xdebug filtering.2..Replacing xdebug with phpdbgThe second solution is to replace xdebug with another coverage data collector..As you may know, PHP 5.6+ includes its own debugger: phpdbg..And lucky for us, PHPUnit is compatible with it.Usage$ phpdbg -qrr vendor/bin/phpunit –coverage-html build/coverage-reportResultsImpact of using phpdbg as code coverage data source.You can find more information in the articles Generating Code Coverage with PHPUnit and phpdbg and Phpdbg Is Much Faster Than Xdebug For Code Coverage.Drawbacks of using phpdbgThere are two things to keep in mind, though.First, the report might slightly differ from one generated using xdebug..Personally, I’m more than willing to trade 20 minutes of execution time for 0.5% of difference in the final report..But it might not be possible for every project aiming at a perfect 100%.Secondly, as you can see, memory consumption rises drastically with phpdbg..Although it might not be a problem when running tests on a development machine or on a self-hosted CI platform, it may become problematic on some SaaS CI platforms with limited resources.Both solutions have their advantages and drawbacks: execution time for one, memory consumption for the other..In any case, your projects should greatly benefit from them.. More details

Leave a Reply