blob: 3370c91132499359db74ed117445a9ff55869f2e [file] [log] [blame]
admind13ff072006-10-03 03:40:45 +00001<?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
admin1c5ef5e2006-10-03 04:50:09 +000022 * 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.
admind13ff072006-10-03 03:40:45 +000026 *
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 */
33class CI_Profiler {
34
adminb3ab70b2006-10-07 03:07:29 +000035 var $CI;
admin90a7b9d2006-10-03 04:48:39 +000036
37 function CI_Profiler()
38 {
adminb3ab70b2006-10-07 03:07:29 +000039 $this->CI =& get_instance();
40 $this->CI->load->language('profiler');
admin90a7b9d2006-10-03 04:48:39 +000041 }
admind13ff072006-10-03 03:40:45 +000042
43 // --------------------------------------------------------------------
44
45 /**
46 * Auto Profiler
47 *
48 * This function cycles through the entire array of mark points and
49 * matches any two points that are named identially (ending in "_start"
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 {
admin90a7b9d2006-10-03 04:48:39 +000058 $profile = array();
adminb3ab70b2006-10-07 03:07:29 +000059 foreach ($this->CI->benchmark->marker as $key => $val)
admind13ff072006-10-03 03:40:45 +000060 {
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 {
adminb3ab70b2006-10-07 03:07:29 +000065 if (isset($this->CI->benchmark->marker[$match[1].'_end']) AND isset($this->CI->benchmark->marker[$match[1].'_start']))
admind13ff072006-10-03 03:40:45 +000066 {
adminb3ab70b2006-10-07 03:07:29 +000067 $profile[$match[1]] = $this->CI->benchmark->elapsed_time($match[1].'_start', $key);
admind13ff072006-10-03 03:40:45 +000068 }
69 }
70 }
admin90a7b9d2006-10-03 04:48:39 +000071
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";
admina813a462006-10-03 19:17:41 +000077 $output .= '<fieldset style="border:1px solid #990000;padding:6px 10px 10px 10px;margin:0 0 20px 0;background-color:#eee">';
admin90a7b9d2006-10-03 04:48:39 +000078 $output .= "\n";
adminb3ab70b2006-10-07 03:07:29 +000079 $output .= '<legend style="color:#990000;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_benchmarks').'&nbsp;&nbsp;</legend>';
admin90a7b9d2006-10-03 04:48:39 +000080 $output .= "\n";
admina813a462006-10-03 19:17:41 +000081 $output .= "\n\n<table cellpadding='4' cellspacing='1' border='0' width='100%'>\n";
admin90a7b9d2006-10-03 04:48:39 +000082
83 foreach ($profile as $key => $val)
84 {
85 $key = ucwords(str_replace(array('_', '-'), ' ', $key));
admina813a462006-10-03 19:17:41 +000086 $output .= "<tr><td width='50%' style='color:#0000;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";
admin90a7b9d2006-10-03 04:48:39 +000087 }
88
89 $output .= "</table>\n";
admina813a462006-10-03 19:17:41 +000090 $output .= "</fieldset>";
admin90a7b9d2006-10-03 04:48:39 +000091
92 return $output;
admind13ff072006-10-03 03:40:45 +000093 }
94
95 // --------------------------------------------------------------------
96
admin90a7b9d2006-10-03 04:48:39 +000097
98 function _compile_queries()
99 {
100 $output = "\n\n";
admina813a462006-10-03 19:17:41 +0000101 $output .= '<fieldset style="border:1px solid #0000FF;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
admin90a7b9d2006-10-03 04:48:39 +0000102 $output .= "\n";
adminb3ab70b2006-10-07 03:07:29 +0000103 $output .= '<legend style="color:#0000FF;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_queries').'&nbsp;&nbsp;</legend>';
admin90a7b9d2006-10-03 04:48:39 +0000104 $output .= "\n";
105
106 if ( ! class_exists('CI_DB_driver'))
107 {
adminb3ab70b2006-10-07 03:07:29 +0000108 $output .= "<div style='color:#0000FF;font-weight:normal;padding:4px 0 0 0;'>".$this->CI->lang->line('profiler_no_db')."</div>";
admin90a7b9d2006-10-03 04:48:39 +0000109 }
110 else
111 {
adminb3ab70b2006-10-07 03:07:29 +0000112 if (count($this->CI->db->queries) == 0)
admin90a7b9d2006-10-03 04:48:39 +0000113 {
adminb3ab70b2006-10-07 03:07:29 +0000114 $output .= "<div style='color:#0000FF;font-weight:normal;padding:4px 0 4px 0;'>".$this->CI->lang->line('profiler_no_queries')."</div>";
admin90a7b9d2006-10-03 04:48:39 +0000115 }
116 else
117 {
adminb3ab70b2006-10-07 03:07:29 +0000118 foreach ($this->CI->db->queries as $val)
admin90a7b9d2006-10-03 04:48:39 +0000119 {
admina813a462006-10-03 19:17:41 +0000120 $output .= '<div style="padding:3px;margin:12px 0 12px 0;background-color:#ddd;color:#000">';
admin90a7b9d2006-10-03 04:48:39 +0000121 $output .= $val;
122 $output .= "</div>\n";
123 }
124 }
125 }
126
admina813a462006-10-03 19:17:41 +0000127 $output .= "</fieldset>";
admin90a7b9d2006-10-03 04:48:39 +0000128
129 return $output;
130 }
131
132 // --------------------------------------------------------------------
133
134 function _compile_post()
admina813a462006-10-03 19:17:41 +0000135 {
admin90a7b9d2006-10-03 04:48:39 +0000136 $output = "\n\n";
admina813a462006-10-03 19:17:41 +0000137 $output .= '<fieldset style="border:1px solid #009900;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
admin90a7b9d2006-10-03 04:48:39 +0000138 $output .= "\n";
adminb3ab70b2006-10-07 03:07:29 +0000139 $output .= '<legend style="color:#009900;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_post_data').'&nbsp;&nbsp;</legend>';
admin90a7b9d2006-10-03 04:48:39 +0000140 $output .= "\n";
141
142 if (count($_POST) == 0)
143 {
adminb3ab70b2006-10-07 03:07:29 +0000144 $output .= "<div style='color:#009900;font-weight:normal;padding:4px 0 4px 0'>".$this->CI->lang->line('profiler_no_post')."</div>";
admin90a7b9d2006-10-03 04:48:39 +0000145 }
146 else
147 {
admina813a462006-10-03 19:17:41 +0000148 $output .= "\n\n<table cellpadding='4' cellspacing='1' border='0' width='100%'>\n";
admin90a7b9d2006-10-03 04:48:39 +0000149
150 foreach ($_POST as $key => $val)
151 {
152 if ( ! is_numeric($key))
153 {
154 $key = "'".$key."'";
155 }
156
admina813a462006-10-03 19:17:41 +0000157 $output .= "<tr><td width='50%' style='color:#0000;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";
admin90a7b9d2006-10-03 04:48:39 +0000158 }
159
160 $output .= "</table>\n";
161 }
admina813a462006-10-03 19:17:41 +0000162 $output .= "</fieldset>";
admin90a7b9d2006-10-03 04:48:39 +0000163
164 return $output;
165 }
166
167 // --------------------------------------------------------------------
168
169 /**
admin1c5ef5e2006-10-03 04:50:09 +0000170 * Run the Profiler
admin90a7b9d2006-10-03 04:48:39 +0000171 *
172 * @access private
173 * @return string
174 */
175 function run($output = '')
admin88a8ad12006-10-07 03:16:32 +0000176 {
admin07ad6662006-10-03 06:12:52 +0000177 $output = '<br clear="all" />';
admina813a462006-10-03 19:17:41 +0000178 $output .= "<div style='background-color:#fff;padding:10px;'>";
admin90a7b9d2006-10-03 04:48:39 +0000179
180 $output .= $this->_compile_benchmarks();
181 $output .= $this->_compile_post();
182 $output .= $this->_compile_queries();
183
admina813a462006-10-03 19:17:41 +0000184 $output .= '</div>';
185
admin90a7b9d2006-10-03 04:48:39 +0000186 return $output;
187 }
188
admind13ff072006-10-03 03:40:45 +0000189}
190
191// END CI_Profiler class
192?>