/** * @param int $count * @return array */ public function runTest($count = 1) { gc_collect_cycles(); // Forces collection of any existing garbage cycles $this->_profiler->start(); for ($i = 0; $i < $count; $i++) { // Store the result so it appears in memory profiling $this->_executeTest(); } $this->_profiler->stop(); $time = $this->_profiler->getTime(); $timeOne = $this->_profiler->getTime() / $count; $memory = $this->_profiler->getMemory(); return array('time' => $time, 'time_one' => $timeOne, 'memory' => $memory, 'count' => $count, 'formated' => sprintf("Time: %s/%s; Memory: %s; Count: %s", Timer::formatMS($time), Timer::formatMS($timeOne), FS::format($memory), $count)); }
/** * Format the results, rounding numbers, showing difference percentages * and removing a flat time based on the benchmark overhead * * @param array $results array($name => array('time' => 1.0)) * @return array array(array('Test' => $name, 'Time' => '1000 ms', 'Perc' => '100 %')) */ public function formatResults(array $results) { uasort($results['list'], function ($testOne, $testTwo) { if ($testOne['time'] === $testTwo['time']) { return 0; } else { return $testOne['time'] < $testTwo['time'] ? -1 : 1; } }); $minTime = INF; $minMemory = INF; foreach ($results['list'] as $name => $result) { // time $time = $result['time']; //$time -= $this->_overhead['time']; // Substract base_time $results['list'][$name]['time'] = $time; $minTime = min($minTime, $time); // memory $memory = $results['list'][$name]['memory']; $memory -= $this->_overhead['memory']; $results['list'][$name]['memory'] = $memory; $minMemory = min($minMemory, $memory); } $output = array(); $isOne = count($results['list']) === 1; foreach ($results['list'] as $name => $result) { if ($isOne) { $output[] = array(self::COL_NAME => $name, self::COL_TIME => $this->_timeFormat($result['time'], 0), self::COL_TIME_ONE => $this->_timeFormat($result['time'] / $this->_count), self::COL_MEMORY => FS::format($result['memory'], 2)); } else { $output[] = array(self::COL_NAME => $name, self::COL_TIME => $this->_timeFormat($result['time'], 0), self::COL_TIME_ONE => $this->_timeFormat($result['time'] / $this->_count), self::COL_TIME_REL => Vars::relativePercent($minTime, $result['time']), self::COL_MEMORY => FS::format($result['memory'], 2), self::COL_MEMORY_REL => Vars::relativePercent($minMemory, $result['memory'])); } } return $output; }
/** * Returns the resources (time, memory) of the request as a string. * * @param bool $getPeakMemory * @param bool $isRealMemory * @return string */ public static function resourceUsage($getPeakMemory = true, $isRealMemory = false) { if ($getPeakMemory) { $message = 'Time: %s, Peak memory: %s'; $memory = memory_get_peak_usage($isRealMemory); } else { $message = 'Time: %s, Memory: %s'; $memory = memory_get_usage($isRealMemory); } $memory = FS::format($memory, 2); $time = Timer::format(Timer::timeSinceStart()); return sprintf($message, $time, $memory); }
public function testFormat() { $size = FS::format(512, 0); is('512 B', $size); $size = FS::format(512, 2); is('512 B', $size); $size = FS::format(2048, 1); is('2.0 KB', $size); $size = FS::format(25151251, 2); is('23.99 MB', $size); $size = FS::format(19971597926, 2); is('18.60 GB', $size); $size = FS::format(2748779069440, 1); is('2.5 TB', $size); $size = FS::format(2814750000000000.0, 1); is('2.5 PB', $size); }