PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/fuel/core/vendor/phpquickprofiler/phpquickprofiler.php

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