phpunit speedtrap

SpeedTrap is a very useful tool that reports on slow-running PHPUnit tests right in the console. Many factors affect test execution time. A test not properly isolated from variable latency (database, network, etc.) and even basic load on the test machine will cause test execution times to fluctuate.

The tool helps identify slow tests but cannot explain why those tests are slow. For that consider using Blackfire.io to profile the test suite, or another PHPUnit listener PHPUnit_Listener_XHProf, to specifically identify slow code.

Speedtrap

Install Speedtrap

Similar to PHPUnit, you can install it with Composer in dev mode :

$ composer require --dev johnkary/phpunit-speedtrap

Then you can enable it with all defaults by adding the following code to your project’s phpunit.xml file:

<phpunit bootstrap="vendor/autoload.php">
...
    <listeners>
        <listener class="JohnKary\PHPUnit\Listener\SpeedTrapListener" />
    </listeners>
</phpunit>

Now run the test suite. If one or more test executions exceed the slowness threshold (500ms by default), it will report on those tests in the console after all tests have completed.

Of course you can configure the slowThreshold and the reportLength, which respectively indicate the number of milliseconds when a test is considered “slow” and the number of slow tests included in the report. Some projects have a few complex tests that take a long time to run. It is possible to set a different slowness threshold for individual test cases.

You can also disable slowness profiling using an environment variable. Finally, Symfony users will have difficulties to directly run this tool, since the framework come with its own version of PHPUnit, however this version seems to be incompatible with the tool.

PHPUnit SpeedTrap is an open source tool released under the MIT License. More information at https://github.com/johnkary/phpunit-speedtrap

LEAVE A REPLY

Please enter your comment!
Please enter your name here