/** * Retrieve timer ids sorted to correspond the nesting * * @return array */ private function _getSortedTimers() { $timerIds = Magento_Profiler::getTimers(); if (count($timerIds) <= 2) { /* No sorting needed */ return $timerIds; } /* Prepare PCRE once to use it inside the loop body */ $nestingSep = preg_quote(Magento_Profiler::NESTING_SEPARATOR, '/'); $patternLastTimerName = '/' . $nestingSep . '(?:.(?!' . $nestingSep . '))+$/'; $prevTimerId = $timerIds[0]; $result = array($prevTimerId); for ($i = 1; $i < count($timerIds); $i++) { $timerId = $timerIds[$i]; /* Skip already added timer */ if (!$timerId) { continue; } /* Loop over all timers that need to be closed under previous timer */ while (strpos($timerId, $prevTimerId . Magento_Profiler::NESTING_SEPARATOR) !== 0) { /* Add to result all timers nested in the previous timer */ for ($j = $i + 1; $j < count($timerIds); $j++) { if (strpos($timerIds[$j], $prevTimerId . Magento_Profiler::NESTING_SEPARATOR) === 0) { $result[] = $timerIds[$j]; /* Mark timer as already added */ $timerIds[$j] = null; } } /* Go to upper level timer */ $count = 0; $prevTimerId = preg_replace($patternLastTimerName, '', $prevTimerId, -1, $count); /* Break the loop if no replacements was done. It is possible when we are */ /* working with top level (root) item */ if (!$count) { break; } } /* Add current timer to the result */ $result[] = $timerId; $prevTimerId = $timerId; } return $result; }
public function testGetTimers() { $expectedTimers = array('some_root_timer', 'some_root_timer->some_nested_timer', 'some_root_timer->some_nested_timer->some_deeply_nested_timer', 'one_more_root_timer'); $actualTimers = Magento_Profiler::getTimers(); $this->assertEquals($expectedTimers, $actualTimers); }