Speed up PHPUnit Code Coverage Analysis

Updated on 2019–06–23.

PHPUnit 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 my results of running the test suites of three sample projects:

Image for post
Image for post
Impact of activating code coverage analysis with default xdebug setup.

As you can see, activating the code coverage analysis multiplies the time needed to run the test suites. Those are just examples of course, and depending on the size and complexity of your project and its tests 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 three quick ways of speeding up the code coverage collection: first by optimizing xdebug usage, then by replacing it with an alternative code coverage driver (phpdbg and pcov).

1. Whitelist filtering with xdebug

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

# dump filter file
$ php vendor/bin/phpunit --dump-xdebug-filter build/xdebug-filter.php
# run the test suite
$ php vendor/bin/phpunit --prepend build/xdebug-filter.php --coverage-html build/coverage-report

Results

Image for post
Image for post
Impact of using xdebug filtering.

2. Replacing xdebug with phpdbg

Usage

# run the test suite
$ phpdbg -qrr vendor/bin/phpunit --coverage-html build/coverage-report

Results

Image for post
Image for post
Impact 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.

Impressive results ! But 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.

3. Using PCOV as the code coverage driver

Usage

# install the extension
$ pecl install pcov
# run the test suite
$ php -d pcov.enabled=1 vendor/bin/phpunit — coverage-html=var/coverage-report

Results

Image for post
Image for post
Impact of using PCOV as code coverage driver.

PCOV seams to be the clear winner right now ! Execution time is faster than phpdbg, and memory consumption is lower than xdebug. You will have to be able to install the extension on your CI platform to benefit from it, but it’s definitely worth a try !

I would like to thank the authors of all libraries cited in this article, including Sebastian Bergmann for PHPUnit, Derick Rethans for Xdebug, Joe Watkins for both Phpdbg and PCOV, and all the contributors for their hard work and everything they bring to the PHP community.

Written by

Full-stack web developer, Symfony, Docker, Elasticsearch, NodeJS, React, Vue and Flutter enthusiast. https://github.com/slashfan + https://twitter.com/nicocabot

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store