Skip to content

A stopwatch extension for phpunit. Get timing for parts of your code to detect performance bottlenecks.

License

Notifications You must be signed in to change notification settings

teqneers/phpunit-stopwatch

Repository files navigation

phpunit-stopwatch

CI

Scrutinizer Code Quality Code Coverage Code Climate

Code Coverage Type Coverage

This project provides a composer package and a Phar archive with an extension for measuring and analysing parts of your code during a test run with phpunit/phpunit.

The extension is compatible with the following versions of phpunit/phpunit:

Once you've added some measurement points to your code, the extension will stop watch and count them. The results are displayed for each test and as a total report at the end of the test run.

Here is an example of how the output of a single test class might look:

Stopwatch for TQ\Tests\Example\SingleTest::testDataCalculation:
- TQ\Testing\Database::deleteData                         0.117secs (    3x, Ø   0.04) TOTAL    327.026secs (  184x, Ø   1.78)
- ...onment\Testing::cleanupInstance                      0.259secs (    1x, Ø   0.26) TOTAL      6.159secs (   60x, Ø   0.10)
- TQ\Testing\Database::import                             7.889secs (   11x, Ø   0.72) TOTAL    250.958secs (  352x, Ø   0.71)
- Test                                                    1.428secs (    1x, Ø   1.43) TOTAL   1041.228secs (   70x, Ø  14.87)
.

Stopwatch for TQ\Tests\Example\SingleTest::testDataTransfer:
- TQ\Testing\Database::deleteData                         0.116secs (    3x, Ø   0.04) TOTAL    327.142secs (  187x, Ø   1.75)
- ...onment\Testing::cleanupInstance                      0.256secs (    1x, Ø   0.26) TOTAL      6.415secs (   61x, Ø   0.11)
- TQ\Testing\Database::import                             7.573secs (   11x, Ø   0.69) TOTAL    258.531secs (  363x, Ø   0.71)
- Test                                                    5.998secs (    1x, Ø   6.00) TOTAL   1047.226secs (   71x, Ø  14.75)
.

Stopwatch for TQ\Tests\Example\SingleTest TearDown:
- TQ\Testing\Database::deleteData                        38.486secs (    6x, Ø   6.41) TOTAL    365.511secs (  190x, Ø   1.92)
- ...onment\Testing::cleanupInstance                      0.256secs (    1x, Ø   0.26) TOTAL      6.415secs (   61x, Ø   0.11)
- TQ\Testing\Database::import                             7.573secs (   11x, Ø   0.69) TOTAL    258.531secs (  363x, Ø   0.71)
- Test                                                    5.998secs (    1x, Ø   6.00) TOTAL   1047.226secs (   71x, Ø  14.75)

And at the end of the test run, you will get a summary of all stopwatches used, and it is going to look like this:

Stopwatch TOTALS:
- Test                                                                                 TOTAL   1047.246secs (   78x, Ø  13.43)
- TQ\Testing\Database::deleteData                                                      TOTAL    365.511secs (  190x, Ø   1.92)
- TQ\Testing\Database::import                                                          TOTAL    258.531secs (  363x, Ø   0.71)
- ...onment\Testing::cleanupInstance                                                   TOTAL      6.416secs (   62x, Ø   0.10)
- TQ\Production\Monitoring::ping                                                       TOTAL     17.967secs (    7x, Ø   2.57)

Usage

Stopwatch is very easy to use. All you need is to start and stop the stopwatch anywhere in your code with a single line of code. Let's say your tests are pretty slow, but you don't know who's the culprit? Your guess is that it might be in the database setup. Go there and add this around the code you suspect:

        Stopwatch::start(__METHOD__);

        self::initializeDatabase(
            $db->getConnection(),
            ...static::createData()
        );

        Stopwatch::stop(__METHOD__);

If a test, a setUp, tearDown, setUpBeforeClass or tearDownAfterClass method executes that code, the stopwatch will measure the time it takes to execute the code between the start and stop calls and display it in the test output.

Installation

Installation with composer

Run

composer require --dev teqneers/phpunit-stopwatch

to install teqneers/phpunit-stopwatch as a composer package.

Installation as Phar

Download phpunit-stopwatch.phar from the latest release.

Usage

Bootstrapping the extension

Before the extension can detect slow tests in phpunit/phpunit, you need to bootstrap it. The bootstrapping mechanism depends on the version of phpunit/phpunit you are using.

Bootstrapping the extension as a composer package

To bootstrap the extension as a composer package when using

  • phpunit/phpunit:^10.0.0
  • phpunit/phpunit:^11.0.0

adjust your phpunit.xml configuration file and configure the

 <phpunit
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
     bootstrap="vendor/autoload.php"
 >
+    <extensions>
+        <bootstrap class="TQ\Testing\Extension\Stopwatch\Extension"/>
+    </extensions>
     <testsuites>
         <testsuite name="unit">
             <directory>test/Unit/</directory>
         </testsuite>
     </testsuites>
 </phpunit>

Bootstrapping the extension as a PHAR

To bootstrap the extension as a PHAR when using

  • phpunit/phpunit:^10.0.0
  • phpunit/phpunit:^11.0.0

adjust your phpunit.xml configuration file and configure the

 <phpunit
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
     bootstrap="vendor/autoload.php"
+    extensionsDirectory="directory/where/you/saved/the/extension/phars"
 >
+    <extensions>
+        <extension class="TQ\Testing\Extension\Stopwatch\Extension"/>
+    </extensions>
     <testsuites>
         <testsuite name="unit">
             <directory>test/Unit/</directory>
         </testsuite>
     </testsuites>
 </phpunit>

Configuring the extension

So far, there are no configuration settings for the extension.

Running tests

When you have bootstrapped the extension, you can run your tests as usually. E.g.:

vendor/bin/phpunit

When the extension is used somewhere in your code, it will give you a report:

License

This project uses the MIT license.

Credits

This package is inspired by ergebnis/phpunit-slow-test-detector.