From 08f6020b0085842d4ae81949a0f4886c94158a55 Mon Sep 17 00:00:00 2001 From: admin Date: Tue, 3 Oct 2006 05:28:00 +0000 Subject: --- system/libraries/Benchmark.php | 35 ---------- system/libraries/Output.php | 131 ++++++++++++++++++------------------ system/libraries/Router.php | 5 ++ user_guide/libraries/benchmark.html | 72 +++++++++----------- user_guide/libraries/output.html | 15 +++++ user_guide/libraries/profiler.html | 95 -------------------------- user_guide/nav/nav.js | 3 +- user_guide/toc.html | 3 +- 8 files changed, 122 insertions(+), 237 deletions(-) delete mode 100644 user_guide/libraries/profiler.html diff --git a/system/libraries/Benchmark.php b/system/libraries/Benchmark.php index feedbf570..d29e91798 100644 --- a/system/libraries/Benchmark.php +++ b/system/libraries/Benchmark.php @@ -48,7 +48,6 @@ class CI_Benchmark { { $this->marker[$name] = microtime(); } - // END mark() // -------------------------------------------------------------------- @@ -81,39 +80,6 @@ class CI_Benchmark { return number_format(($em + $es) - ($sm + $ss), $decimals); } - // END elapsed_time() - - // -------------------------------------------------------------------- - - /** - * 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; - } // -------------------------------------------------------------------- @@ -132,7 +98,6 @@ class CI_Benchmark { { return '{memory_usage}'; } - // END memory_usage() } diff --git a/system/libraries/Output.php b/system/libraries/Output.php index b5b7c9e97..4ab5dd23c 100644 --- a/system/libraries/Output.php +++ b/system/libraries/Output.php @@ -29,9 +29,10 @@ class CI_Output { var $final_output; - var $cache_expiration = 0; - var $headers = array(); - var $enable_profiler = FALSE; + var $cache_expiration = 0; + var $headers = array(); + var $enable_profiler = FALSE; + function CI_Output() { @@ -121,46 +122,52 @@ class CI_Output { /** * Display Output * - * All "view" data is automatically put into this variable - * by the controller class: + * All "view" data is automatically put into this variable by the controller class: * * $this->final_output * - * This function simply echos the variable out. It also does the following: - * - * Stops the benchmark timer so the page rendering speed can be shown. - * - * Determines if the "memory_get_usage' function is available so that - * the memory usage can be shown. + * This function sends the finalized output data to the browser along + * with any server headers and profile data. It also stops the + * benchmark timer so the page rendering speed and memory usage can be shown. * * @access public - * @return void + * @return mixed */ function _display($output = '') { - // 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... + // Note: We use globals because we can't use $obj =& get_instance() + // since this function is sometimes called by the caching mechanism, + // which happens before the CI super object is available. global $BM, $CFG; - + + // -------------------------------------------------------------------- + + // Set the output data if ($output == '') { $output =& $this->final_output; } + // -------------------------------------------------------------------- + // Do we need to write a cache file? if ($this->cache_expiration > 0) { $this->_write_cache($output); } + + // -------------------------------------------------------------------- - // Parse out the elapsed time and memory usage, and - // swap the pseudo-variables with the data + // Parse out the elapsed time and memory usage, + // then swap the pseudo-variables with the data + $elapsed = $BM->elapsed_time('total_execution_time_start', 'total_execution_time_end'); + $output = str_replace('{elapsed_time}', $elapsed, $output); + $memory = ( ! function_exists('memory_get_usage')) ? '0' : round(memory_get_usage()/1024/1024, 2).'MB'; - $output = str_replace('{memory_usage}', $memory, $output); - $output = str_replace('{elapsed_time}', $elapsed, $output); + + // -------------------------------------------------------------------- // Is compression requested? if ($CFG->item('compress_output') === TRUE) @@ -173,6 +180,8 @@ class CI_Output { } } } + + // -------------------------------------------------------------------- // Are there any server headers to send? if (count($this->headers) > 0) @@ -182,30 +191,54 @@ 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')) + // Does the get_instance() function exist? + // If not we know we are dealing with a cache file so we'll + // simply echo out the data and exit. + if ( ! function_exists('get_instance')) { - $obj =& get_instance(); - - if ($this->enable_profiler == TRUE) - { - $output .= $this->_run_profiler(); - } + echo $output; + log_message('debug', "Final output sent to browser"); + log_message('debug', "Total execution time: ".$elapsed); + } + + // -------------------------------------------------------------------- + + // Grab the super object. We'll need it in a moment... + $obj =& get_instance(); - if (method_exists($obj, '_output')) + // Do we need to generate profile data? + // If so, load the Profile class and run it. + if ($this->enable_profiler == TRUE) + { + $obj->load->library('profiler'); + + // If the output data contains closing and tags + // we will remove them and add them back after we insert the profile data + if (preg_match("|.*?|is", $output)) { - $obj->_output($output); + $output = preg_replace("|.*?|is", '', $output); + $output .= $obj->profiler->run(); + $output .= ''; } else { - echo $output; // Send it to the browser! + $output .= $obj->profiler->run(); } } + + // -------------------------------------------------------------------- + + // Does the controller contain a function named _output()? + // If so send the output there. Otherwise, echo it. + if (method_exists($obj, '_output')) + { + $obj->_output($output); + } else - { + { echo $output; // Send it to the browser! } @@ -324,36 +357,6 @@ 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\n"; - - foreach ($profile as $key => $val) - { - $key = ucwords(str_replace(array('_', '-'), ' ', $key)); - $output .= "\n"; - } - - $output .= "
".$key."".$val."
\n"; - } - - return $output; - } } // END Output Class diff --git a/system/libraries/Router.php b/system/libraries/Router.php index d62cf5090..7a4fd3899 100644 --- a/system/libraries/Router.php +++ b/system/libraries/Router.php @@ -530,6 +530,11 @@ class CI_Router { */ function fetch_method() { + if ($this->method == $this->fetch_class()) + { + return 'index'; + } + return $this->method; } diff --git a/user_guide/libraries/benchmark.html b/user_guide/libraries/benchmark.html index b8b0bfd32..8c1dc2c44 100644 --- a/user_guide/libraries/benchmark.html +++ b/user_guide/libraries/benchmark.html @@ -78,9 +78,9 @@ timing of the entire system execution to be shown.

@@ -98,15 +98,15 @@ timing of the entire system execution to be shown.

Here's an example using real code:

-$this->benchmark->mark('start');
+$this->benchmark->mark('code_start');

// Some code happens here

-$this->benchmark->mark('end');
+$this->benchmark->mark('code_end');

-echo $this->benchmark->elapsed_time('start', 'end');
+echo $this->benchmark->elapsed_time('code_start', 'code_end');
-

Note: The words "start" and "end" are arbitrary. They are simply words used to set two markers. You can +

Note: The words "code_start" and "code_end" are arbitrary. They are simply words used to set two markers. You can use any words you want, and you can set multiple sets of markers. Consider this example:

$this->benchmark->mark('dog');
@@ -124,6 +124,32 @@ echo $this->benchmark->elapsed_time('cat', 'bird');
echo $this->benchmark->elapsed_time('dog', 'bird');
+ +

Profiling Your Benchmark Points

+ +

If you want your benchmark data to be available to the +Profiler all of your marked points must be set up in pairs, and +each mark point name must end with _start and _end. +Each pair of points must otherwise be named identically. Example:

+ + +$this->benchmark->mark('my_mark_start');
+
+// Some code happens here...
+
+$this->benchmark->mark('my_mark_end'); +

+ +$this->benchmark->mark('another_mark_start');
+
+// Some more code happens here...
+
+$this->benchmark->mark('another_mark_end'); + +

Please read the Profiler page for more information.

+ + +

Displaying Total Execution Time

@@ -155,42 +181,6 @@ system using the following code in one of your view file:

{memory_usage} - -

Auto Profiler

- -

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.

- -

Note: Even though this is a feature of the Benchmark class you will enable it from the Output class as indicated below.

- -

To enable the profiler place the the following function anywhere within your Controllers:

-$this->output->enable_profiler(TRUE); - -

When enabled you'll see a table of execution times at the bottom of your pages.

- -

To disable the profiler you will use:

-$this->output->enable_profiler(FALSE); - -

Important: In order to use this feature all of your marked points must end with _start and _end, and -each pair of points must otherwise be named identically. Example:

- - -$this->benchmark->mark('my_mark_start');
-
-// Some code happens here...
-
-$this->benchmark->mark('my_mark_end'); -

- -$this->benchmark->mark('another_mark_start');
-
-// Some more code happens here...
-
-$this->benchmark->mark('another_mark_end'); - -
- - diff --git a/user_guide/libraries/output.html b/user_guide/libraries/output.html index adc8fb58e..ad6a53d8f 100644 --- a/user_guide/libraries/output.html +++ b/user_guide/libraries/output.html @@ -105,6 +105,21 @@ $this->output->set_header("Cache-Control: post-check=0, pre-check=0", false);
output->set_header("Pragma: no-cache");
+

$this->output->enable_profiler();

+ +

Permits you to enable/disable the Profiler, which will display benchmark and other data +at the bottom of your pages for debugging and optimization purposes.

+ +

To enable the profiler place the the following function anywhere within your Controller functions:

+$this->output->enable_profiler(TRUE); + +

When enabled a report will be generated and inserted at the bottom of your pages.

+ +

To disable the profiler you will use:

+$this->output->enable_profiler(FALSE); + + + diff --git a/user_guide/libraries/profiler.html b/user_guide/libraries/profiler.html deleted file mode 100644 index 2b162be0b..000000000 --- a/user_guide/libraries/profiler.html +++ /dev/null @@ -1,95 +0,0 @@ - - - - -Code Igniter User Guide - - - - - - - - - - - - - - - - - - - - - - -
- - - - - -

Code Igniter User Guide Version 1.5.0

-
- - - - - - - - - -
- - -
- - - -
- - - - -

Profiler Class

- -

The Profiler Class enables you to display benchmark, query, POST data at the bottom of your pages during -development in order to help with debugging and optimization.

- - -

Initializing the Class

- -

Important: This class does NOT need to be initialized. It is loaded automatically by the Output class if -profiling is enabled as shown below.

- - - -
- - - - - - - \ No newline at end of file diff --git a/user_guide/nav/nav.js b/user_guide/nav/nav.js index fc2d609a1..5afa28742 100644 --- a/user_guide/nav/nav.js +++ b/user_guide/nav/nav.js @@ -52,7 +52,8 @@ function create_menu(basepath) '
  • Scaffolding
  • ' + '
  • URI Routing
  • ' + '
  • Error Handling
  • ' + - '
  • Web Page Caching
  • ' + + '
  • Caching
  • ' + + '
  • Profiling Your Application
  • ' + '
  • Running Multiple Applications
  • ' + '
  • Alternative PHP Syntax
  • ' + '
  • Security
  • ' + diff --git a/user_guide/toc.html b/user_guide/toc.html index f4675b9b5..d479aa19a 100644 --- a/user_guide/toc.html +++ b/user_guide/toc.html @@ -107,7 +107,8 @@ Table of Contents
  • Scaffolding
  • URI Routing
  • Error Handling
  • -
  • Web Page Caching
  • +
  • Caching
  • +
  • Profiling Your Application
  • Running Multiple Applications
  • Alternative PHP Syntax
  • Security
  • -- cgit v1.2.3-24-g4f1b