00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 class Varien_Profiler
00029 {
00030
00031
00032
00033
00034
00035
00036 static private $_timers = array();
00037 static private $_enabled = false;
00038 static private $_memory_get_usage = false;
00039
00040 public static function enable()
00041 {
00042 self::$_enabled = true;
00043 self::$_memory_get_usage = function_exists('memory_get_usage');
00044 }
00045
00046 public static function disable()
00047 {
00048 self::$_enabled = false;
00049 }
00050
00051 public static function reset($timerName)
00052 {
00053 self::$_timers[$timerName] = array(
00054 'start'=>false,
00055 'count'=>0,
00056 'sum'=>0,
00057 'realmem'=>0,
00058 'emalloc'=>0,
00059 );
00060 }
00061
00062 public static function resume($timerName)
00063 {
00064 if (!self::$_enabled) {
00065 return;
00066 }
00067
00068 if (empty(self::$_timers[$timerName])) {
00069 self::reset($timerName);
00070 }
00071 if (self::$_memory_get_usage) {
00072 self::$_timers[$timerName]['realmem_start'] = memory_get_usage(true);
00073 self::$_timers[$timerName]['emalloc_start'] = memory_get_usage();
00074 }
00075 self::$_timers[$timerName]['start'] = microtime(true);
00076 self::$_timers[$timerName]['count'] ++;
00077 }
00078
00079 public static function start($timerName)
00080 {
00081 self::resume($timerName);
00082 }
00083
00084 public static function pause($timerName)
00085 {
00086 if (!self::$_enabled) {
00087 return;
00088 }
00089
00090 if (empty(self::$_timers[$timerName])) {
00091 self::reset($timerName);
00092 }
00093 if (false!==self::$_timers[$timerName]['start']) {
00094 self::$_timers[$timerName]['sum'] += microtime(true)-self::$_timers[$timerName]['start'];
00095 self::$_timers[$timerName]['start'] = false;
00096 if (self::$_memory_get_usage) {
00097 self::$_timers[$timerName]['realmem'] += memory_get_usage(true)-self::$_timers[$timerName]['realmem_start'];
00098 self::$_timers[$timerName]['emalloc'] += memory_get_usage()-self::$_timers[$timerName]['emalloc_start'];
00099 }
00100 }
00101 }
00102
00103 public static function stop($timerName)
00104 {
00105 self::pause($timerName);
00106 }
00107
00108 public static function fetch($timerName, $key='sum')
00109 {
00110 if (empty(self::$_timers[$timerName])) {
00111 return false;
00112 } elseif (empty($key)) {
00113 return self::$_timers[$timerName];
00114 }
00115 switch ($key) {
00116 case 'sum':
00117 $sum = self::$_timers[$timerName]['sum'];
00118 if (self::$_timers[$timerName]['start']!==false) {
00119 $sum += microtime(true)-self::$_timers[$timerName]['start'];
00120 }
00121 return $sum;
00122
00123 case 'count':
00124 $count = self::$_timers[$timerName]['count'];
00125 return $count;
00126
00127 case 'realmem':
00128 if (!isset(self::$_timers[$timerName]['realmem'])) {
00129 self::$_timers[$timerName]['realmem'] = -1;
00130 }
00131 return self::$_timers[$timerName]['realmem'];
00132
00133 case 'emalloc':
00134 if (!isset(self::$_timers[$timerName]['emalloc'])) {
00135 self::$_timers[$timerName]['emalloc'] = -1;
00136 }
00137 return self::$_timers[$timerName]['emalloc'];
00138
00139 default:
00140 if (!empty(self::$_timers[$timerName][$key])) {
00141 return self::$_timers[$timerName][$key];
00142 }
00143 }
00144 return false;
00145 }
00146
00147 public static function getTimers()
00148 {
00149 return self::$_timers;
00150 }
00151
00152
00153
00154
00155
00156 public static function getSqlProfiler($res) {
00157 if(!$res){
00158 return '';
00159 }
00160 $out = '';
00161 $profiler = $res->getProfiler();
00162 if($profiler->getEnabled()) {
00163 $totalTime = $profiler->getTotalElapsedSecs();
00164 $queryCount = $profiler->getTotalNumQueries();
00165 $longestTime = 0;
00166 $longestQuery = null;
00167
00168 foreach ($profiler->getQueryProfiles() as $query) {
00169 if ($query->getElapsedSecs() > $longestTime) {
00170 $longestTime = $query->getElapsedSecs();
00171 $longestQuery = $query->getQuery();
00172 }
00173 }
00174
00175 $out .= 'Executed ' . $queryCount . ' queries in ' . $totalTime . ' seconds' . "<br>";
00176 $out .= 'Average query length: ' . $totalTime / $queryCount . ' seconds' . "<br>";
00177 $out .= 'Queries per second: ' . $queryCount / $totalTime . "<br>";
00178 $out .= 'Longest query length: ' . $longestTime . "<br>";
00179 $out .= 'Longest query: <br>' . $longestQuery . "<hr>";
00180 }
00181 return $out;
00182 }
00183 }