/**
 * Smarty {fetch} plugin
 *
 * Type:     function<br>
 * Name:     fetch<br>
 * Purpose:  fetch file, web or ftp data and display results
 * @link http://smarty.php.net/manual/en/language.function.fetch.php {fetch}
 *       (Smarty online manual)
 * @author Monte Ohrt <monte at ohrt dot com>
 * @param array
 * @param Smarty
 * @return string|null if the assign parameter is passed, Smarty assigns the
 *                     result to a template variable
 */
function smarty_function_Kohana_profile($params, &$smarty)
{
    $groups = Profiler::groups();
    $profile = array();
    foreach ($groups as $group_name => $group) {
        foreach ($group as $name => $member) {
            $stats = Profiler::stats($member);
            $profile[] = array('group_name' => $group_name, 'name' => $name, 'count' => count($member), 'total_time' => $stats['total']['time'], 'min_time' => $stats['min']['time'], 'max_time' => $stats['max']['time'], 'average_time' => $stats['average']['time'], 'total_memory' => $stats['total']['memory'], 'min_memory' => $stats['min']['memory'], 'max_memory' => $stats['max']['memory'], 'average_memory' => $stats['average']['memory']);
        }
    }
    $stats = Profiler::application();
    $profile[] = array('group_name' => 'Application timings', 'name' => 'Application timings', 'count' => $stats['count'], 'total_time' => $stats['total']['time'], 'min_time' => $stats['min']['time'], 'max_time' => $stats['max']['time'], 'average_time' => $stats['average']['time'], 'total_memory' => $stats['total']['memory'], 'min_memory' => $stats['min']['memory'], 'max_memory' => $stats['max']['memory'], 'average_memory' => $stats['average']['memory']);
    $smarty->assign('Kohana_profile', $profile);
}
Example #2
0
 /**
  * Gets the min, max, average and total of profiler groups as an array.
  *
  *     $stats = Profiler::group_stats('test');
  *
  * @param   mixed   $groups single group name string, or array with group names; all groups by default
  * @return  array   min, max, average, total
  * @uses    Profiler::groups
  * @uses    Profiler::stats
  */
 public static function group_stats($groups = NULL)
 {
     // Which groups do we need to calculate stats for?
     $groups = $groups === NULL ? Profiler::groups() : array_intersect_key(Profiler::groups(), array_flip((array) $groups));
     // All statistics
     $stats = array();
     foreach ($groups as $group => $names) {
         foreach ($names as $name => $tokens) {
             // Store the stats for each subgroup.
             // We only need the values for "total".
             $_stats = Profiler::stats($tokens);
             $stats[$group][$name] = $_stats['total'];
         }
     }
     // Group stats
     $groups = array();
     foreach ($stats as $group => $names) {
         // Min and max are unknown by default
         $groups[$group]['min'] = $groups[$group]['max'] = array('time' => NULL, 'memory' => NULL);
         // Total values are always integers
         $groups[$group]['total'] = array('time' => 0, 'memory' => 0);
         foreach ($names as $total) {
             if (!isset($groups[$group]['min']['time']) or $groups[$group]['min']['time'] > $total['time']) {
                 // Set the minimum time
                 $groups[$group]['min']['time'] = $total['time'];
             }
             if (!isset($groups[$group]['min']['memory']) or $groups[$group]['min']['memory'] > $total['memory']) {
                 // Set the minimum memory
                 $groups[$group]['min']['memory'] = $total['memory'];
             }
             if (!isset($groups[$group]['max']['time']) or $groups[$group]['max']['time'] < $total['time']) {
                 // Set the maximum time
                 $groups[$group]['max']['time'] = $total['time'];
             }
             if (!isset($groups[$group]['max']['memory']) or $groups[$group]['max']['memory'] < $total['memory']) {
                 // Set the maximum memory
                 $groups[$group]['max']['memory'] = $total['memory'];
             }
             // Increase the total time and memory
             $groups[$group]['total']['time'] += $total['time'];
             $groups[$group]['total']['memory'] += $total['memory'];
         }
         // Determine the number of names (subgroups)
         $count = count($names);
         // Determine the averages
         $groups[$group]['average']['time'] = $groups[$group]['total']['time'] / $count;
         $groups[$group]['average']['memory'] = $groups[$group]['total']['memory'] / $count;
     }
     return $groups;
 }
Example #3
0
        echo $key;
        ?>
"><?php 
        echo __(ucfirst($key));
        ?>
</th>
			<?php 
    }
    ?>
		</tr>
		<?php 
    foreach ($benchmarks as $name => $tokens) {
        ?>
		<tr class="mark time">
			<?php 
        $stats = Profiler::stats($tokens);
        ?>
			<th class="name" rowspan="2" scope="rowgroup"><?php 
        echo HTML::chars($name), ' (', count($tokens), ')';
        ?>
</th>
			<?php 
        foreach ($group_cols as $key) {
            ?>
			<td class="<?php 
            echo $key;
            ?>
">
				<div>
					<div class="value"><?php 
            echo number_format($stats[$key]['time'], 6);
Example #4
0
 /**
  * 返回框架调用信息
  *
  * @return array
  */
 public static function core_system()
 {
     $groups = self::groups();
     $token = $groups['Core']['PHP Framework Start'];
     $rs = Profiler::stats($token);
     $rs['file_count'] = Profiler::$_marks[$token[0]]['file_count'];
     return $rs;
 }
 /**
  * @return array|int|string
  */
 private static function getSql()
 {
     // calc explain
     if (self::cfg('html.showSqlExplain') && self::cfg('html.showSqlExplain')) {
         /*Перебераем все вкладки*/
         foreach (self::$_SQL as $instance => $query) {
             /**/
             foreach ($query as $sql => $data) {
                 if (stripos($sql, 'select') === 0) {
                     if (class_exists('\\Database\\ActiveRecord\\Record')) {
                         $pdo = \Database\ActiveRecord\Record::$db->getPdoInstance();
                         try {
                             /*
                             								$stmt = $pdo->prepare('EXPLAIN '.$sql);
                             								//$stmt->execute();
                             								$expl = $stmt->fetchAll(PDO::FETCH_ASSOC);*/
                         } catch (PDOException $e) {
                             self::addData($e, 'PDOException');
                         }
                     } else {
                         $expl = Database::instance($instance)->query(Database::SELECT, 'EXPLAIN ' . $sql)->as_array();
                     }
                     self::$_SQL[$instance][$sql]['explain'] = $expl;
                 }
             }
         }
     }
     // collect data
     $sql = [];
     $groups = Profiler::groups();
     foreach ($groups as $groupName => $benchmarks) {
         if (strpos($groupName, 'database') !== 0) {
             continue;
         }
         $sqlGroup = preg_replace('/(.*) \\(\'(.*)\'\\)/Usi', '$2', $groupName);
         /*$sqlGroup = substr($groupName, strpos($groupName, '(') + 2,
         				strpos($groupName, ')') - strpos($groupName, '(') - 2);
         */
         $sql[$sqlGroup] = ['data' => [], 'total' => ['time' => 0, 'memory' => 0, 'count' => 0]];
         foreach ($benchmarks as $benchName => $tokens) {
             foreach ($tokens as $token) {
                 $stats = Profiler::stats([$token]);
                 $sql_string = $benchName;
                 if (isset(self::$_SQL[$sqlGroup][$benchName]['data'])) {
                     if (strpos($sql_string, ":")) {
                         foreach (self::$_SQL[$sqlGroup][$benchName]['data'] as $key => $value) {
                             $sql_string = preg_replace('|' . $key . '|Usi', "'" . $value . "'", $sql_string);
                         }
                     }
                 }
                 $sql[$sqlGroup]['data'][] = ['sql' => $sql_string, 'time' => $stats['total']['time'], 'memory' => $stats['total']['memory'], 'rows' => isset(self::$_SQL[$sqlGroup][$benchName]) ? self::$_SQL[$sqlGroup][$benchName]['rows'] : null, 'explain' => isset(self::$_SQL[$sqlGroup][$benchName]) ? self::$_SQL[$sqlGroup][$benchName]['explain'] : null];
                 $sql[$sqlGroup]['total']['time'] += $stats['total']['time'];
                 $sql[$sqlGroup]['total']['memory'] += $stats['total']['memory'];
                 $sql[$sqlGroup]['total']['count']++;
             }
         }
     }
     return $sql;
 }
Example #6
0
 /**
  * Creates a formatted array of all Benchmarks
  *
  * @return array formatted benchmarks
  */
 public static function get_benchmarks()
 {
     if (Kohana::$profiling == FALSE) {
         return array();
     }
     if (self::$_benchmarks !== FALSE) {
         return self::$_benchmarks;
     }
     $groups = Profiler::groups();
     $result = array();
     foreach (array_keys($groups) as $group) {
         if (strpos($group, 'database (') === FALSE) {
             foreach ($groups[$group] as $name => $marks) {
                 $stats = Profiler::stats($marks);
                 $result[$group][] = array('name' => $name, 'count' => count($marks), 'total_time' => $stats['total']['time'], 'avg_time' => $stats['average']['time'], 'total_memory' => $stats['total']['memory'], 'avg_memory' => $stats['average']['memory']);
             }
         }
     }
     // add total stats
     $total = Profiler::application();
     $result['application'] = array('count' => 1, 'total_time' => $total['current']['time'], 'avg_time' => $total['average']['time'], 'total_memory' => $total['current']['memory'], 'avg_memory' => $total['average']['memory']);
     self::$_benchmarks = $result;
     return $result;
 }
 private static function getSql()
 {
     // calc explain
     if (self::cfg('html.showSqlExplain') && self::cfg('html.showSqlExplain')) {
         foreach (self::$_SQL as $instance => $query) {
             foreach ($query as $sql => $data) {
                 if (stripos($sql, 'select') === 0) {
                     $explain = Database::instance($instance)->query(Database::SELECT, 'EXPLAIN ' . $sql)->as_array();
                     self::$_SQL[$instance][$sql]['explain'] = $explain;
                 }
             }
         }
     }
     // collect data
     $sql = array();
     $groups = Profiler::groups();
     foreach ($groups as $groupName => $benchmarks) {
         if (strpos($groupName, 'database') !== 0) {
             continue;
         }
         $sqlGroup = substr($groupName, strpos($groupName, '(') + 1, strpos($groupName, ')') - strpos($groupName, '(') - 1);
         $sql[$sqlGroup] = array('data' => array(), 'total' => array('time' => 0, 'memory' => 0, 'count' => 0));
         foreach ($benchmarks as $benchName => $tokens) {
             // skip explain queries
             if (stripos(trim($benchName), 'explain') === 0) {
                 continue;
             }
             foreach ($tokens as $token) {
                 $stats = Profiler::stats(array($token));
                 $sql[$sqlGroup]['data'][] = array('sql' => $benchName, 'time' => $stats['total']['time'], 'memory' => $stats['total']['memory'], 'rows' => isset(self::$_SQL[$sqlGroup][$benchName]) ? self::$_SQL[$sqlGroup][$benchName]['rows'] : null, 'explain' => isset(self::$_SQL[$sqlGroup][$benchName]) ? self::$_SQL[$sqlGroup][$benchName]['explain'] : null);
                 $sql[$sqlGroup]['total']['time'] += $stats['total']['time'];
                 $sql[$sqlGroup]['total']['memory'] += $stats['total']['memory'];
                 $sql[$sqlGroup]['total']['count']++;
             }
         }
     }
     return $sql;
 }
Example #8
0
 /**
  * 返回分组对应的性能统计数据
  *
  * @param string $groups        	
  * @return array [min, max, average, total]
  * @uses Profiler::groups
  * @uses Profiler::stats
  * @example $stats = Profiler::group_stats('resize');
  */
 public static function group_stats($groups = NULL)
 {
     $groups = $groups === NULL ? Profiler::groups() : array_intersect_key(Profiler::groups(), array_flip((array) $groups));
     $stats = array();
     foreach ($groups as $group => $names) {
         foreach ($names as $name => $tokens) {
             $_stats = Profiler::stats($tokens);
             $stats[$group][$name] = $_stats['total'];
         }
     }
     $groups = array();
     foreach ($stats as $group => $names) {
         $groups[$group]['min'] = $groups[$group]['max'] = array('time' => NULL, 'memory' => NULL);
         $groups[$group]['total'] = array('time' => 0, 'memory' => 0);
         foreach ($names as $total) {
             if (!isset($groups[$group]['min']['time']) or $groups[$group]['min']['time'] > $total['time']) {
                 $groups[$group]['min']['time'] = $total['time'];
             }
             if (!isset($groups[$group]['min']['memory']) or $groups[$group]['min']['memory'] > $total['memory']) {
                 $groups[$group]['min']['memory'] = $total['memory'];
             }
             if (!isset($groups[$group]['max']['time']) or $groups[$group]['max']['time'] < $total['time']) {
                 $groups[$group]['max']['time'] = $total['time'];
             }
             if (!isset($groups[$group]['max']['memory']) or $groups[$group]['max']['memory'] < $total['memory']) {
                 $groups[$group]['max']['memory'] = $total['memory'];
             }
             $groups[$group]['total']['time'] += $total['time'];
             $groups[$group]['total']['memory'] += $total['memory'];
         }
         $count = count($names);
         $groups[$group]['average']['time'] = $groups[$group]['total']['time'] / $count;
         $groups[$group]['average']['memory'] = $groups[$group]['total']['memory'] / $count;
     }
     return $groups;
 }
Example #9
0
 /**
  * Get benchmark groups
  *
  * <code>
  * Debugger::get_benchmark_groups()
  * </code>
  *
  * @return array formatted benchmarks
  */
 public static function get_benchmark_groups()
 {
     if (Kohana::$profiling === false) {
         return array();
     }
     if (empty(Debugger::$_benchmark_groups) === false) {
         return Debugger::$_benchmark_groups;
     }
     $groups = Profiler::groups();
     $result = array();
     foreach (array_keys($groups) as $group) {
         if (strpos($group, 'database (') === false) {
             foreach ($groups[$group] as $name => $marks) {
                 $result[$group][] = array('name' => $name, 'count' => count($marks), 'stats' => Profiler::stats($marks));
             }
         }
     }
     return $result;
 }
Example #10
0
 public function benchmark($table = FALSE)
 {
     if ($this->enabled) {
         foreach (Profiler::groups() as $group => $benchmarks) {
             $tablename = ucfirst($group);
             // Exclude database unless specifically run
             if (empty($table) and strpos($tablename, 'Database') === FALSE or strpos($tablename, $table) === 0) {
                 $row = array(array(__('Benchmark'), __('Min'), __('Max'), __('Average'), __('Total')));
                 foreach ($benchmarks as $name => $tokens) {
                     $stats = Profiler::stats($tokens);
                     $cell = array($name . ' (' . count($tokens) . ')');
                     foreach (array('min', 'max', 'average', 'total') as $key) {
                         $cell[] = ' ' . number_format($stats[$key]['time'], 6) . ' ' . __('seconds');
                     }
                     $row[] = $cell;
                 }
                 $cell = array('');
                 foreach (array('min', 'max', 'average', 'total') as $key) {
                     $cell[] = ' ' . number_format($stats[$key]['memory'] / 1024, 4) . ' kb';
                 }
                 $row[] = $cell;
                 // Translate before passing...
                 $this->fb(array(__($tablename), $row), FirePHP::TABLE);
             }
         }
         if (empty($table) || strpos('Application', $table) === 0) {
             $stats = Profiler::application();
             $tablename = array(__('Application Execution') . ' (' . $stats['count'] . ')');
             $row = array(array('', 'min', 'max', 'average', 'current'));
             $cell = array('Time');
             foreach (array('min', 'max', 'average', 'current') as $key) {
                 $cell[] = number_format($stats[$key]['time'], 6) . ' ' . __('seconds');
             }
             $row[] = $cell;
             $cell = array('Memory');
             foreach (array('min', 'max', 'average', 'current') as $key) {
                 $cell[] = number_format($stats[$key]['memory'] / 1024, 4) . ' kb';
             }
             $row[] = $cell;
             $this->fb(array($tablename, $row), FirePHP::TABLE);
         }
     }
     return $this;
 }