public function query($type, $sql, $as_object) { if (isset($this->_config['connection']) === true) { $this->database = $this->_config['connection']['database']; } $result = parent::query($type, $sql, false); if ($type === Database::SELECT) { $table = array(); if (count($result) > 0) { foreach ($result->current() as $key => $data) { $table[0][] = $key; } $result->rewind(); foreach ($result as $row) { $table[] = $row; } $result->rewind(); } else { $table[] = array('No', 'rows'); } $group = Profiler::groups(); $group = Profiler::total($group['database (default)'][$sql][0]); FirePHP::getInstance()->table($this->database . ' : (' . number_format($group[0], 6) . 's) ' . $sql, $table); } elseif ($type === Database::INSERT) { FirePHP::getInstance()->info($this->database . ' : Insert id: ' . $result[0] . ' Affected rows: ' . $result[1]); } else { FirePHP::getInstance()->info($this->database . ' : Affected rows: ' . $result[0]); } return $result; }
/** * Smarty {report}{/report} block plugin * * Banded Report Generator Framework * * This is the main block for this framework and it acts as a container for the * rest of the {report_*} block types and handles the looping requirements of * the given dataset. * * @type block * @name report * @version 0.1.6 * @see http://www.phpinsider.com/smarty-forum/viewtopic.php?t=4125 * * @author boots < jayboots @at@ yahoo com > * @copyright brainpower, boots, 2004-2005 * @license LGPL * * @thanks messju mohr, sophistry * * @param recordset REQUIRED * @param record REQUIRED * @param groups default: null * @param resort default: false */ function smarty_block_Kohana_profiler($params, $content, &$smarty, &$repeat) { static $stack = array(); static $profiles = array(); static $firsttime = true; if ($firsttime) { $smarty->assign_by_ref('Kohana_profiles', $profiles); $firsttime = false; } if ($repeat) { // opening tag if (Kohana::$profiling) { $name = isset($params['name']) ? $params['name'] : 'other'; $token = Profiler::start('Smarty_rendering', $name); } else { $token = $name = ''; } // always need to do this in case the status of Kohana::$profiling is changed before closing tag array_push($stack, array($token, $name)); } else { // closing tag list($token, $name) = array_pop($stack); if (Kohana::$profiling) { Profiler::stop($token); // array (time, mem, size) $total = Profiler::total($token); $total[2] = strlen($content); $total[3] = microtime(true) - KOHANA_START_TIME; $total[4] = memory_get_usage(); $smarty->assign('Kohana_profiler', $total); if (isset($profiles[$name])) { $profiles[$name][0] += $total[0]; $profiles[$name][1] += $total[1]; $profiles[$name][2] += $total[2]; } else { $profiles[$name] = $total; } } else { $total = array(0, 0, strlen($content), microtime(true) - KOHANA_START_TIME, memory_get_usage()); $smarty->assign('Kohana_profiler', $total); } return $content; } }
/** * Gets the min, max, average and total of a set of tokens as an array. * * @param array profiler tokens * @return array min, max, average, total */ public static function stats(array $tokens) { // Min and max are unknown by default $min = $max = array('time' => NULL, 'memory' => NULL); // Total values are always integers $total = array('time' => 0, 'memory' => 0); foreach ($tokens as $token) { // Get the total time and memory for this benchmark list($time, $memory) = Profiler::total($token); if ($max['time'] === NULL or $time > $max['time']) { // Set the maximum time $max['time'] = $time; } if ($min['time'] === NULL or $time < $min['time']) { // Set the minimum time $min['time'] = $time; } // Increase the total time $total['time'] += $time; if ($max['memory'] === NULL or $memory > $max['memory']) { // Set the maximum memory $max['memory'] = $memory; } if ($min['memory'] === NULL or $memory < $min['memory']) { // Set the minimum memory $min['memory'] = $memory; } // Increase the total memory $total['memory'] += $memory; } // Determine the number of tokens $count = count($tokens); // Determine the averages $average = array('time' => $total['time'] / $count, 'memory' => $total['memory'] / $count); return array('min' => $min, 'max' => $max, 'total' => $total, 'average' => $average); }
public function __destruct() { $app = Profiler::application(); $group = Profiler::groups(); $table = array(); $table[] = array('Type', 'Time (s)', 'Mem (kb)'); foreach ($group as $rName => $route) { $table[] = array($rName); foreach ($route as $tName => $type) { foreach ($type as $stat) { $stats = Profiler::total($stat); $table[] = array($tName, number_format($stats[0], 6), number_format($stats[1] / 1024, 4)); } } } $this->fire->info(Session::instance()->as_array(), 'Session'); $this->fire->group('Stats: ' . $app['count']); $this->fire->info('Min: ' . number_format($app['min']['time'], 6) . 's ' . number_format($app['min']['memory'] / 1024, 4) . 'kb'); $this->fire->info('Max: ' . number_format($app['max']['time'], 6) . 's ' . number_format($app['max']['memory'] / 1024, 4) . 'kb'); $this->fire->info('Average: ' . number_format($app['average']['time'], 6) . 's ' . number_format($app['average']['memory'] / 1024, 4) . 'kb'); $this->fire->info('Total: ' . number_format($app['total']['time'], 6) . 's ' . number_format($app['total']['memory'] / 1024, 4) . 'kb'); $this->fire->groupEnd(); //$this->fire->table('Execution stats ('.number_format($endTime, 6).'s '.number_format($endMem, 4).'kb)', $table); }
/** * Runs Codebench on the extending class. * * @return array benchmark output */ public function run() { // Array of all methods to loop over $methods = array_filter(get_class_methods($this), array($this, '_method_filter')); // Make sure the benchmark runs at least once, // also if no subject data has been provided. if (empty($this->subjects)) { $this->subjects = array('NULL' => NULL); } // Initialize benchmark output $codebench = array('class' => get_class($this), 'description' => $this->description, 'loops' => array('base' => (int) $this->loops, 'total' => (int) $this->loops * count($this->subjects) * count($methods)), 'subjects' => $this->subjects, 'benchmarks' => array()); // Benchmark each method foreach ($methods as $method) { // Initialize benchmark output for this method $codebench['benchmarks'][$method] = array('time' => 0, 'memory' => 0); // Using Reflection because simply calling $this->$method($subject) in the loop below // results in buggy benchmark times correlating to the length of the method name. $reflection = new ReflectionMethod(get_class($this), $method); // Benchmark each subject on each method foreach ($this->subjects as $subject_key => $subject) { // Prerun each method/subject combo before the actual benchmark loop. // This way relatively expensive initial processes won't be benchmarked, e.g. autoloading. // At the same time we capture the return here so we don't have to do that in the loop anymore. $return = $reflection->invoke($this, $subject); // Start the timer for one subject $token = Profiler::start('codebench', $method . $subject_key); // The heavy work for ($i = 0; $i < $this->loops; ++$i) { $reflection->invoke($this, $subject); } // Stop and read the timer $benchmark = Profiler::total($token); // Benchmark output specific to the current method and subject $codebench['benchmarks'][$method]['subjects'][$subject_key] = array('return' => $return, 'time' => $benchmark[0], 'memory' => $benchmark[1]); // Update method totals $codebench['benchmarks'][$method]['time'] += $benchmark[0]; $codebench['benchmarks'][$method]['memory'] += $benchmark[1]; } } // Initialize the fastest and slowest benchmarks for both methods and subjects, time and memory, // these values will be overwritten using min() and max() later on. // The 999999999 values look like a hack, I know, but they work, // unless your method runs for more than 31 years or consumes over 1GB of memory. $fastest_method = $fastest_subject = array('time' => 999999999, 'memory' => 999999999); $slowest_method = $slowest_subject = array('time' => 0, 'memory' => 0); // Find the fastest and slowest benchmarks, needed for the percentage calculations foreach ($methods as $method) { // Update the fastest and slowest method benchmarks $fastest_method['time'] = min($fastest_method['time'], $codebench['benchmarks'][$method]['time']); $fastest_method['memory'] = min($fastest_method['memory'], $codebench['benchmarks'][$method]['memory']); $slowest_method['time'] = max($slowest_method['time'], $codebench['benchmarks'][$method]['time']); $slowest_method['memory'] = max($slowest_method['memory'], $codebench['benchmarks'][$method]['memory']); foreach ($this->subjects as $subject_key => $subject) { // Update the fastest and slowest subject benchmarks $fastest_subject['time'] = min($fastest_subject['time'], $codebench['benchmarks'][$method]['subjects'][$subject_key]['time']); $fastest_subject['memory'] = min($fastest_subject['memory'], $codebench['benchmarks'][$method]['subjects'][$subject_key]['memory']); $slowest_subject['time'] = max($slowest_subject['time'], $codebench['benchmarks'][$method]['subjects'][$subject_key]['time']); $slowest_subject['memory'] = max($slowest_subject['memory'], $codebench['benchmarks'][$method]['subjects'][$subject_key]['memory']); } } // Percentage calculations for methods foreach ($codebench['benchmarks'] as &$method) { // Calculate percentage difference relative to fastest and slowest methods $method['percent']['fastest']['time'] = empty($fastest_method['time']) ? 0 : $method['time'] / $fastest_method['time'] * 100; $method['percent']['fastest']['memory'] = empty($fastest_method['memory']) ? 0 : $method['memory'] / $fastest_method['memory'] * 100; $method['percent']['slowest']['time'] = empty($slowest_method['time']) ? 0 : $method['time'] / $slowest_method['time'] * 100; $method['percent']['slowest']['memory'] = empty($slowest_method['memory']) ? 0 : $method['memory'] / $slowest_method['memory'] * 100; // Assign a grade for time and memory to each method $method['grade']['time'] = $this->_grade($method['percent']['fastest']['time']); $method['grade']['memory'] = $this->_grade($method['percent']['fastest']['memory']); // Percentage calculations for subjects foreach ($method['subjects'] as &$subject) { // Calculate percentage difference relative to fastest and slowest subjects for this method $subject['percent']['fastest']['time'] = empty($fastest_subject['time']) ? 0 : $subject['time'] / $fastest_subject['time'] * 100; $subject['percent']['fastest']['memory'] = empty($fastest_subject['memory']) ? 0 : $subject['memory'] / $fastest_subject['memory'] * 100; $subject['percent']['slowest']['time'] = empty($slowest_subject['time']) ? 0 : $subject['time'] / $slowest_subject['time'] * 100; $subject['percent']['slowest']['memory'] = empty($slowest_subject['memory']) ? 0 : $subject['memory'] / $slowest_subject['memory'] * 100; // Assign a grade letter for time and memory to each subject $subject['grade']['time'] = $this->_grade($subject['percent']['fastest']['time']); $subject['grade']['memory'] = $this->_grade($subject['percent']['fastest']['memory']); } } return $codebench; }
/** * Retrieves query benchmarks from Database * * @return array */ public static function get_queries() { if (self::$_queries !== FALSE) { return self::$_queries; } $result = array(); $count = $time = $memory = 0; $groups = Profiler::groups(); foreach (Database::$instances as $name => $db) { $group_name = 'database (' . strtolower($name) . ')'; $group = arr::get($groups, $group_name, FALSE); if ($group) { $sub_time = $sub_memory = $sub_count = 0; foreach ($group as $query => $tokens) { $sub_count += count($tokens); foreach ($tokens as $token) { $total = Profiler::total($token); $sub_time += $total[0]; $sub_memory += $total[1]; $result[$name][] = array('name' => $query, 'time' => $total[0], 'memory' => $total[1]); } } $count += $sub_count; $time += $sub_time; $memory += $sub_memory; $result[$name]['total'] = array($sub_count, $sub_time, $sub_memory); } } self::$_queries = array('count' => $count, 'time' => $time, 'memory' => $memory, 'data' => $result); return self::$_queries; }
/** * 返回性能统计数据 * * @param array $tokens * @return array [min, max, average, total] * @example $stats = Profiler::stats($tokens); */ public static function stats(array $tokens) { $min = $max = array('time' => NULL, 'memory' => NULL); $total = array('time' => 0, 'memory' => 0); foreach ($tokens as $token) { list($time, $memory) = Profiler::total($token); if ($max['time'] === NULL or $time > $max['time']) { $max['time'] = $time; } if ($min['time'] === NULL or $time < $min['time']) { $min['time'] = $time; } $total['time'] += $time; if ($max['memory'] === NULL or $memory > $max['memory']) { $max['memory'] = $memory; } if ($min['memory'] === NULL or $memory < $min['memory']) { $min['memory'] = $memory; } $total['memory'] += $memory; } $count = count($tokens); $average = array('time' => $total['time'] / $count, 'memory' => $total['memory'] / $count); return array('min' => $min, 'max' => $max, 'total' => $total, 'average' => $average); }
/** * Retrieves query benchmarks from database * * <code> * Debugger::get_database_queries() * </code> * * @return array */ public static function get_database_queries() { if (!class_exists('Database')) { return array(); } if (Debugger::$_database_queries !== null) { return Debugger::$_database_queries; } $result = array(); $groups = Profiler::groups(); foreach (Database::$instances as $name => $db) { $group_name = 'database (' . strtolower($name) . ')'; $group = arr::get($groups, $group_name, false); if ($group) { $sub_time = $sub_memory = $sub_count = 0; foreach ($group as $query => $tokens) { $sub_count += count($tokens); foreach ($tokens as $token) { $total = Profiler::total($token); $sub_time += $total[0]; $sub_memory += $total[1]; $result[$name][] = array('name' => $query, 'time' => $total[0], 'memory' => $total[1]); } } } } return Debugger::$_database_queries = $result; }