1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 | <?php /* - - - - - - - - - - - - - - - - - - - - - Title : PHP Quick Profiler Class Author : Created by Ryan Campbell URL : http://particletree.com/features/php-quick-profiler/ Last Updated : April 22, 2009 Description : This class processes the logs and organizes the data for output to the browser. Initialize this class with a start time at the beginning of your code, and then call the display method when your code is terminating. - - - - - - - - - - - - - - - - - - - - - */ class PhpQuickProfiler { public $output = array(); public $config = ''; public function __construct($startTime, $config = 'pqp/') { $this->startTime = $startTime; $this->config = $config; require_once($config.'classes/Console.php'); } /*------------------------------------------- FORMAT THE DIFFERENT TYPES OF LOGS -------------------------------------------*/ public function gatherConsoleData() { $logs = Console::getLogs(); if($logs['console']) { foreach($logs['console'] as $key => $log) { if($log['type'] == 'log') { $logs['console'][$key]['data'] = print_r($log['data'], true); } elseif($log['type'] == 'memory') { $logs['console'][$key]['data'] = $this->getReadableFileSize($log['data']); } elseif($log['type'] == 'speed') { $logs['console'][$key]['data'] = $this->getReadableTime(($log['data'] - $this->startTime)*1000); } } } $this->output['logs'] = $logs; } /*------------------------------------------- AGGREGATE DATA ON THE FILES INCLUDED -------------------------------------------*/ public function gatherFileData() { $files = get_included_files(); $fileList = array(); $fileTotals = array( "count" => count($files), "size" => 0, "largest" => 0, ); foreach($files as $key => $file) { $size = filesize($file); $fileList[] = array( 'name' => $file, 'size' => $this->getReadableFileSize($size) ); $fileTotals['size'] += $size; if($size > $fileTotals['largest']) $fileTotals['largest'] = $size; } $fileTotals['size'] = $this->getReadableFileSize($fileTotals['size']); $fileTotals['largest'] = $this->getReadableFileSize($fileTotals['largest']); $this->output['files'] = $fileList; $this->output['fileTotals'] = $fileTotals; } /*------------------------------------------- MEMORY USAGE AND MEMORY AVAILABLE -------------------------------------------*/ public function gatherMemoryData() { $memoryTotals = array(); $memoryTotals['used'] = $this->getReadableFileSize(memory_get_peak_usage()); $memoryTotals['total'] = ini_get("memory_limit"); $this->output['memoryTotals'] = $memoryTotals; } /*-------------------------------------------------------- QUERY DATA -- DATABASE OBJECT WITH LOGGING REQUIRED ----------------------------------------------------------*/ public function gatherQueryData() { $queryTotals = array(); $queryTotals['count'] = 0; $queryTotals['time'] = 0; $queries = array(); if($this->db != '') { $queryTotals['count'] += $this->db->queryCount; foreach($this->db->queries as $key => $query) { $query = $this->attemptToExplainQuery($query); $queryTotals['time'] += $query['time']; $query['time'] = $this->getReadableTime($query['time']); $queries[] = $query; } } $queryTotals['time'] = $this->getReadableTime($queryTotals['time']); $this->output['queries'] = $queries; $this->output['queryTotals'] = $queryTotals; } /*-------------------------------------------------------- CALL SQL EXPLAIN ON THE QUERY TO FIND MORE INFO ----------------------------------------------------------*/ function attemptToExplainQuery($query) { try { $sql = 'EXPLAIN '.$query['sql']; $rs = $this->db->query($sql); } catch(Exception $e) {} if($rs) { $row = mysql_fetch_array($rs, MYSQL_ASSOC); $query['explain'] = $row; } return $query; } /*------------------------------------------- SPEED DATA FOR ENTIRE PAGE LOAD -------------------------------------------*/ public function gatherSpeedData() { $speedTotals = array(); $speedTotals['total'] = $this->getReadableTime(($this->getMicroTime() - $this->startTime)*1000); $speedTotals['allowed'] = ini_get("max_execution_time"); $this->output['speedTotals'] = $speedTotals; } /*------------------------------------------- HELPER FUNCTIONS TO FORMAT DATA -------------------------------------------*/ function getMicroTime() { $time = microtime(); $time = explode(' ', $time); return $time[1] + $time[0]; } public function getReadableFileSize($size, $retstring = null) { // adapted from code at http://aidanlister.com/repos/v/function.size_readable.php $sizes = array('bytes', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'); if ($retstring === null) { $retstring = '%01.2f %s'; } $lastsizestring = end($sizes); foreach ($sizes as $sizestring) { if ($size < 1024) { break; } if ($sizestring != $lastsizestring) { $size /= 1024; } } if ($sizestring == $sizes[0]) { $retstring = '%01d %s'; } // Bytes aren't normally fractional return sprintf($retstring, $size, $sizestring); } public function getReadableTime($time) { $ret = $time; $formatter = 0; $formats = array('ms', 's', 'm'); if($time >= 1000 && $time < 60000) { $formatter = 1; $ret = ($time / 1000); } if($time >= 60000) { $formatter = 2; $ret = ($time / 1000) / 60; } $ret = number_format($ret,3,'.','') . ' ' . $formats[$formatter]; return $ret; } /*--------------------------------------------------------- DISPLAY TO THE SCREEN -- CALL WHEN CODE TERMINATING -----------------------------------------------------------*/ public function display($db = '', $master_db = '') { $this->db = $db; $this->master_db = $master_db; $this->gatherConsoleData(); $this->gatherFileData(); $this->gatherMemoryData(); $this->gatherQueryData(); $this->gatherSpeedData(); require_once($this->config.'display.php'); displayPqp($this->output, OWA_PUBLIC_URL.'includes/pqp/'); } } ?> |