blob: 265aaf0311325a19d07e87560a36ae8711470ffd [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
22 * in order to help with debugging and optimization
23 *
24 * @package CodeIgniter
25 * @subpackage Libraries
26 * @category Libraries
27 * @author Rick Ellis
28 * @link http://www.codeigniter.com/user_guide/libraries/benchmark.html
29 */
30class CI_Profiler {
31
admin90a7b9d2006-10-03 04:48:39 +000032 var $obj;
33
34 function CI_Profiler()
35 {
36 $this->obj =& get_instance();
37 $this->obj->load->language('profiler');
38 }
admind13ff072006-10-03 03:40:45 +000039
40 // --------------------------------------------------------------------
41
42 /**
43 * Auto Profiler
44 *
45 * This function cycles through the entire array of mark points and
46 * matches any two points that are named identially (ending in "_start"
47 * and "_end" respectively). It then compiles the execution times for
48 * all points and returns it as an array
49 *
50 * @access private
51 * @return array
52 */
53 function _compile_benchmarks()
54 {
admin90a7b9d2006-10-03 04:48:39 +000055 $profile = array();
56 foreach ($this->obj->benchmark->marker as $key => $val)
admind13ff072006-10-03 03:40:45 +000057 {
58 // We match the "end" marker so that the list ends
59 // up in the order that it was defined
60 if (preg_match("/(.+?)_end/i", $key, $match))
61 {
admin90a7b9d2006-10-03 04:48:39 +000062 if (isset($this->obj->benchmark->marker[$match[1].'_end']))
admind13ff072006-10-03 03:40:45 +000063 {
admin90a7b9d2006-10-03 04:48:39 +000064 $profile[$match[1]] = $this->obj->benchmark->elapsed_time($match[1].'_start', $key);
admind13ff072006-10-03 03:40:45 +000065 }
66 }
67 }
admin90a7b9d2006-10-03 04:48:39 +000068
69 // Build a table containing the profile data.
70 // Note: At some point we should turn this into a template that can
71 // be modified. We also might want to make this data available to be logged
72
73 $output = "\n\n";
74 $output .= '<fieldset style="border:1px solid #990000;padding:6px 0 10px 10px;margin:0 0 20px 0;background-color:#efefef">';
75 $output .= "\n";
76 $output .= '<legend style="color:#990000;">&nbsp;&nbsp;'.$this->obj->lang->line('profiler_benchmarks').'&nbsp;&nbsp;</legend>';
77 $output .= "\n";
78 $output .= "\n\n<table cellpadding='4' cellspacing='1' border='0'>\n";
79
80 foreach ($profile as $key => $val)
81 {
82 $key = ucwords(str_replace(array('_', '-'), ' ', $key));
83 $output .= "<tr><td style='color:#0000;font-weight:bold;'>".$key."&nbsp;&nbsp;</td><td style='color:#990000;font-weight:normal;'>".$val."</td></tr>\n";
84 }
85
86 $output .= "</table>\n";
87 $output .= "</fieldset>\n\n";
88
89 return $output;
admind13ff072006-10-03 03:40:45 +000090 }
91
92 // --------------------------------------------------------------------
93
admin90a7b9d2006-10-03 04:48:39 +000094
95 function _compile_queries()
96 {
97 $output = "\n\n";
98 $output .= '<fieldset style="border:1px solid #0000FF;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#efefef">';
99 $output .= "\n";
100 $output .= '<legend style="color:#0000FF;">&nbsp;&nbsp;'.$this->obj->lang->line('profiler_queries').'&nbsp;&nbsp;</legend>';
101 $output .= "\n";
102
103 if ( ! class_exists('CI_DB_driver'))
104 {
105 $output .= "<div style='color:#0000FF;font-weight:normal;padding:4px 0 0 0;'>".$this->obj->lang->line('profiler_no_db')."</div>";
106 }
107 else
108 {
109 if (count($this->obj->db->queries) == 0)
110 {
111 $output .= "<div style='color:#0000FF;font-weight:normal;padding:4px 0 4px 0;'>".$this->obj->lang->line('profiler_no_queries')."</div>";
112 }
113 else
114 {
115 foreach ($this->obj->db->queries as $val)
116 {
117 $output .= '<div style="padding:0;margin:12px 0 12px 0;background-color:#efefef;color:#000">';
118 $output .= $val;
119 $output .= "</div>\n";
120 }
121 }
122 }
123
124 $output .= "</fieldset>\n\n";
125
126 return $output;
127 }
128
129 // --------------------------------------------------------------------
130
131 function _compile_post()
132 {
133 $output = "\n\n";
134 $output .= '<fieldset style="border:1px solid #009900;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#efefef">';
135 $output .= "\n";
136 $output .= '<legend style="color:#009900;">&nbsp;&nbsp;'.$this->obj->lang->line('profiler_post_data').'&nbsp;&nbsp;</legend>';
137 $output .= "\n";
138
139 if (count($_POST) == 0)
140 {
141 $output .= "<div style='color:#009900;font-weight:normal;padding:4px 0 4px 0'>".$this->obj->lang->line('profiler_no_post')."</div>";
142 }
143 else
144 {
145 $output .= "\n\n<table cellpadding='4' cellspacing='1' border='0'>\n";
146
147 foreach ($_POST as $key => $val)
148 {
149 if ( ! is_numeric($key))
150 {
151 $key = "'".$key."'";
152 }
153
154 $output .= "<tr><td style='color:#0000;'>&#36;_POST[".$key."]&nbsp;&nbsp;</td><td style='color:#009900;font-weight:normal;'>".htmlspecialchars(stripslashes($val))."</td></tr>\n";
155 }
156
157 $output .= "</table>\n";
158 }
159 $output .= "</fieldset>\n\n";
160
161 return $output;
162 }
163
164 // --------------------------------------------------------------------
165
166 /**
167 * Run the Auto-profiler
168 *
169 * @access private
170 * @return string
171 */
172 function run($output = '')
173 {
174 $output = '<br style="margin:0;padding:0;clear:both;" />';
175
176 $output .= $this->_compile_benchmarks();
177 $output .= $this->_compile_post();
178 $output .= $this->_compile_queries();
179
180 return $output;
181 }
182
admind13ff072006-10-03 03:40:45 +0000183}
184
185// END CI_Profiler class
186?>