The Cost of Test Isolation (and other PHPUnit Features)

Some of PHPUnit features come with the cost of a performance penality. This posting explores the effect of the --no-syntax-check, $backupGlobals = FALSE;, and --coverage-html options.

Syntax Check: disabled, $GLOBALS Backup: disabled, Code Coverage: disabled

By default, PHPUnit performs a syntax check on the test source files before it loads them. This behaviour can be disabled using the --no-syntax-check option.

Also by default, PHPUnit runs your tests in a way where even changes to global and super-global variables (such as $GLOBALS) do not affect other tests. The backup and restore operations for the global and super-global variables that ensure this behaviour can be disabled by declaring $backupGlobals = FALSE; in a test case class. This was done for the benchmark below.

sb@ubuntu Money % time phpunit --no-syntax-check MoneyTest
PHPUnit 3.3.6-dev by Sebastian Bergmann.

......................

Time: 0 seconds

OK (22 tests, 34 assertions)
phpunit --no-syntax-check MoneyTest
0.16s user 0.08s system 100% cpu 0.235 total
Profiling PHPUnit with Xdebug

Syntax Check: disabled, $GLOBALS Backup: enabled, Code Coverage: disabled

The following benchmark clearly shows the cost of the calls to the serialize() and unserialize() functions that are performed during the backup and restore operations for the global and super-global variables.

sb@ubuntu Money % time phpunit --no-syntax-check MoneyTest
PHPUnit 3.3.6-dev by Sebastian Bergmann.

......................

Time: 0 seconds

OK (22 tests, 34 assertions)
phpunit --no-syntax-check MoneyTest
0.31s user 0.14s system 93% cpu 0.483 total
Profiling PHPUnit with Xdebug

Syntax Check: enabled, $GLOBALS Backup: enabled, Code Coverage: disabled

Although the syntax check of test source files involves the creation of a new PHP process, the slowdown does not look too bad according to the following benchmark.

sb@ubuntu Money % time phpunit MoneyTest
PHPUnit 3.3.6-dev by Sebastian Bergmann.

......................

Time: 0 seconds

OK (22 tests, 34 assertions)
phpunit MoneyTest
0.31s user 0.16s system 95% cpu 0.487 total
Profiling PHPUnit with Xdebug

Syntax Check: enabled, $GLOBALS Backup: enabled, Code Coverage: enabled

The performance penality that comes with code coverage data collection and report generation that we see in the final benchmark has already been discussed previously on this blog here and here.

sb@ubuntu Money % time phpunit --coverage-html /tmp/coverage MoneyTest
PHPUnit 3.3.6-dev by Sebastian Bergmann.

......................

Time: 0 seconds

OK (22 tests, 34 assertions)
Generating code coverage report, this may take a moment.
phpunit --coverage-html /tmp/coverage MoneyTest
0.95s user 0.26s system 95% cpu 1.275 total
Profiling PHPUnit with Xdebug