* @license MIT * * @group Performance * @coversNothing */ abstract class BasePerformanceTest extends PHPUnit_Framework_TestCase { /** * @var float time when last capture was started */ private $startTime = 0; /** * @var int bytes when last capture was started */ private $startMemory = 0; /** * {@inheritDoc} */ public static function setUpBeforeClass() { $header = "Performance test - " . get_called_class() . ":"; echo "\n\n" . str_repeat('=', strlen($header)) . "\n" . $header . "\n\n"; } /** * Start profiler snapshot */ protected function startCapturing() { $this->startMemory = memory_get_usage(); $this->startTime = microtime(true); } /** * Echo current profiler output * * @param string $messageTemplate * * @return array */ protected function endCapturing($messageTemplate) { $time = microtime(true) - $this->startTime; $memory = memory_get_usage() - $this->startMemory; if (gc_enable()) { gc_collect_cycles(); } echo sprintf($messageTemplate, $time, $memory / 1024) . "\n"; return array( 'time' => $time, 'memory' => $memory ); } /** * Display comparison between two profiles * * @param array $baseProfile * @param array $proxyProfile */ protected function compareProfile(array $baseProfile, array $proxyProfile) { $baseMemory = max(1, $baseProfile['memory']); $timeOverhead = (($proxyProfile['time'] / $baseProfile['time']) - 1) * 100; $memoryOverhead = (($proxyProfile['memory'] / $baseMemory) - 1) * 100; echo sprintf('Comparison time / memory: %.2f%% / %.2f%%', $timeOverhead, $memoryOverhead) . "\n\n"; } }