Skip to content
Search

Last updated

Find slow PHPUnit tests with OpenTelemetry

Let's say our test suite took two minutes last spring and takes nine now. Nobody added a slow test on purpose. It happened one test at a time, and each of them looked fine in its pull request.

These steps need a collector, so they run only on your machine or in CI, not in the playground.

Find slow tests with the JUnit log of PHPUnit

The first idea is the report PHPUnit can already write.

vendor/bin/phpunit --log-junit junit.xml
<testcase name="test_imports_one_order" class="Shop\Tests\OrderImportTest" time="0.025626"
<testcase name="test_imports_a_full_file" class="Shop\Tests\OrderImportTest" time="0.457305"
<testcase name="test_adds_tax" class="Shop\Tests\PriceCalculatorTest" time="0.000596"
<testcase name="test_rounds_to_cents" class="Shop\Tests\PriceCalculatorTest" time="0.000345"

The times are there, and with four tests we find the slow one with our eyes.

Why a JUnit file is not enough

  • A real suite has thousands of lines like these, in one XML file for each run. To find the slowest tests somebody has to write a script that sorts it.
  • The file knows one run. It can't say that a test took 40ms last month and takes 450ms now.
  • The slow runs happen in CI, on other machines. The file stays there, and it's gone with the build.
  • It has no memory use, so a test that eats 500Mb looks like any other.

We want the same numbers sent to a place that keeps them, from every run, with nothing to do for the person who runs the tests.

Send every PHPUnit test as a span

flow-php/telemetry is an independent implementation of OpenTelemetry, written in PHP, and it is not a wrapper around the official SDK. The bridge needs PHP 8.3 or newer, PHPUnit and the curl extension.

composer require --dev flow-php/phpunit-telemetry-bridge

The whole setup is one block in phpunit.xml:

<extensions>
    <bootstrap class="Flow\Bridge\PHPUnit\Telemetry\TelemetryExtension">
        <parameter name="service_name" value="shop-tests"/>
        <parameter name="endpoint" value="http://localhost:4318"/>
    </bootstrap>
</extensions>

We run the suite as always:

vendor/bin/phpunit
....                                                                4 / 4 (100%)

Time: 00:00.497, Memory: 50.50 MB

OK (4 tests, 4 assertions)

Nothing looks different, and no test changed. At the end of the run the extension sent one trace to the collector: a span for the run, one for each test class and one for each test.

Test Suite Run
  shop
    Shop\Tests\OrderImportTest
      test_imports_one_order
      test_imports_a_full_file
    Shop\Tests\PriceCalculatorTest
      test_adds_tax
      test_rounds_to_cents

Every test span has its duration, the result (test.case.result.status=passed), and the memory it used (flow.phpunit.test.memory.peak).

Show only the slow tests

The spans go to an OpenTelemetry Collector. The guide about the collector for PHP shows how to start one in Docker. We tell it to drop every span that is not a test, and every test faster than 100ms:

receivers:
  otlp:
    protocols:
      http:
        endpoint: 0.0.0.0:4318

processors:
  filter/slow-tests:
    error_mode: ignore
    traces:
      span:
        - 'attributes["test.case.name"] == nil'
        - 'end_time_unix_nano - start_time_unix_nano < 100000000'

exporters:
  debug:
    verbosity: normal

service:
  pipelines:
    traces:
      receivers: [otlp]
      processors: [filter/slow-tests]
      exporters: [debug]

We run the four tests again, and the collector prints one line, with the long attributes cut:

test_imports_a_full_file 9846d920ea0f27322718e52774f5bcfb cdd86f0f36b631d4 flow.phpunit.test.id=Shop\Tests\OrderImportTest::test_imports_a_full_file test.case.result.status=passed flow.phpunit.test.memory.peak=37305016

That's the test that imports a whole file, with its class and its peak memory in bytes. 100ms is 100000000 in the filter, because the collector counts in nanoseconds.

See the test suite get slower over time

One slow test is a finding. The trend is what we came for, and the extension sends it as metrics with every run:

flow.phpunit.test.duration{test.case.result.status=passed} count=4 sum=0.48274 max=0.45617
flow.phpunit.suite.duration{test.suite.name=Shop\Tests\OrderImportTest} count=1 sum=0.482965
flow.phpunit.suite.duration{test.suite.name=Shop\Tests\PriceCalculatorTest} count=1 sum=0.001313

A monitoring tool draws flow.phpunit.suite.duration as one line for each test class, run after run. The day a class doubles its time is visible on the chart, with the commit of that day next to it.

Send the results from CI

In CI the address of the collector and the name of the service come from the environment, so phpunit.xml stays the same for everybody:

FLOW_PHPUNIT_OTEL_ENDPOINT=https://otlp.example.com:4318 \
FLOW_PHPUNIT_OTEL_SERVICE_NAME=shop-tests \
vendor/bin/phpunit

On a machine with no collector the tests run and pass as before. The extension writes the reason to the PHP error log, and the result of the suite doesn't change.

Where to go from here

All the parameters, the headers for a hosted service and the file transport are in the bridge documentation. What a span and a trace are is in the guide about tracing in PHP.

Found a typo or an outdated section? Edit this guide on GitHub

scroll back to top