John Kary

This post may now be outdated. See the johnkary/phpunit-speedtrap project on GitHub for the most recent instructions.

All developers should strive to write "fast tests," and "fast" will be different for each developer, language and software stack. Traditionally, unit tests should be very fast, a few milliseconds or faster, while functional tests and end-to-end tests will run a bit longer, maybe a few seconds.

As your test suite grows, you can expect it to run longer. But if you're not careful, some slower tests can creep in and disproportionately increase your suite's total execution time.


SpeedTrap is a PHPUnit Listener that reports on slow-running tests in your PHPUnit test suite console output.

Screenshot of terminal output of SpeedTrap

Many factors affect test execution time. A test not properly isolated from variable latency (database, network, etc.) and even basic load on your test machine will cause test times to fluctuate.

SpeedTrap helps you identify slow tests but cannot tell you why those tests are slow. For that you should check out something like PHPUnit_Listener_XHProf to help identify specifically which methods in your call stack are slow.


SpeedTrap is installable via Composer and should be added as a dev dependency:

composer require --dev johnkary/phpunit-speedtrap


Enabled it with default configuration by adding the following to your test suite's phpunit.xml file:

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

Now run your test suite as normal. If tests run that exceed the slowness threshold (500ms by default), SpeedTrap will report on them in the console after the suite completes.


SpeedTrap has two configurable parameters:

These configuration parameters are set in phpunit.xml when adding the listener:

<phpunit bootstrap="vendor/autoload.php">
    <!-- ... other suite configuration here ... -->

<span class="nt">&lt;listeners&gt;</span>
    <span class="nt">&lt;listener</span> <span class="na">class=</span><span class="s">&quot;JohnKary\PHPUnit\Listener\SpeedTrapListener&quot;</span><span class="nt">&gt;</span>
        <span class="nt">&lt;arguments&gt;</span>
            <span class="nt">&lt;array&gt;</span>
                <span class="nt">&lt;element</span> <span class="na">key=</span><span class="s">&quot;slowThreshold&quot;</span><span class="nt">&gt;</span>
                    <span class="nt">&lt;integer&gt;</span>500<span class="nt">&lt;/integer&gt;</span>
                <span class="nt">&lt;/element&gt;</span>
                <span class="nt">&lt;element</span> <span class="na">key=</span><span class="s">&quot;reportLength&quot;</span><span class="nt">&gt;</span>
                    <span class="nt">&lt;integer&gt;</span>10<span class="nt">&lt;/integer&gt;</span>
                <span class="nt">&lt;/element&gt;</span>
            <span class="nt">&lt;/array&gt;</span>
        <span class="nt">&lt;/arguments&gt;</span>
    <span class="nt">&lt;/listener&gt;</span>
<span class="nt">&lt;/listeners&gt;</span>


This allows you to set your own criteria for "slow" tests, and how many you care to know about.


SpeedTest was inspired by Rspec's -p option that displays feedback about slow tests.


phpunit-speedtrap is available under the MIT License.

comments powered by Disqus