admin | d13ff07 | 2006-10-03 03:40:45 +0000 | [diff] [blame] | 1 | <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); |
| 2 | /** |
| 3 | * Code Igniter |
| 4 | * |
| 5 | * An open source application development framework for PHP 4.3.2 or newer |
| 6 | * |
| 7 | * @package CodeIgniter |
| 8 | * @author Rick Ellis |
| 9 | * @copyright Copyright (c) 2006, pMachine, Inc. |
| 10 | * @license http://www.codeignitor.com/user_guide/license.html |
| 11 | * @link http://www.codeigniter.com |
| 12 | * @since Version 1.0 |
| 13 | * @filesource |
| 14 | */ |
| 15 | |
| 16 | // ------------------------------------------------------------------------ |
| 17 | |
| 18 | /** |
| 19 | * Code Igniter Profiler Class |
| 20 | * |
| 21 | * This class enables you to display benchmark, query, and other data |
admin | 1c5ef5e | 2006-10-03 04:50:09 +0000 | [diff] [blame] | 22 | * in order to help with debugging and optimization. |
| 23 | * |
| 24 | * Note: At some point it would be good to move all the HTML in this class |
| 25 | * into a set of template files in order to allow customization. |
admin | d13ff07 | 2006-10-03 03:40:45 +0000 | [diff] [blame] | 26 | * |
| 27 | * @package CodeIgniter |
| 28 | * @subpackage Libraries |
| 29 | * @category Libraries |
| 30 | * @author Rick Ellis |
| 31 | * @link http://www.codeigniter.com/user_guide/libraries/benchmark.html |
| 32 | */ |
| 33 | class CI_Profiler { |
| 34 | |
admin | b3ab70b | 2006-10-07 03:07:29 +0000 | [diff] [blame] | 35 | var $CI; |
admin | 90a7b9d | 2006-10-03 04:48:39 +0000 | [diff] [blame] | 36 | |
| 37 | function CI_Profiler() |
| 38 | { |
admin | b3ab70b | 2006-10-07 03:07:29 +0000 | [diff] [blame] | 39 | $this->CI =& get_instance(); |
| 40 | $this->CI->load->language('profiler'); |
admin | 90a7b9d | 2006-10-03 04:48:39 +0000 | [diff] [blame] | 41 | } |
admin | d13ff07 | 2006-10-03 03:40:45 +0000 | [diff] [blame] | 42 | |
| 43 | // -------------------------------------------------------------------- |
| 44 | |
| 45 | /** |
| 46 | * Auto Profiler |
| 47 | * |
| 48 | * This function cycles through the entire array of mark points and |
admin | bd6bee7 | 2006-10-21 19:39:00 +0000 | [diff] [blame^] | 49 | * matches any two points that are named identically (ending in "_start" |
admin | d13ff07 | 2006-10-03 03:40:45 +0000 | [diff] [blame] | 50 | * and "_end" respectively). It then compiles the execution times for |
| 51 | * all points and returns it as an array |
| 52 | * |
| 53 | * @access private |
| 54 | * @return array |
| 55 | */ |
| 56 | function _compile_benchmarks() |
| 57 | { |
admin | 90a7b9d | 2006-10-03 04:48:39 +0000 | [diff] [blame] | 58 | $profile = array(); |
admin | b3ab70b | 2006-10-07 03:07:29 +0000 | [diff] [blame] | 59 | foreach ($this->CI->benchmark->marker as $key => $val) |
admin | d13ff07 | 2006-10-03 03:40:45 +0000 | [diff] [blame] | 60 | { |
| 61 | // We match the "end" marker so that the list ends |
| 62 | // up in the order that it was defined |
| 63 | if (preg_match("/(.+?)_end/i", $key, $match)) |
| 64 | { |
admin | b3ab70b | 2006-10-07 03:07:29 +0000 | [diff] [blame] | 65 | if (isset($this->CI->benchmark->marker[$match[1].'_end']) AND isset($this->CI->benchmark->marker[$match[1].'_start'])) |
admin | d13ff07 | 2006-10-03 03:40:45 +0000 | [diff] [blame] | 66 | { |
admin | b3ab70b | 2006-10-07 03:07:29 +0000 | [diff] [blame] | 67 | $profile[$match[1]] = $this->CI->benchmark->elapsed_time($match[1].'_start', $key); |
admin | d13ff07 | 2006-10-03 03:40:45 +0000 | [diff] [blame] | 68 | } |
| 69 | } |
| 70 | } |
admin | 90a7b9d | 2006-10-03 04:48:39 +0000 | [diff] [blame] | 71 | |
| 72 | // Build a table containing the profile data. |
| 73 | // Note: At some point we should turn this into a template that can |
| 74 | // be modified. We also might want to make this data available to be logged |
| 75 | |
| 76 | $output = "\n\n"; |
admin | a813a46 | 2006-10-03 19:17:41 +0000 | [diff] [blame] | 77 | $output .= '<fieldset style="border:1px solid #990000;padding:6px 10px 10px 10px;margin:0 0 20px 0;background-color:#eee">'; |
admin | 90a7b9d | 2006-10-03 04:48:39 +0000 | [diff] [blame] | 78 | $output .= "\n"; |
admin | b3ab70b | 2006-10-07 03:07:29 +0000 | [diff] [blame] | 79 | $output .= '<legend style="color:#990000;"> '.$this->CI->lang->line('profiler_benchmarks').' </legend>'; |
admin | 90a7b9d | 2006-10-03 04:48:39 +0000 | [diff] [blame] | 80 | $output .= "\n"; |
admin | a813a46 | 2006-10-03 19:17:41 +0000 | [diff] [blame] | 81 | $output .= "\n\n<table cellpadding='4' cellspacing='1' border='0' width='100%'>\n"; |
admin | 90a7b9d | 2006-10-03 04:48:39 +0000 | [diff] [blame] | 82 | |
| 83 | foreach ($profile as $key => $val) |
| 84 | { |
| 85 | $key = ucwords(str_replace(array('_', '-'), ' ', $key)); |
admin | a813a46 | 2006-10-03 19:17:41 +0000 | [diff] [blame] | 86 | $output .= "<tr><td width='50%' style='color:#0000;font-weight:bold;background-color:#ddd;'>".$key." </td><td width='50%' style='color:#990000;font-weight:normal;background-color:#ddd;'>".$val."</td></tr>\n"; |
admin | 90a7b9d | 2006-10-03 04:48:39 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | $output .= "</table>\n"; |
admin | a813a46 | 2006-10-03 19:17:41 +0000 | [diff] [blame] | 90 | $output .= "</fieldset>"; |
admin | 90a7b9d | 2006-10-03 04:48:39 +0000 | [diff] [blame] | 91 | |
| 92 | return $output; |
admin | d13ff07 | 2006-10-03 03:40:45 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | // -------------------------------------------------------------------- |
| 96 | |
admin | b06d69f | 2006-10-13 21:44:17 +0000 | [diff] [blame] | 97 | /** |
| 98 | * Compile Queries |
| 99 | * |
| 100 | * @access private |
| 101 | * @return string |
| 102 | */ |
admin | 90a7b9d | 2006-10-03 04:48:39 +0000 | [diff] [blame] | 103 | function _compile_queries() |
| 104 | { |
| 105 | $output = "\n\n"; |
admin | a813a46 | 2006-10-03 19:17:41 +0000 | [diff] [blame] | 106 | $output .= '<fieldset style="border:1px solid #0000FF;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">'; |
admin | 90a7b9d | 2006-10-03 04:48:39 +0000 | [diff] [blame] | 107 | $output .= "\n"; |
admin | b3ab70b | 2006-10-07 03:07:29 +0000 | [diff] [blame] | 108 | $output .= '<legend style="color:#0000FF;"> '.$this->CI->lang->line('profiler_queries').' </legend>'; |
admin | 90a7b9d | 2006-10-03 04:48:39 +0000 | [diff] [blame] | 109 | $output .= "\n"; |
| 110 | |
| 111 | if ( ! class_exists('CI_DB_driver')) |
| 112 | { |
admin | b3ab70b | 2006-10-07 03:07:29 +0000 | [diff] [blame] | 113 | $output .= "<div style='color:#0000FF;font-weight:normal;padding:4px 0 0 0;'>".$this->CI->lang->line('profiler_no_db')."</div>"; |
admin | 90a7b9d | 2006-10-03 04:48:39 +0000 | [diff] [blame] | 114 | } |
| 115 | else |
| 116 | { |
admin | b3ab70b | 2006-10-07 03:07:29 +0000 | [diff] [blame] | 117 | if (count($this->CI->db->queries) == 0) |
admin | 90a7b9d | 2006-10-03 04:48:39 +0000 | [diff] [blame] | 118 | { |
admin | b3ab70b | 2006-10-07 03:07:29 +0000 | [diff] [blame] | 119 | $output .= "<div style='color:#0000FF;font-weight:normal;padding:4px 0 4px 0;'>".$this->CI->lang->line('profiler_no_queries')."</div>"; |
admin | 90a7b9d | 2006-10-03 04:48:39 +0000 | [diff] [blame] | 120 | } |
| 121 | else |
| 122 | { |
admin | b3ab70b | 2006-10-07 03:07:29 +0000 | [diff] [blame] | 123 | foreach ($this->CI->db->queries as $val) |
admin | 90a7b9d | 2006-10-03 04:48:39 +0000 | [diff] [blame] | 124 | { |
admin | a813a46 | 2006-10-03 19:17:41 +0000 | [diff] [blame] | 125 | $output .= '<div style="padding:3px;margin:12px 0 12px 0;background-color:#ddd;color:#000">'; |
admin | 90a7b9d | 2006-10-03 04:48:39 +0000 | [diff] [blame] | 126 | $output .= $val; |
| 127 | $output .= "</div>\n"; |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | |
admin | a813a46 | 2006-10-03 19:17:41 +0000 | [diff] [blame] | 132 | $output .= "</fieldset>"; |
admin | 90a7b9d | 2006-10-03 04:48:39 +0000 | [diff] [blame] | 133 | |
| 134 | return $output; |
| 135 | } |
| 136 | |
| 137 | // -------------------------------------------------------------------- |
admin | b06d69f | 2006-10-13 21:44:17 +0000 | [diff] [blame] | 138 | |
| 139 | /** |
| 140 | * Compile $_POST Data |
| 141 | * |
| 142 | * @access private |
| 143 | * @return string |
| 144 | */ |
admin | 90a7b9d | 2006-10-03 04:48:39 +0000 | [diff] [blame] | 145 | function _compile_post() |
admin | a813a46 | 2006-10-03 19:17:41 +0000 | [diff] [blame] | 146 | { |
admin | 90a7b9d | 2006-10-03 04:48:39 +0000 | [diff] [blame] | 147 | $output = "\n\n"; |
admin | a813a46 | 2006-10-03 19:17:41 +0000 | [diff] [blame] | 148 | $output .= '<fieldset style="border:1px solid #009900;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">'; |
admin | 90a7b9d | 2006-10-03 04:48:39 +0000 | [diff] [blame] | 149 | $output .= "\n"; |
admin | b3ab70b | 2006-10-07 03:07:29 +0000 | [diff] [blame] | 150 | $output .= '<legend style="color:#009900;"> '.$this->CI->lang->line('profiler_post_data').' </legend>'; |
admin | 90a7b9d | 2006-10-03 04:48:39 +0000 | [diff] [blame] | 151 | $output .= "\n"; |
| 152 | |
| 153 | if (count($_POST) == 0) |
| 154 | { |
admin | b3ab70b | 2006-10-07 03:07:29 +0000 | [diff] [blame] | 155 | $output .= "<div style='color:#009900;font-weight:normal;padding:4px 0 4px 0'>".$this->CI->lang->line('profiler_no_post')."</div>"; |
admin | 90a7b9d | 2006-10-03 04:48:39 +0000 | [diff] [blame] | 156 | } |
| 157 | else |
| 158 | { |
admin | a813a46 | 2006-10-03 19:17:41 +0000 | [diff] [blame] | 159 | $output .= "\n\n<table cellpadding='4' cellspacing='1' border='0' width='100%'>\n"; |
admin | 90a7b9d | 2006-10-03 04:48:39 +0000 | [diff] [blame] | 160 | |
| 161 | foreach ($_POST as $key => $val) |
| 162 | { |
| 163 | if ( ! is_numeric($key)) |
| 164 | { |
| 165 | $key = "'".$key."'"; |
| 166 | } |
| 167 | |
admin | a813a46 | 2006-10-03 19:17:41 +0000 | [diff] [blame] | 168 | $output .= "<tr><td width='50%' style='color:#0000;background-color:#ddd;'>$_POST[".$key."] </td><td width='50%' style='color:#009900;font-weight:normal;background-color:#ddd;'>".htmlspecialchars(stripslashes($val))."</td></tr>\n"; |
admin | 90a7b9d | 2006-10-03 04:48:39 +0000 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | $output .= "</table>\n"; |
| 172 | } |
admin | a813a46 | 2006-10-03 19:17:41 +0000 | [diff] [blame] | 173 | $output .= "</fieldset>"; |
admin | 90a7b9d | 2006-10-03 04:48:39 +0000 | [diff] [blame] | 174 | |
| 175 | return $output; |
| 176 | } |
| 177 | |
| 178 | // -------------------------------------------------------------------- |
| 179 | |
| 180 | /** |
admin | 1c5ef5e | 2006-10-03 04:50:09 +0000 | [diff] [blame] | 181 | * Run the Profiler |
admin | 90a7b9d | 2006-10-03 04:48:39 +0000 | [diff] [blame] | 182 | * |
| 183 | * @access private |
| 184 | * @return string |
| 185 | */ |
| 186 | function run($output = '') |
admin | 88a8ad1 | 2006-10-07 03:16:32 +0000 | [diff] [blame] | 187 | { |
admin | 07ad666 | 2006-10-03 06:12:52 +0000 | [diff] [blame] | 188 | $output = '<br clear="all" />'; |
admin | a813a46 | 2006-10-03 19:17:41 +0000 | [diff] [blame] | 189 | $output .= "<div style='background-color:#fff;padding:10px;'>"; |
admin | 90a7b9d | 2006-10-03 04:48:39 +0000 | [diff] [blame] | 190 | |
| 191 | $output .= $this->_compile_benchmarks(); |
| 192 | $output .= $this->_compile_post(); |
| 193 | $output .= $this->_compile_queries(); |
| 194 | |
admin | a813a46 | 2006-10-03 19:17:41 +0000 | [diff] [blame] | 195 | $output .= '</div>'; |
| 196 | |
admin | 90a7b9d | 2006-10-03 04:48:39 +0000 | [diff] [blame] | 197 | return $output; |
| 198 | } |
| 199 | |
admin | d13ff07 | 2006-10-03 03:40:45 +0000 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | // END CI_Profiler class |
| 203 | ?> |