PageRenderTime 23ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/convert/system/libraries/Profiler.php

https://github.com/usagi-project/mynets1
PHP | 343 lines | 199 code | 52 blank | 92 comment | 19 complexity | c78b96f5f6b99087657ec6312c603a25 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) 2006, 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. $output = "\n\n";
  93. $output .= '<fieldset style="border:1px solid #0000FF;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
  94. $output .= "\n";
  95. if ( ! class_exists('CI_DB_driver'))
  96. {
  97. $output .= '<legend style="color:#0000FF;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_queries').'&nbsp;&nbsp;</legend>';
  98. $output .= "\n";
  99. $output .= "\n\n<table cellpadding='4' cellspacing='1' border='0' width='100%'>\n";
  100. $output .="<tr><td width='100%' style='color:#0000FF;font-weight:normal;background-color:#eee;'>".$this->CI->lang->line('profiler_no_db')."</td></tr>\n";
  101. }
  102. else
  103. {
  104. $output .= '<legend style="color:#0000FF;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_queries').' ('.count($this->CI->db->queries).')&nbsp;&nbsp;</legend>';
  105. $output .= "\n";
  106. $output .= "\n\n<table cellpadding='4' cellspacing='1' border='0' width='100%'>\n";
  107. if (count($this->CI->db->queries) == 0)
  108. {
  109. $output .= "<tr><td width='100%' style='color:#0000FF;font-weight:normal;background-color:#eee;'>".$this->CI->lang->line('profiler_no_queries')."</td></tr>\n";
  110. }
  111. else
  112. {
  113. $highlight = array('SELECT', 'FROM', 'WHERE', 'AND', 'LEFT JOIN', 'ORDER BY', 'LIMIT', 'INSERT', 'INTO', 'VALUES', 'UPDATE', 'OR');
  114. foreach ($this->CI->db->queries as $key => $val)
  115. {
  116. $val = htmlspecialchars($val, ENT_QUOTES);
  117. $time = number_format($this->CI->db->query_times[$key], 4);
  118. foreach ($highlight as $bold)
  119. {
  120. $val = str_replace($bold, '<strong>'.$bold.'</strong>', $val);
  121. }
  122. $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";
  123. }
  124. }
  125. }
  126. $output .= "</table>\n";
  127. $output .= "</fieldset>";
  128. return $output;
  129. }
  130. // --------------------------------------------------------------------
  131. /**
  132. * Compile $_GET Data
  133. *
  134. * @access private
  135. * @return string
  136. */
  137. function _compile_get()
  138. {
  139. $output = "\n\n";
  140. $output .= '<fieldset style="border:1px solid #cd6e00;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
  141. $output .= "\n";
  142. $output .= '<legend style="color:#cd6e00;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_get_data').'&nbsp;&nbsp;</legend>';
  143. $output .= "\n";
  144. if (count($_GET) == 0)
  145. {
  146. $output .= "<div style='color:#cd6e00;font-weight:normal;padding:4px 0 4px 0'>".$this->CI->lang->line('profiler_no_get')."</div>";
  147. }
  148. else
  149. {
  150. $output .= "\n\n<table cellpadding='4' cellspacing='1' border='0' width='100%'>\n";
  151. foreach ($_GET as $key => $val)
  152. {
  153. if ( ! is_numeric($key))
  154. {
  155. $key = "'".$key."'";
  156. }
  157. $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;'>";
  158. if (is_array($val))
  159. {
  160. $output .= "<pre>" . htmlspecialchars(stripslashes(print_r($val, true))) . "</pre>";
  161. }
  162. else
  163. {
  164. $output .= htmlspecialchars(stripslashes($val));
  165. }
  166. $output .= "</td></tr>\n";
  167. }
  168. $output .= "</table>\n";
  169. }
  170. $output .= "</fieldset>";
  171. return $output;
  172. }
  173. // --------------------------------------------------------------------
  174. /**
  175. * Compile $_POST Data
  176. *
  177. * @access private
  178. * @return string
  179. */
  180. function _compile_post()
  181. {
  182. $output = "\n\n";
  183. $output .= '<fieldset style="border:1px solid #009900;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
  184. $output .= "\n";
  185. $output .= '<legend style="color:#009900;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_post_data').'&nbsp;&nbsp;</legend>';
  186. $output .= "\n";
  187. if (count($_POST) == 0)
  188. {
  189. $output .= "<div style='color:#009900;font-weight:normal;padding:4px 0 4px 0'>".$this->CI->lang->line('profiler_no_post')."</div>";
  190. }
  191. else
  192. {
  193. $output .= "\n\n<table cellpadding='4' cellspacing='1' border='0' width='100%'>\n";
  194. foreach ($_POST as $key => $val)
  195. {
  196. if ( ! is_numeric($key))
  197. {
  198. $key = "'".$key."'";
  199. }
  200. // $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;'>".htmlspecialchars(stripslashes($val))."</td></tr>\n";
  201. $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;'>";
  202. if (is_array($val))
  203. {
  204. $output .= "<pre>" . htmlspecialchars(stripslashes(print_r($val, true))) . "</pre>";
  205. }
  206. else
  207. {
  208. $output .= htmlspecialchars(stripslashes($val));
  209. }
  210. $output .= "</td></tr>\n";
  211. }
  212. $output .= "</table>\n";
  213. }
  214. $output .= "</fieldset>";
  215. return $output;
  216. }
  217. // --------------------------------------------------------------------
  218. /**
  219. * Show query string
  220. *
  221. * @access private
  222. * @return string
  223. */
  224. function _compile_uri_string()
  225. {
  226. $output = "\n\n";
  227. $output .= '<fieldset style="border:1px solid #000;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
  228. $output .= "\n";
  229. $output .= '<legend style="color:#000;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_uri_string').'&nbsp;&nbsp;</legend>';
  230. $output .= "\n";
  231. if ($this->CI->uri->uri_string == '')
  232. {
  233. $output .= "<div style='color:#000;font-weight:normal;padding:4px 0 4px 0'>".$this->CI->lang->line('profiler_no_uri')."</div>";
  234. }
  235. else
  236. {
  237. $output .= "<div style='color:#000;font-weight:normal;padding:4px 0 4px 0'>".$this->CI->uri->uri_string."</div>";
  238. }
  239. $output .= "</fieldset>";
  240. return $output;
  241. }
  242. // --------------------------------------------------------------------
  243. /**
  244. * Compile memory usage
  245. *
  246. * Display total used memory
  247. *
  248. * @access public
  249. * @return string
  250. */
  251. function _compile_memory_usage()
  252. {
  253. $output = "\n\n";
  254. $output .= '<fieldset style="border:1px solid #000;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
  255. $output .= "\n";
  256. $output .= '<legend style="color:#000;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_memory_usage').'&nbsp;&nbsp;</legend>';
  257. $output .= "\n";
  258. if (function_exists('memory_get_usage') && ($usage = memory_get_usage()) != '')
  259. {
  260. $output .= "<div style='color:#000;font-weight:normal;padding:4px 0 4px 0'>".number_format($usage).' bytes</div>';
  261. }
  262. else
  263. {
  264. $output .= "<div style='color:#000;font-weight:normal;padding:4px 0 4px 0'>".$this->CI->lang->line('profiler_no_memory_usage')."</div>";
  265. }
  266. $output .= "</fieldset>";
  267. return $output;
  268. }
  269. // --------------------------------------------------------------------
  270. /**
  271. * Run the Profiler
  272. *
  273. * @access private
  274. * @return string
  275. */
  276. function run()
  277. {
  278. $output = '<br clear="all" />';
  279. $output .= "<div style='background-color:#fff;padding:10px;'>";
  280. $output .= $this->_compile_memory_usage();
  281. $output .= $this->_compile_benchmarks();
  282. $output .= $this->_compile_uri_string();
  283. $output .= $this->_compile_get();
  284. $output .= $this->_compile_post();
  285. $output .= $this->_compile_queries();
  286. $output .= '</div>';
  287. return $output;
  288. }
  289. }
  290. // END CI_Profiler class
  291. ?>