microtime(TRUE), 'stop' => FALSE, 'memory_start' => self::memory_usage(), 'memory_stop' => FALSE); array_unshift(self::$marks[$name], $mark); } /** * Set a benchmark stop point. * * @param string benchmark name * @return void */ public static function stop($name) { if (self::$_resetThisCounter) { self::$_resetThisCounter = false; return; } if (isset(self::$marks[$name]) and self::$marks[$name][0]['stop'] === FALSE) { self::$marks[$name][0]['stop'] = microtime(TRUE); self::$marks[$name][0]['memory_stop'] = self::memory_usage(); } } /** * Get the elapsed time between a start and stop. * * @param string benchmark name, TRUE for all * @param integer number of decimal places to count to * @return array */ public static function get($name, $decimals = 4) { if ($name === TRUE) { $times = array(); $names = array_keys(self::$marks); foreach ($names as $name) { // Get each mark recursively $times[$name] = self::get($name, $decimals); } // Return the array return $times; } if (! isset(self::$marks[$name])) return FALSE; if (empty(self::$marks[$name])) { return array('time' => 0, 'memory' => 0, 'count' => 0); } if (self::$marks[$name][0]['stop'] === FALSE) { // Stop the benchmark to prevent mis-matched results self::stop($name); } // Return a string version of the time between the start and stop points // Properly reading a float requires using number_format or sprintf $time = $memory = 0; $count = count(self::$marks[$name]); for ($i = 0; $i < $count; $i ++) { $time += self::$marks[$name][$i]['stop'] - self::$marks[$name][$i]['start']; $memory += self::$marks[$name][$i]['memory_stop'] - self::$marks[$name][$i]['memory_start']; } return array( 'time' => number_format($time, $decimals), 'memory' => number_format(($memory / 1024 / 1024), 4), 'count' => $count); } /** * Dump marks to log file.(Testing) * * @return array */ public static function dump() { if (! count(self::$marks)) { return false; } $marks = Zeed_Benchmark::get(true); $logFile = ZEED_PATH_DATA . 'log/Benchmark-' . date('Y-m-d') . '.log'; $clientIP = Zeed_Util::clientIP(); $clientName = 'xsharp' . rand(1, 8); $time = time(); if (false != $handle = fopen($logFile, 'a')) { $logContent = ''; foreach ($marks as $name => $mark) { $time += rand(1, 8); //$logContent .= $clientIP . ' ' . $clientName . ' [' . date('m/M/Y:H:i:s +0800') . '] "GET ' . $name . ' HTTP/1.1" ' . $mark['time'] . ' ' . $mark['memory'] . ' ' . $mark['count'] . "\n"; $logContent .= $clientIP . ' - ' . $clientName . ' [' . date('m/M/Y:H:i:s +0800', $time) . '] "GET /' . $name . ' HTTP/1.1" 200 ' . (int) ($mark['time'] * 1000000) . "\r\n"; } fwrite($handle, $logContent); fclose($handle); } } /** * Returns the current memory usage. This is only possible if the * memory_get_usage function is supported in PHP. * * @return integer */ private static function memory_usage() { static $func = NULL; if ($func === NULL) { // Test if memory usage can be seen $func = function_exists('memory_get_usage'); } return $func ? memory_get_usage() : 0; } /** * Colorful print_r() * * @param Array|String|Mixed $var * @param String $memo */ public static function print_r($var, $memo = null) { if (isset($_SERVER['HTTP_USER_AGENT'])) { $color_bg = "RGB(" . rand(100, 255) . "," . rand(100, 255) . "," . rand(100, 255) . ")"; if (! is_null($memo)) { $prefix = '
' . (is_string($memo)? $memo : print_r($memo, true)) . ''; $postfix = '
'; } else { $prefix = $postfix = ""; } echo $prefix . '
' . "\n";
            print_r($var);
            echo "\n
\n" . $postfix; } else { if (! is_null($memo)) { if (is_string($memo)) { echo $memo; } else { print_r($memo); } echo " - - - - -\n"; } print_r($var); echo "\n"; } } public static function println($str, $flush = true) { if (isset($_SERVER['HTTP_USER_AGENT'])) { echo $str . '
'; } else { echo $str . "\n"; } if ($flush) { Zeed_Util::flush(); } } /** * */ public static function sql() { if (class_exists('Fit_Db', false)) { $dbInstances = & Fit_Db::$instances; } elseif (class_exists('Zeed_Db', false)) { $dbInstances = & Zeed_Db::$instances; } else { return null; } if (empty($dbInstances)) { return null; } $totalTime = 0; $queryCount = 0; $longestTime = 0; $longestQuery = null; $sqlAll = array(); $i = 0; foreach ($dbInstances as $name => $dbAdapter) { $profiler = $dbAdapter->getProfiler(); $_cQueryCount = $profiler->getTotalNumQueries(); if ($_cQueryCount < 1) { continue; } else { $totalTime += $profiler->getTotalElapsedSecs(); $queryCount += $profiler->getTotalNumQueries(); } foreach ($profiler->getQueryProfiles() as $_qp) { $_es = $_qp->getElapsedSecs(); $_q = $_qp->getQuery(); $sqlAll["$_es"] = array('sql' => $_q, 'time' => $_es, 'id' => $i, 'con' => $name); if ($_qp->getQueryType() == Zend_Db_Profiler::SELECT) { $sqlAll["$_es"]['explain'] = $dbAdapter->query('EXPLAIN ' . $_q)->fetchAll(); } if ($_es > $longestTime) { $longestTime = $_es; $longestQuery = $_q; } $i ++; } } if ($queryCount < 1) { return null; } echo '+'; echo ''; } } // End ^ LF ^ UTF-8