PHPUnit SpeedTrap

PHPUnit Speedtrap is a package by John Kary that reports on slow-running tests in your PHPUnit test suite. This package is inspired by Ruby RSpec’s --profile option that displays feedback about slow tests:

SpeedTrap 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.

From the project’s readme, here’s an example of what you can expect:

I am not super familiar with tapping into PHPUnit, so I enjoyed diving into the package’s source code and learning how PHPUnit listeners work. In a phpunit.xml file you can enable this package with the following XML:

<listeners>
    <listener class="JohnKary\PHPUnit\Listener\SpeedTrapListener" />
</listeners>

A more extensive example includes customizable options for defining the “slowTreshold” and “reportLength”. You can customize what your test suite considers a “slow” test defined in milliseconds:

<listeners>
    <listener class="JohnKary\PHPUnit\Listener\SpeedTrapListener">
        <arguments>
            <array>
                <element key="slowThreshold">
                    <integer>500</integer>
                </element>
                <element key="reportLength">
                    <integer>5</integer>
                </element>
            </array>
        </arguments>
    </listener>
</listeners>

Along with the listener, the package demonstrates using an annotation to override these configuration options on the per-test level:

/**
 * @slowThreshold 5000
 */
public function testLongRunningProcess()
{
    // Code that takes a longer time to execute
}

You can learn more about the PHPUnit Speedtrap package at johnkary/phpunit-speedtrap and also check out these tips on speeding up your PHPUnit tests