2017 © Pedro Peláez
 

library phpunit-speedtrap

Find slow tests in your PHPUnit test suite

image

johnkary/phpunit-speedtrap

Find slow tests in your PHPUnit test suite

  • Friday, July 13, 2018
  • by johnkary
  • Repository
  • 13 Watchers
  • 372 Stars
  • 1,981,683 Installations
  • PHP
  • 93 Dependents
  • 2 Suggesters
  • 28 Forks
  • 9 Open issues
  • 11 Versions
  • 12 % Grown

The README.md

phpunit-speedtrap

Integrate, (*1)

SpeedTrap reports on slow-running PHPUnit tests right in the console., (*2)

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., (*3)

SpeedTrap helps identify slow tests but cannot explain why those tests are slow. Consider using Blackfire.io to profile the test suite to specifically identify slow code., (*4)

Screenshot of terminal using SpeedTrap, (*5)

Installation

SpeedTrap is installed using Composer. Add it as a require-dev dependency:, (*6)

composer require --dev johnkary/phpunit-speedtrap

Usage

Enable with all defaults by adding the following code to your project's phpunit.xml file:, (*7)

<phpunit bootstrap="vendor/autoload.php">
...
    <extensions>
        <extension class="JohnKary\PHPUnit\Extension\SpeedTrap" />
    </extensions>
</phpunit>

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

Config Parameters

SpeedTrap also supports these parameters:, (*9)

  • slowThreshold - Number of milliseconds when a test is considered "slow" (Default: 500ms)
  • reportLength - Number of slow tests included in the report (Default: 10 tests)

Each parameter is set in phpunit.xml:, (*10)

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

    <extensions>
        <extension class="JohnKary\PHPUnit\Extension\SpeedTrap">
            <arguments>
                <array>
                    <element key="slowThreshold">
                        <integer>500</integer>
                    </element>
                    <element key="reportLength">
                        <integer>10</integer>
                    </element>
                </array>
            </arguments>
        </extension>
    </extensions>
</phpunit>

Custom slowness threshold per-test case

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., (*11)

The annotation @slowThreshold can set a custom slowness threshold for each test case. This number may be higher or lower than the default threshold and is used instead of the default threshold for that specific test., (*12)

class SomeTestCase extends PHPUnit\Framework\TestCase
{
    /**
     * @slowThreshold 5000
     */
    public function testLongRunningProcess()
    {
        // Code that takes a longer time to execute
    }
}

Setting @slowThreshold 0 will never report that test as slow., (*13)

Disable slowness profiling using an environment variable

SpeedTrap profiles for slow tests when enabled in phpunit.xml. But using an environment variable named PHPUNIT_SPEEDTRAP can enable or disable the extension:, (*14)

$ PHPUNIT_SPEEDTRAP="disabled" ./vendor/bin/phpunit

Use case: Disable profiling in development, but profile with Travis CI

Travis CI is popular for running tests in the cloud after pushing new code to a repository., (*15)

Step 1) Enable SpeedTrap in phpunit.xml, but set PHPUNIT_SPEEDTRAP="disabled" to disable profiling when running tests., (*16)

<phpunit bootstrap="vendor/autoload.php">
...
    <php>
        <env name="PHPUNIT_SPEEDTRAP" value="disabled" />
    </php>

    <extensions>
        <extension class="JohnKary\PHPUnit\Extension\SpeedTrap" />
    </extensions>
</phpunit>

Step 2) Configure .travis.yml with PHPUNIT_SPEEDTRAP="enabled" to profile for slow tests when running on Travis CI:, (*17)

language: php

php:
  - 7.3

env:
  - PHPUNIT_SPEEDTRAP="enabled"

Step 3) View the Travis CI build output and read the slowness report printed in the console., (*18)

Travis CI Documentation - Environment Variables, (*19)

Use case: Enable profiling in development, but disable with Travis CI

Step 1) Enable SpeedTrap in phpunit.xml. The slowness report will output during all test suite executions., (*20)

<phpunit bootstrap="vendor/autoload.php">
...
    <extensions>
        <extension class="JohnKary\PHPUnit\Extension\SpeedTrap" />
    </extensions>
</phpunit>

Step 2) Configure .travis.yml with PHPUNIT_SPEEDTRAP="disabled" to turn off profiling when running on Travis CI:, (*21)

language: php

php:
  - 7.3

env:
  - PHPUNIT_SPEEDTRAP="disabled"

Step 3) View the Travis CI build output and confirm the slowness report is not printed in the console., (*22)

Use case: Only enable SpeedTrap on demand via command-line

Useful when you only want to profile slow tests once in a while., (*23)

Step 1) Setup phpunit.xml to enable SpeedTrap, but disable slowness profiling by setting PHPUNIT_SPEEDTRAP="disabled" like this:, (*24)

<phpunit bootstrap="vendor/autoload.php">
...
    <php>
        <env name="PHPUNIT_SPEEDTRAP" value="disabled" />
    </php>

    <extensions>
        <extension class="JohnKary\PHPUnit\Extension\SpeedTrap" />
    </extensions>
</phpunit>

Step 2) When executing phpunit from the command-line, enable slowness profiling only for this run by passing the environment variable PHPUNIT_SPEEDTRAP="enabled" like this:, (*25)

$ PHPUNIT_SPEEDTRAP=enabled ./vendor/bin/phpunit

Using with Symfony Framework

Symfony Framework comes with package symfony/phpunit-bridge that installs its own version of PHPUnit and ignores what is defined in your project's composer.json or composer.lock file. See the PHPUnit versions it installs with command ls vendor/bin/.phpunit/, (*26)

symfony/phpunit-bridge allows environment variable SYMFONY_PHPUNIT_REQUIRE to define additional dependencies while installing phpunit., (*27)

The easiest way to set environment variables for the script simple-phpunit is via phpunit.xml.dist:, (*28)

<phpunit bootstrap="vendor/autoload.php">
    <php>
        <env name="SYMFONY_PHPUNIT_REQUIRE" value="johnkary/phpunit-speedtrap:^4"/>
        <env name="SYMFONY_PHPUNIT_VERSION" value="9"/>
    </php>

    <extensions>
        <extension class="JohnKary\PHPUnit\Extension\SpeedTrap" />
    </extensions>
</phpunit>

Using the above example, running vendor/bin/simple-phpunit will now install the latest PHPUnit 9 and require the latest phpunit-speedtrap v4., (*29)

Development

Follow these steps to add new features or develop your own fork:, (*30)

# Get source code (or replace with your fork URL)
$ git checkout https://github.com/johnkary/phpunit-speedtrap.git phpunit-speedtrap

# Install dev dependencies
$ cd phpunit-speedtrap
$ composer install

# Run test suite to verify code runs as expected
$ vendor/bin/phpunit

Inspiration

SpeedTrap was inspired by RSpec's --profile option that displays feedback about slow tests., (*31)

License

phpunit-speedtrap is available under the MIT License., (*32)

The Versions

13/07 2018

dev-master

9999999-dev https://github.com/johnkary/phpunit-speedtrap

Find slow tests in your PHPUnit test suite

  Sources   Download

MIT

The Requires

 

phpunit profile slow

24/02 2018

v3.0.0

3.0.0.0 https://github.com/johnkary/phpunit-speedtrap

Find slow tests in your PHPUnit test suite

  Sources   Download

MIT

The Requires

 

phpunit profile slow

06/12 2017

v2.0.0

2.0.0.0 https://github.com/johnkary/phpunit-speedtrap

Find slow tests in your PHPUnit test suite

  Sources   Download

MIT

The Requires

 

phpunit profile slow

25/03 2017

1.1.x-dev

1.1.9999999.9999999-dev https://github.com/johnkary/phpunit-speedtrap

Find slow tests in your PHPUnit test suite

  Sources   Download

MIT

The Requires

 

phpunit profile slow

25/03 2017

v1.1.0

1.1.0.0 https://github.com/johnkary/phpunit-speedtrap

Find slow tests in your PHPUnit test suite

  Sources   Download

MIT

The Requires

 

phpunit profile slow

25/03 2017

1.0.x-dev

1.0.9999999.9999999-dev https://github.com/johnkary/phpunit-speedtrap

Find slow tests in your PHPUnit test suite

  Sources   Download

MIT

The Requires

 

phpunit profile slow

25/03 2017

v1.0.3

1.0.3.0 https://github.com/johnkary/phpunit-speedtrap

Find slow tests in your PHPUnit test suite

  Sources   Download

MIT

The Requires

 

phpunit profile slow

17/03 2017

v2.0.0-BETA1

2.0.0.0-beta1 https://github.com/johnkary/phpunit-speedtrap

Find slow tests in your PHPUnit test suite

  Sources   Download

MIT

The Requires

 

phpunit profile slow

13/02 2017

v1.0.2

1.0.2.0 https://github.com/johnkary/phpunit-speedtrap

Find slow tests in your PHPUnit test suite

  Sources   Download

MIT

The Requires

 

phpunit profile slow

13/09 2015

v1.0.1

1.0.1.0 https://github.com/johnkary/phpunit-speedtrap

Find slow tests in your PHPUnit test suite

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

phpunit profile slow

21/03 2015

v1.0.0

1.0.0.0 https://github.com/johnkary/phpunit-speedtrap

Find slow tests in your PHPUnit test suite

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

phpunit profile slow