johnkary/phpunit-speedtrap

Output total time taken by slow tests

bdsl opened this issue · 2 comments

bdsl commented

I think it would be really handy to see the total time taken by all the slow tests, to get a good idea of how useful it would be to speed them up or move them out of the main test suite.

I haven't looked at the implementation of phpunit-speedtrap but if this is a desired feature I think I'd be happy to have a go at implementing it.

@bdsl That's a good idea. I would accept a PR for that feature if someone would like to implement it.

It should be fairly easy. If I were writing it, the endTest() method has access to all test execution times in milliseconds. Accumulate each test's execution times, then in a new output method like renderStats() calculate the total slow/fast times by partitioning the collected times, summing them, and formatting for output.

PHPUnit outputs the total execution time in seconds. That makes sense for this feature because many slow tests together will likely exceed 1 second.

Maybe an output like this when slow tests are found:

$ ./vendor/bin/phpunit --group=slow
PHPUnit 8.5.14 by Sebastian Bergmann and contributors.

..........                                                                 10 / 10 (100%)
You should really speed up these slow tests (>500ms)...
 1. 6805ms to run JohnKary\\PHPUnit\\Listener\\Tests\\SomeSlowTest::testWithDataProvider with data set "Rock"
 2. 1000ms to run JohnKary\\PHPUnit\\Listener\\Tests\\SomeSlowTest::testWithDataProvider with data set "Chalk"
 3. 695ms to run JohnKary\\PHPUnit\\Listener\\Tests\\SomeSlowTest::testWithDataProvider with data set "Jayhawk"

Fast Tests: 0.5 seconds (5.56%)
Slow Tests: 8.5 seconds (94.44%)

Time 9.0 seconds, Memory: 8.00 MB

If no slow tests are detected output Slow Tests: 0 seconds (0%) so the output remains consistent for anyone programmatically parsing it.

I'm open to any suggestions. The above is proposed as a starting point.

bdsl commented

Thanks @johnkary - sorry for radio silence. I'll try and give that a go this weekend.