/halogy/libraries/Profiler.php

https://bitbucket.org/haloweb/halogy-1.0/ · PHP · 392 lines · 224 code · 65 blank · 103 comment · 22 complexity · 038d469be8664e441223abbe9a941cb4 MD5 · raw file

  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP 4.3.2 or newer
  6. *
  7. * @package CodeIgniter
  8. * @author ExpressionEngine Dev Team
  9. * @copyright Copyright (c) 2008 - 2009, EllisLab, Inc.
  10. * @license http://codeigniter.com/user_guide/license.html
  11. * @link http://codeigniter.com
  12. * @since Version 1.0
  13. * @filesource
  14. */
  15. // ------------------------------------------------------------------------
  16. /**
  17. * CodeIgniter Profiler Class
  18. *
  19. * This class enables you to display benchmark, query, and other data
  20. * in order to help with debugging and optimization.
  21. *
  22. * Note: At some point it would be good to move all the HTML in this class
  23. * into a set of template files in order to allow customization.
  24. *
  25. * @package CodeIgniter
  26. * @subpackage Libraries
  27. * @category Libraries
  28. * @author ExpressionEngine Dev Team
  29. * @link http://codeigniter.com/user_guide/general/profiling.html
  30. */
  31. class CI_Profiler {
  32. var $CI;
  33. function CI_Profiler()
  34. {
  35. $this->CI =& get_instance();
  36. $this->CI->load->language('profiler');
  37. }
  38. // --------------------------------------------------------------------
  39. /**
  40. * Auto Profiler
  41. *
  42. * This function cycles through the entire array of mark points and
  43. * matches any two points that are named identically (ending in "_start"
  44. * and "_end" respectively). It then compiles the execution times for
  45. * all points and returns it as an array
  46. *
  47. * @access private
  48. * @return array
  49. */
  50. function _compile_benchmarks()
  51. {
  52. $profile = array();
  53. foreach ($this->CI->benchmark->marker as $key => $val)
  54. {
  55. // We match the "end" marker so that the list ends
  56. // up in the order that it was defined
  57. if (preg_match("/(.+?)_end/i", $key, $match))
  58. {
  59. if (isset($this->CI->benchmark->marker[$match[1].'_end']) AND isset($this->CI->benchmark->marker[$match[1].'_start']))
  60. {
  61. $profile[$match[1]] = $this->CI->benchmark->elapsed_time($match[1].'_start', $key);
  62. }
  63. }
  64. }
  65. // Build a table containing the profile data.
  66. // Note: At some point we should turn this into a template that can
  67. // be modified. We also might want to make this data available to be logged
  68. $output = "\n\n";
  69. $output .= '<fieldset style="border:1px solid #990000;padding:6px 10px 10px 10px;margin:0 0 20px 0;background-color:#eee">';
  70. $output .= "\n";
  71. $output .= '<legend style="color:#990000;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_benchmarks').'&nbsp;&nbsp;</legend>';
  72. $output .= "\n";
  73. $output .= "\n\n<table cellpadding='4' cellspacing='1' border='0' width='100%'>\n";
  74. foreach ($profile as $key => $val)
  75. {
  76. $key = ucwords(str_replace(array('_', '-'), ' ', $key));
  77. $output .= "<tr><td width='50%' style='color:#000;font-weight:bold;background-color:#ddd;'>".$key."&nbsp;&nbsp;</td><td width='50%' style='color:#990000;font-weight:normal;background-color:#ddd;'>".$val."</td></tr>\n";
  78. }
  79. $output .= "</table>\n";
  80. $output .= "</fieldset>";
  81. return $output;
  82. }
  83. // --------------------------------------------------------------------
  84. /**
  85. * Compile Queries
  86. *
  87. * @access private
  88. * @return string
  89. */
  90. function _compile_queries()
  91. {
  92. $dbs = array();
  93. // Let's determine which databases are currently connected to
  94. foreach (get_object_vars($this->CI) as $CI_object)
  95. {
  96. if (is_object($CI_object) && is_subclass_of(get_class($CI_object), 'CI_DB') )
  97. {
  98. $dbs[] = $CI_object;
  99. }
  100. }
  101. if (count($dbs) == 0)
  102. {
  103. $output = "\n\n";
  104. $output .= '<fieldset style="border:1px solid #0000FF;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
  105. $output .= "\n";
  106. $output .= '<legend style="color:#0000FF;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_queries').'&nbsp;&nbsp;</legend>';
  107. $output .= "\n";
  108. $output .= "\n\n<table cellpadding='4' cellspacing='1' border='0' width='100%'>\n";
  109. $output .="<tr><td width='100%' style='color:#0000FF;font-weight:normal;background-color:#eee;'>".$this->CI->lang->line('profiler_no_db')."</td></tr>\n";
  110. $output .= "</table>\n";
  111. $output .= "</fieldset>";
  112. return $output;
  113. }
  114. // Load the text helper so we can highlight the SQL
  115. $this->CI->load->helper('text');
  116. // Key words we want bolded
  117. $highlight = array('SELECT', 'DISTINCT', 'FROM', 'WHERE', 'AND', 'LEFT&nbsp;JOIN', 'ORDER&nbsp;BY', 'GROUP&nbsp;BY', 'LIMIT', 'INSERT', 'INTO', 'VALUES', 'UPDATE', 'OR', 'HAVING', 'OFFSET', 'NOT&nbsp;IN', 'IN', 'LIKE', 'NOT&nbsp;LIKE', 'COUNT', 'MAX', 'MIN', 'ON', 'AS', 'AVG', 'SUM', '(', ')');
  118. $output = "\n\n";
  119. foreach ($dbs as $db)
  120. {
  121. $output .= '<fieldset style="border:1px solid #0000FF;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
  122. $output .= "\n";
  123. $output .= '<legend style="color:#0000FF;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_database').':&nbsp; '.$db->database.'&nbsp;&nbsp;&nbsp;'.$this->CI->lang->line('profiler_queries').': '.count($this->CI->db->queries).'&nbsp;&nbsp;&nbsp;</legend>';
  124. $output .= "\n";
  125. $output .= "\n\n<table cellpadding='4' cellspacing='1' border='0' width='100%'>\n";
  126. if (count($db->queries) == 0)
  127. {
  128. $output .= "<tr><td width='100%' style='color:#0000FF;font-weight:normal;background-color:#eee;'>".$this->CI->lang->line('profiler_no_queries')."</td></tr>\n";
  129. }
  130. else
  131. {
  132. foreach ($db->queries as $key => $val)
  133. {
  134. $time = number_format($db->query_times[$key], 4);
  135. $val = highlight_code($val, ENT_QUOTES);
  136. foreach ($highlight as $bold)
  137. {
  138. $val = str_replace($bold, '<strong>'.$bold.'</strong>', $val);
  139. }
  140. $output .= "<tr><td width='1%' valign='top' style='color:#990000;font-weight:normal;background-color:#ddd;'>".$time."&nbsp;&nbsp;</td><td style='color:#000;font-weight:normal;background-color:#ddd;'>".$val."</td></tr>\n";
  141. }
  142. }
  143. $output .= "</table>\n";
  144. $output .= "</fieldset>";
  145. }
  146. return $output;
  147. }
  148. // --------------------------------------------------------------------
  149. /**
  150. * Compile $_GET Data
  151. *
  152. * @access private
  153. * @return string
  154. */
  155. function _compile_get()
  156. {
  157. $output = "\n\n";
  158. $output .= '<fieldset style="border:1px solid #cd6e00;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
  159. $output .= "\n";
  160. $output .= '<legend style="color:#cd6e00;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_get_data').'&nbsp;&nbsp;</legend>';
  161. $output .= "\n";
  162. if (count($_GET) == 0)
  163. {
  164. $output .= "<div style='color:#cd6e00;font-weight:normal;padding:4px 0 4px 0'>".$this->CI->lang->line('profiler_no_get')."</div>";
  165. }
  166. else
  167. {
  168. $output .= "\n\n<table cellpadding='4' cellspacing='1' border='0' width='100%'>\n";
  169. foreach ($_GET as $key => $val)
  170. {
  171. if ( ! is_numeric($key))
  172. {
  173. $key = "'".$key."'";
  174. }
  175. $output .= "<tr><td width='50%' style='color:#000;background-color:#ddd;'>&#36;_GET[".$key."]&nbsp;&nbsp; </td><td width='50%' style='color:#cd6e00;font-weight:normal;background-color:#ddd;'>";
  176. if (is_array($val))
  177. {
  178. $output .= "<pre>" . htmlspecialchars(stripslashes(print_r($val, true))) . "</pre>";
  179. }
  180. else
  181. {
  182. $output .= htmlspecialchars(stripslashes($val));
  183. }
  184. $output .= "</td></tr>\n";
  185. }
  186. $output .= "</table>\n";
  187. }
  188. $output .= "</fieldset>";
  189. return $output;
  190. }
  191. // --------------------------------------------------------------------
  192. /**
  193. * Compile $_POST Data
  194. *
  195. * @access private
  196. * @return string
  197. */
  198. function _compile_post()
  199. {
  200. $output = "\n\n";
  201. $output .= '<fieldset style="border:1px solid #009900;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
  202. $output .= "\n";
  203. $output .= '<legend style="color:#009900;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_post_data').'&nbsp;&nbsp;</legend>';
  204. $output .= "\n";
  205. if (count($_POST) == 0)
  206. {
  207. $output .= "<div style='color:#009900;font-weight:normal;padding:4px 0 4px 0'>".$this->CI->lang->line('profiler_no_post')."</div>";
  208. }
  209. else
  210. {
  211. $output .= "\n\n<table cellpadding='4' cellspacing='1' border='0' width='100%'>\n";
  212. foreach ($_POST as $key => $val)
  213. {
  214. if ( ! is_numeric($key))
  215. {
  216. $key = "'".$key."'";
  217. }
  218. $output .= "<tr><td width='50%' style='color:#000;background-color:#ddd;'>&#36;_POST[".$key."]&nbsp;&nbsp; </td><td width='50%' style='color:#009900;font-weight:normal;background-color:#ddd;'>";
  219. if (is_array($val))
  220. {
  221. $output .= "<pre>" . htmlspecialchars(stripslashes(print_r($val, true))) . "</pre>";
  222. }
  223. else
  224. {
  225. $output .= htmlspecialchars(stripslashes($val));
  226. }
  227. $output .= "</td></tr>\n";
  228. }
  229. $output .= "</table>\n";
  230. }
  231. $output .= "</fieldset>";
  232. return $output;
  233. }
  234. // --------------------------------------------------------------------
  235. /**
  236. * Show query string
  237. *
  238. * @access private
  239. * @return string
  240. */
  241. function _compile_uri_string()
  242. {
  243. $output = "\n\n";
  244. $output .= '<fieldset style="border:1px solid #000;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
  245. $output .= "\n";
  246. $output .= '<legend style="color:#000;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_uri_string').'&nbsp;&nbsp;</legend>';
  247. $output .= "\n";
  248. if ($this->CI->uri->uri_string == '')
  249. {
  250. $output .= "<div style='color:#000;font-weight:normal;padding:4px 0 4px 0'>".$this->CI->lang->line('profiler_no_uri')."</div>";
  251. }
  252. else
  253. {
  254. $output .= "<div style='color:#000;font-weight:normal;padding:4px 0 4px 0'>".$this->CI->uri->uri_string."</div>";
  255. }
  256. $output .= "</fieldset>";
  257. return $output;
  258. }
  259. // --------------------------------------------------------------------
  260. /**
  261. * Show the controller and function that were called
  262. *
  263. * @access private
  264. * @return string
  265. */
  266. function _compile_controller_info()
  267. {
  268. $output = "\n\n";
  269. $output .= '<fieldset style="border:1px solid #995300;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
  270. $output .= "\n";
  271. $output .= '<legend style="color:#995300;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_controller_info').'&nbsp;&nbsp;</legend>';
  272. $output .= "\n";
  273. $output .= "<div style='color:#995300;font-weight:normal;padding:4px 0 4px 0'>".$this->CI->router->fetch_class()."/".$this->CI->router->fetch_method()."</div>";
  274. $output .= "</fieldset>";
  275. return $output;
  276. }
  277. // --------------------------------------------------------------------
  278. /**
  279. * Compile memory usage
  280. *
  281. * Display total used memory
  282. *
  283. * @access public
  284. * @return string
  285. */
  286. function _compile_memory_usage()
  287. {
  288. $output = "\n\n";
  289. $output .= '<fieldset style="border:1px solid #5a0099;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
  290. $output .= "\n";
  291. $output .= '<legend style="color:#5a0099;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_memory_usage').'&nbsp;&nbsp;</legend>';
  292. $output .= "\n";
  293. if (function_exists('memory_get_usage') && ($usage = memory_get_usage()) != '')
  294. {
  295. $output .= "<div style='color:#5a0099;font-weight:normal;padding:4px 0 4px 0'>".number_format($usage).' bytes</div>';
  296. }
  297. else
  298. {
  299. $output .= "<div style='color:#5a0099;font-weight:normal;padding:4px 0 4px 0'>".$this->CI->lang->line('profiler_no_memory_usage')."</div>";
  300. }
  301. $output .= "</fieldset>";
  302. return $output;
  303. }
  304. // --------------------------------------------------------------------
  305. /**
  306. * Run the Profiler
  307. *
  308. * @access private
  309. * @return string
  310. */
  311. function run()
  312. {
  313. $output = "<div id='codeigniter_profiler' style='clear:both;background-color:#fff;padding:10px;'>";
  314. $output .= $this->_compile_uri_string();
  315. $output .= $this->_compile_controller_info();
  316. $output .= $this->_compile_memory_usage();
  317. $output .= $this->_compile_benchmarks();
  318. $output .= $this->_compile_get();
  319. $output .= $this->_compile_post();
  320. $output .= $this->_compile_queries();
  321. $output .= '</div>';
  322. return $output;
  323. }
  324. }
  325. // END CI_Profiler class
  326. /* End of file Profiler.php */
  327. /* Location: ./system/libraries/Profiler.php */