/** * Format the given value depending on the currently used unit */ protected function format($value) { if ($this->isPercentage()) { return (string) $value . '%'; } if ($this->isBytes()) { return Format::bytes($value); } if ($this->isSeconds()) { return Format::seconds($value); } return number_format($value, 2); }
/** * Prepares benchmark data for output * * Use Benchmark::TIME and Benchmark::MEMORY to choose whether you prefer * to have either time or memory or both in your output * * @param int Whether to get time and/or memory summary * @return array */ protected static function prepareDataForRendering($what = null) { if ($what === null) { $what = self::TIME | self::MEMORY; } $columns = array((object) array('title' => 'Time', 'align' => 'left', 'maxlen' => 4), (object) array('title' => 'Description', 'align' => 'left', 'maxlen' => 11)); if ($what & self::TIME) { $columns[] = (object) array('title' => 'Off (ms)', 'align' => 'right', 'maxlen' => 11); $columns[] = (object) array('title' => 'Dur (ms)', 'align' => 'right', 'maxlen' => 13); } if ($what & self::MEMORY) { $columns[] = (object) array('title' => 'Mem (diff)', 'align' => 'right', 'maxlen' => 10); $columns[] = (object) array('title' => 'Mem (total)', 'align' => 'right', 'maxlen' => 11); } $bench = self::getInstance(); $last = $bench->start; $rows = array(); $lastmem = 0; foreach ($bench->measures as $m) { $micro = sprintf('%03d', round(($m->timestamp - floor($m->timestamp)) * 1000)); $vals = array(date('H:i:s', $m->timestamp) . '.' . $micro, $m->message); if ($what & self::TIME) { $m->relative = $m->timestamp - $bench->start; $m->offset = $m->timestamp - $last; $last = $m->timestamp; $vals[] = sprintf('%0.3f', $m->relative * 1000); $vals[] = sprintf('%0.3f', $m->offset * 1000); } if ($what & self::MEMORY) { $mem = $m->memory - $lastmem; $lastmem = $m->memory; $vals[] = Format::bytes($mem); $vals[] = Format::bytes($m->memory); } $row =& $rows[]; foreach ($vals as $col => $val) { $row[$col] = $val; $columns[$col]->maxlen = max(strlen($val), $columns[$col]->maxlen); } } return (object) array('columns' => $columns, 'rows' => $rows); }