diff options
author | admin <devnull@localhost> | 2006-10-02 23:59:12 +0200 |
---|---|---|
committer | admin <devnull@localhost> | 2006-10-02 23:59:12 +0200 |
commit | e26611f93bfb21f5e143cc91ce529f6db52cdd88 (patch) | |
tree | 575b6b71408d25e6f4067b2a1533e0255fac707e | |
parent | b8afd045b9ed165bdae0d18b3e6f7483a4566e1c (diff) |
-rw-r--r-- | system/codeigniter/CodeIgniter.php | 2 | ||||
-rw-r--r-- | system/libraries/Benchmark.php | 32 | ||||
-rw-r--r-- | system/libraries/Output.php | 70 | ||||
-rw-r--r-- | user_guide/general/changelog.html | 1 | ||||
-rw-r--r-- | user_guide/libraries/benchmark.html | 54 |
5 files changed, 152 insertions, 7 deletions
diff --git a/system/codeigniter/CodeIgniter.php b/system/codeigniter/CodeIgniter.php index a4f18b0e1..32c08c8f0 100644 --- a/system/codeigniter/CodeIgniter.php +++ b/system/codeigniter/CodeIgniter.php @@ -51,7 +51,7 @@ set_magic_quotes_runtime(0); // Kill magic quotes */ $BM =& _load_class('Benchmark'); -$BM->mark('code_igniter_start'); +$BM->mark('total_execution_time_start'); /* * ------------------------------------------------------ diff --git a/system/libraries/Benchmark.php b/system/libraries/Benchmark.php index d8dd903e7..feedbf570 100644 --- a/system/libraries/Benchmark.php +++ b/system/libraries/Benchmark.php @@ -86,6 +86,38 @@ class CI_Benchmark { // -------------------------------------------------------------------- /** + * Auto Profiler + * + * This function cycles through the entire array of mark points and + * matches any two points that are named identially (ending in "_start" + * and "_end" respectively). It then compiles the execution times for + * all points and returns it as an array + * + * @access public + * @return array + */ + function auto_profiler() + { + $marker_keys = array_reverse(array_keys($this->marker)); + + $times = array(); + foreach ($marker_keys as $val) + { + if (preg_match("/(.+?)_start/i", $val, $match)) + { + if (isset($this->marker[$match[1].'_end'])) + { + $times[$match[1]] = $this->elapsed_time($val, $match[1].'_end'); + } + } + } + + return $times; + } + + // -------------------------------------------------------------------- + + /** * Memory Usage * * This function returns the {memory_usage} pseudo-variable. diff --git a/system/libraries/Output.php b/system/libraries/Output.php index 1c3f0d604..b5b7c9e97 100644 --- a/system/libraries/Output.php +++ b/system/libraries/Output.php @@ -31,6 +31,7 @@ class CI_Output { var $final_output; var $cache_expiration = 0; var $headers = array(); + var $enable_profiler = FALSE; function CI_Output() { @@ -90,6 +91,20 @@ class CI_Output { // -------------------------------------------------------------------- /** + * Enable/disable Profiler + * + * @access public + * @param bool + * @return void + */ + function enable_profiler($val = TRUE) + { + $this->enable_profiler = (is_bool($val)) ? $val : TRUE; + } + + // -------------------------------------------------------------------- + + /** * Set Cache * * @access public @@ -123,7 +138,7 @@ class CI_Output { */ function _display($output = '') { - // Note: We can't use $obj =& _get_instance() since this function + // Note: We can't use $obj =& get_instance() since this function // is sometimes called by the caching mechanism, which happens before // it's available. Instead we'll use globals... global $BM, $CFG; @@ -141,7 +156,7 @@ class CI_Output { // Parse out the elapsed time and memory usage, and // swap the pseudo-variables with the data - $elapsed = $BM->elapsed_time('code_igniter_start', 'code_igniter_end'); + $elapsed = $BM->elapsed_time('total_execution_time_start', 'total_execution_time_end'); $memory = ( ! function_exists('memory_get_usage')) ? '0' : round(memory_get_usage()/1024/1024, 2).'MB'; $output = str_replace('{memory_usage}', $memory, $output); @@ -166,14 +181,28 @@ class CI_Output { { @header($header); } - } + } // Send the finalized output either directly // to the browser or to the user's _output() // function if it exists - if (function_exists('_get_instance') AND method_exists($obj, '_output')) + if (function_exists('get_instance')) { - $obj->_output($output); + $obj =& get_instance(); + + if ($this->enable_profiler == TRUE) + { + $output .= $this->_run_profiler(); + } + + if (method_exists($obj, '_output')) + { + $obj->_output($output); + } + else + { + echo $output; // Send it to the browser! + } } else { @@ -295,6 +324,37 @@ class CI_Output { return TRUE; } + // -------------------------------------------------------------------- + + /** + * Run the Auto-profiler + * + * @access private + * @return string + */ + function _run_profiler() + { + $obj =& get_instance(); + + $profile = $obj->benchmark->auto_profiler(); + + $output = ''; + if (count($profile) > 0) + { + $output .= "\n\n<table cellpadding='4' cellspacing='1' border='0'>\n"; + + foreach ($profile as $key => $val) + { + $key = ucwords(str_replace(array('_', '-'), ' ', $key)); + $output .= "<tr><td><strong>".$key."</strong></td><td>".$val."</td></tr>\n"; + } + + $output .= "</table>\n"; + } + + return $output; + } + } // END Output Class ?>
\ No newline at end of file diff --git a/user_guide/general/changelog.html b/user_guide/general/changelog.html index 1cac05dca..df4d0f1b8 100644 --- a/user_guide/general/changelog.html +++ b/user_guide/general/changelog.html @@ -73,6 +73,7 @@ Change Log <li>Added relationship capability to the database active record class</li>
<li>Added <a href="../libraries/Zip.html">Zip Encoding Library</a> permitting files to be Zip compressed.</li>
<li>Added the ability to <a href="creating_libraries.html">extend libraries</a> and <a href="core_classes.html">extend core classes</a>, in addition to being able to replace them.</li>
+<li>Added <a href="../libraries/benchmark.html">Auto Profiler</a> to the Benchmark Class, enabling you to generate a report of execution times.</li>
<li>Added support for storing <a href="models.html">models within sub-folders</a>.</li>
<li>Added <a href="../helpers/download_helper.html">Download Helper</a>.</li>
<li>Added <a href="../database/queries.html">simple_query()</a> function to the database classes</li>
diff --git a/user_guide/libraries/benchmark.html b/user_guide/libraries/benchmark.html index 7f3e28bf5..b8b0bfd32 100644 --- a/user_guide/libraries/benchmark.html +++ b/user_guide/libraries/benchmark.html @@ -73,7 +73,20 @@ two marked points to be calculated.</p> invoked, and ended by the output class right before sending the final view to the browser, enabling a very accurate
timing of the entire system execution to be shown.</p>
-<h2>Using the Benchmark</h2>
+
+<h3>Table of Contents</h3>
+
+<ul>
+<li><a href="#using">Using the Benchmark Class</a></li>
+<li><a href="#execution">Displaying Total Execution Time</a></li>
+<li><a href="#memory">Displaying Memory Consumption</a></li>
+<li><a href="#profiler">Auto Profiler</a></li>
+</ul>
+
+
+
+<a name="using"></a>
+<h2>Using the Benchmark Class</h2>
<p>The Benchmark class can be used within your <a href="../general/controllers.html">controllers</a>, <a href="../general/views.html">views</a>, or your <a href="../general/models.html">Models</a>. The process for usage is this:
@@ -111,6 +124,7 @@ echo $this->benchmark->elapsed_time('cat', 'bird');<br /> echo $this->benchmark->elapsed_time('dog', 'bird');</code>
+<a name="execution"></a>
<h2>Displaying Total Execution Time</h2>
<p>If you would like to display the total elapsed time from the moment Code Igniter starts to the moment the final output
@@ -128,6 +142,7 @@ output is sent to the browser. It doesn't matter where you use the function cal <p class="important"><strong>Note:</strong> If you want to benchmark anything within your controller
functions you must set your own start/end points.</p>
+<a name="memory"></a>
<h2>Displaying Memory Consumption</h2>
<p>If your PHP installation is configured with --enable-memory-limit, you can display the amount of memory consumed by the entire
@@ -140,6 +155,43 @@ system using the following code in one of your view file:</p> <code>{memory_usage}</code>
+<a name="profiler"></a>
+<h2>Auto Profiler</h2>
+
+<p>When the "auto profiler" is enabled, you'll see a report printed at the bottom of your pages containing a list of
+execution times for all benchmarks you have set throughout your application. This information can help you optimize your program.</p>
+
+<p class="important"><strong>Note:</strong> Even though this is a feature of the Benchmark class you will enable it from the Output class as indicated below.</p>
+
+<p>To enable the profiler place the the following function anywhere within your Controllers:</p>
+<code>$this->output->enable_profiler(TRUE);</code>
+
+<p>When enabled you'll see a table of execution times at the bottom of your pages.</p>
+
+<p>To disable the profiler you will use:</p>
+<code>$this->output->enable_profiler(FALSE);</code>
+
+<p>Important: In order to use this feature all of your marked points must end with <kbd>_start</kbd> and <kbd>_end</kbd>, and
+each pair of points must otherwise be named identically. Example:</p>
+
+<code>
+$this->benchmark->mark('my_mark<kbd>_start</kbd>');<br />
+<br />
+// Some code happens here...<br />
+<br />
+$this->benchmark->mark('my_mark<kbd>_end</kbd>');
+<br /><br />
+
+$this->benchmark->mark('another_mark<kbd>_start</kbd>');<br />
+<br />
+// Some more code happens here...<br />
+<br />
+$this->benchmark->mark('another_mark<kbd>_end</kbd>');
+
+</code>
+
+
+
</div>
<!-- END CONTENT -->
|