Spaces:
No application file
No application file
File size: 1,799 Bytes
d2897cd |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
<?php
declare(strict_types=1);
namespace Mautic\CoreBundle\Test\Hooks;
use PHPUnit\Runner\AfterLastTestHook;
use PHPUnit\Runner\AfterTestHook;
/**
* This extension outputs a list of slow test classes to the STDOUT.
* Enable this extension by setting the following environmental variable `MAUTIC_TEST_LOG_SLOW_TESTS=1`.
* You can set an optional threshold in seconds (e.g. `MAUTIC_TEST_SLOW_TESTS_THRESHOLD=1.5`).
*/
class SlowTestExtension implements AfterTestHook, AfterLastTestHook
{
/**
* @var bool
*/
private $enabled;
/**
* Threshold in seconds.
*
* @var float
*/
private $threshold;
/**
* @var array<string, float>
*/
private $classes = [];
/**
* @var float
*/
private $started;
public function __construct()
{
$this->enabled = (bool) getenv('MAUTIC_TEST_LOG_SLOW_TESTS');
$this->threshold = (float) (getenv('MAUTIC_TEST_SLOW_TESTS_THRESHOLD') ?: 2);
$this->started = microtime(true);
}
public function executeAfterTest(string $test, float $time): void
{
if (!$this->enabled) {
return;
}
$time = microtime(true) - $this->started;
$this->started = microtime(true);
if ($time <= $this->threshold) {
return;
}
$class = substr($test, 0, strpos($test, '::'));
if (!isset($this->classes[$class])) {
$this->classes[$class] = 0;
}
$this->classes[$class] += $time;
}
public function executeAfterLastTest(): void
{
if (!$this->classes) {
return;
}
arsort($this->classes);
fwrite(STDOUT, PHP_EOL.'Slow test classes:'.PHP_EOL.var_export($this->classes, true));
}
}
|