Categories
Programming

Speed Up Your PHPUnit Tests – Disable Xdebug

I recently noticed that my PHPUnit tests were running kind of slow which prompted me to start looking into how to speed them up. After some digging, I discovered that Xdebug can have a major impact on the speed of your phpunit tests.

But, if you’re like me, you can’t live without a full debugging experience when you are coding, so disabling it permanently isn’t a viable option.

Xdebug

Xdebug is an extension for PHP that allows developers to pause execution and step through the program to see what is happening at any “breakpoint” they set in the code.

To enable Xdebug you can either add the proper configuration settings to your php.ini file or add and xdebug.ini file in the conf.d directory that PHP checks to load configuration options. To disable Xdebug these options must be commented out so that PHP doesn’t read them and load Xdebug.

At first glance, you might think your only option is to modify the Xdebug settings in php.ini every time you want to run tests to disable it and change them back when you are done running tests. This would be a MAJOR pain in the ass. However, there is a better way. In the rest of this article I’ll explain how.

It turns out that there are a few flags that you can use on the CLI to change the files that PHP loads before it runs. You can view all of the options in the PHP Options Documentation.

The -n Command Line Option (warning, may have side effects)

The first and simplest option is the -n option. This flag tells PHP to run without loading any .ini files.  In my research, I found that this option worked fine.

I would be really careful with this one. I think you could end up with some false positive and/or false negative test results because PHP isn’t loading any of the options defined in the .ini configuration files. Nonetheless PHPUnit runs much faster when you use this option.

When I run a subset of my test suite consisting of 63 tests and 177 assertions. Without the “-n” option PHPUnit runs for 5.12 seconds. With the “-n” option PHPUnit runs for a mere 2 seconds. And everything passes. But like I said, I’m concerned that since PHP isn’t loading any configuration options the “-n” flag might cause some problems.

To use the “-n” option in a Laravel app type the following to run PHPUnit.  If you aren’t in a Laravel app, you may have to change the location to your PHPUnit installation.

When you enter this command in the CLI you are telling PHP to run without loading any .ini configuration files. This options ignores Xdebug along with every other setting normally loaded by PHP.

The -c Command Line Option (better option)

This option tells PHP to look for and load any files you specify in the command. With this option we can ignore just the Xdebug settings before running PHPUnit. The benefit is – all of the other .ini configuration options are preserved. To do this in a Laravel application follow these steps.

1 – Create a file called “disable-xdebug.ini” in the root of your application folder. You can add this file to your .gitignore if you don’t want to include it in version control.

2 – Open the file and add the following code:

The “;” is a comment in a .ini file and tells PHP to ignore the settings that follow them.

3 – Run PHPUnit with the following command:

This command tells PHP to load the file you created in steps 1 and 2, which disables Xdebug since it loads after the original file.

The above describes steps to take for a Laravel app, but may be modified to work in any project.

In my test suite subset of 63 tests and 177 assertions I get the same results with this option as I do with the simpler but possibly less reliable “-n” option.

I also ran this on a larger subset of my test suite to see the difference. In that suite I have 411 tests and 1028 assertions. Without the “-c” option it runs in 36.67 seconds, with the “-c” option it runs in 15.44 seconds!  A  58% reduction in time to run the same tests!

Allow PHP to enable Xdebug makes tests slow!

 

Use PHP option “-c” to disable Xdebug before running makes tests fast!

Finding where .ini files are located on your system

To find the location of .ini files that your system’s PHP is loading try the following commands:

List all .ini files that are loaded by PHP


Output the total contents of all .ini config options


Output just .ini config options that pertain to Xdebug

 

4 replies on “Speed Up Your PHPUnit Tests – Disable Xdebug”

Unless I’m missing something here, php -c disable-xdebug.ini is almost the same thing as php -n except that with -c, if PHP is configured to use conf.d files, it will load them. -c file.ini _replaces_ the main php.ini file, but doesn’t load it on top of the existing file.

To verify this: php -c disable-xdebug.ini -i | grep ini

Thank you for the idea! I found similar solution with -d option.

xDebug 3 can be disabled with command like that:

php -d xdebug.mode=off vendor/bin/codecept

Leave a Reply

Your email address will not be published. Required fields are marked *