admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
| 2 | <html>
|
| 3 | <head>
|
| 4 |
|
| 5 | <title>Code Igniter User Guide</title>
|
| 6 |
|
| 7 | <style type='text/css' media='all'>@import url('../userguide.css');</style>
|
| 8 | <link rel='stylesheet' type='text/css' media='all' href='../userguide.css' />
|
| 9 |
|
admin | 17a890d | 2006-09-27 20:42:42 +0000 | [diff] [blame] | 10 | <script type="text/javascript" src="../nav/nav.js"></script>
|
admin | 2296fc3 | 2006-09-27 21:07:02 +0000 | [diff] [blame] | 11 | <script type="text/javascript" src="../nav/prototype.lite.js"></script>
|
admin | 17a890d | 2006-09-27 20:42:42 +0000 | [diff] [blame] | 12 | <script type="text/javascript" src="../nav/moo.fx.js"></script>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 13 | <script type="text/javascript">
|
| 14 | window.onload = function() {
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 15 | myHeight = new fx.Height('nav', {duration: 400});
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 16 | myHeight.hide();
|
| 17 | }
|
| 18 | </script>
|
| 19 |
|
| 20 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
| 21 | <meta http-equiv='expires' content='-1' />
|
| 22 | <meta http-equiv= 'pragma' content='no-cache' />
|
| 23 | <meta name='robots' content='all' />
|
| 24 | <meta name='author' content='Rick Ellis' />
|
| 25 | <meta name='description' content='Code Igniter User Guide' />
|
| 26 |
|
| 27 | </head>
|
| 28 | <body>
|
| 29 |
|
| 30 | <!-- START NAVIGATION -->
|
| 31 | <div id="nav"><div id="nav_inner"><script type="text/javascript">create_menu('../');</script></div></div>
|
| 32 | <div id="nav2"><a name="top"></a><a href="javascript:void(0);" onclick="myHeight.toggle();"><img src="../images/nav_toggle.jpg" width="153" height="44" border="0" title="Toggle Table of Contents" alt="Toggle Table of Contents" /></a></div>
|
| 33 | <div id="masthead">
|
| 34 | <table cellpadding="0" cellspacing="0" border="0" style="width:100%">
|
| 35 | <tr>
|
admin | e0cd609 | 2006-10-20 01:00:31 +0000 | [diff] [blame] | 36 | <td><h1>Code Igniter User Guide Version 1.5.0b3</h1></td>
|
admin | 4979a12 | 2006-10-11 19:40:07 +0000 | [diff] [blame] | 37 | <td id="breadcrumb_right"><a href="../toc.html">Linear Table of Contents</a></td>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 38 | </tr>
|
| 39 | </table>
|
| 40 | </div>
|
| 41 | <!-- END NAVIGATION -->
|
| 42 |
|
| 43 |
|
| 44 | <!-- START BREADCRUMB -->
|
| 45 | <table cellpadding="0" cellspacing="0" border="0" style="width:100%">
|
| 46 | <tr>
|
| 47 | <td id="breadcrumb">
|
| 48 | <a href="http://www.codeigniter.com/">Code Igniter Home</a> ›
|
| 49 | <a href="../index.html">User Guide Home</a> ›
|
| 50 | Benchmarking Class
|
| 51 | </td>
|
| 52 | <td id="searchbox"><form method="get" action="http://www.google.com/search"><input type="hidden" name="as_sitesearch" id="as_sitesearch" value="www.codeigniter.com/user_guide/" />Search User Guide <input type="text" class="input" style="width:200px;" name="q" id="q" size="31" maxlength="255" value="" /> <input type="submit" class="submit" name="sa" value="Go" /></form></td>
|
| 53 | </tr>
|
| 54 | </table>
|
| 55 | <!-- END BREADCRUMB -->
|
| 56 |
|
| 57 | <br clear="all" />
|
| 58 |
|
| 59 |
|
| 60 | <!-- START CONTENT -->
|
| 61 | <div id="content">
|
| 62 |
|
| 63 |
|
| 64 | <h1>Benchmarking Class</h1>
|
| 65 |
|
| 66 | <p>Code Igniter has a Benchmarking class that is always active, enabling the time difference between any
|
| 67 | two marked points to be calculated.</p>
|
| 68 |
|
| 69 | <p class="important"><strong>Note:</strong> This class is initialized automatically by the system so there is no need to do it manually.</p>
|
| 70 |
|
| 71 |
|
| 72 | <p>In addition, the benchmark is always started the moment the framework is
|
| 73 | invoked, and ended by the output class right before sending the final view to the browser, enabling a very accurate
|
| 74 | timing of the entire system execution to be shown.</p>
|
| 75 |
|
admin | e26611f | 2006-10-02 21:59:12 +0000 | [diff] [blame] | 76 |
|
| 77 | <h3>Table of Contents</h3>
|
| 78 |
|
| 79 | <ul>
|
| 80 | <li><a href="#using">Using the Benchmark Class</a></li>
|
admin | 08f6020 | 2006-10-03 05:28:00 +0000 | [diff] [blame] | 81 | <li><a href="#profiler">Profiling Your Benchmark Points</a></li>
|
admin | e26611f | 2006-10-02 21:59:12 +0000 | [diff] [blame] | 82 | <li><a href="#execution">Displaying Total Execution Time</a></li>
|
| 83 | <li><a href="#memory">Displaying Memory Consumption</a></li>
|
admin | e26611f | 2006-10-02 21:59:12 +0000 | [diff] [blame] | 84 | </ul>
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 | <a name="using"></a>
|
| 89 | <h2>Using the Benchmark Class</h2>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 90 |
|
| 91 | <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:
|
| 92 |
|
| 93 | <ol>
|
| 94 | <li>Mark a start point</li>
|
| 95 | <li>Mark an end point</li>
|
| 96 | <li>Run the "elapsed time" function to view the results</li>
|
| 97 | </ol>
|
| 98 |
|
| 99 | <p>Here's an example using real code:</p>
|
| 100 |
|
admin | 08f6020 | 2006-10-03 05:28:00 +0000 | [diff] [blame] | 101 | <code>$this->benchmark->mark('code_start');<br />
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 102 | <br />
|
| 103 | // Some code happens here<br />
|
| 104 | <br />
|
admin | 08f6020 | 2006-10-03 05:28:00 +0000 | [diff] [blame] | 105 | $this->benchmark->mark('code_end');<br />
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 106 | <br />
|
admin | 08f6020 | 2006-10-03 05:28:00 +0000 | [diff] [blame] | 107 | echo $this->benchmark->elapsed_time('code_start', 'code_end');</code>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 108 |
|
admin | 08f6020 | 2006-10-03 05:28:00 +0000 | [diff] [blame] | 109 | <p><strong>Note:</strong> The words "code_start" and "code_end" are arbitrary. They are simply words used to set two markers. You can
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 110 | use any words you want, and you can set multiple sets of markers. Consider this example:</p>
|
| 111 |
|
| 112 | <code>$this->benchmark->mark('dog');<br />
|
| 113 | <br />
|
| 114 | // Some code happens here<br />
|
| 115 | <br />
|
| 116 | $this->benchmark->mark('cat');<br />
|
| 117 | <br />
|
| 118 | // More code happens here<br />
|
| 119 | <br />
|
| 120 | $this->benchmark->mark('bird');<br />
|
| 121 | <br />
|
| 122 | echo $this->benchmark->elapsed_time('dog', 'cat');<br />
|
| 123 | echo $this->benchmark->elapsed_time('cat', 'bird');<br />
|
| 124 | echo $this->benchmark->elapsed_time('dog', 'bird');</code>
|
| 125 |
|
| 126 |
|
admin | 08f6020 | 2006-10-03 05:28:00 +0000 | [diff] [blame] | 127 | <a name="profiler"></a>
|
| 128 | <h2>Profiling Your Benchmark Points</h2>
|
| 129 |
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 130 | <p>If you want your benchmark data to be available to the
|
| 131 | <a href="../general/profiling.html">Profiler</a> all of your marked points must be set up in pairs, and
|
| 132 | each mark point name must end with <kbd>_start</kbd> and <kbd>_end</kbd>.
|
admin | 08f6020 | 2006-10-03 05:28:00 +0000 | [diff] [blame] | 133 | Each pair of points must otherwise be named identically. Example:</p>
|
| 134 |
|
| 135 | <code>
|
| 136 | $this->benchmark->mark('my_mark<kbd>_start</kbd>');<br />
|
| 137 | <br />
|
| 138 | // Some code happens here...<br />
|
| 139 | <br />
|
| 140 | $this->benchmark->mark('my_mark<kbd>_end</kbd>');
|
| 141 | <br /><br />
|
| 142 |
|
| 143 | $this->benchmark->mark('another_mark<kbd>_start</kbd>');<br />
|
| 144 | <br />
|
| 145 | // Some more code happens here...<br />
|
| 146 | <br />
|
| 147 | $this->benchmark->mark('another_mark<kbd>_end</kbd>');
|
| 148 |
|
| 149 | <p>Please read the <a href="../general/profiling.html">Profiler page</a> for more information.</p>
|
| 150 |
|
| 151 |
|
| 152 |
|
admin | e26611f | 2006-10-02 21:59:12 +0000 | [diff] [blame] | 153 | <a name="execution"></a>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 154 | <h2>Displaying Total Execution Time</h2>
|
| 155 |
|
| 156 | <p>If you would like to display the total elapsed time from the moment Code Igniter starts to the moment the final output
|
| 157 | is sent to the browser, simply place this in one of your view templates:</p>
|
| 158 |
|
| 159 | <code><?=$this->benchmark->elapsed_time();?></code>
|
| 160 |
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 161 | <p>You'll notice that it's the same function used in the examples above to calculate the time between two point, except you are
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 162 | <strong>not</strong> using any parameters. When the parameters are absent, Code Igniter does not stop the benchmark until right before the final
|
| 163 | output is sent to the browser. It doesn't matter where you use the function call, the timer will continue to run until the very end.</p>
|
| 164 |
|
| 165 | <p>An alternate way to show your elapsed time in your view files is to use this pseudo-variable, if you prefer not to use the pure PHP:</p>
|
| 166 | <code>{elapsed_time}</code>
|
| 167 |
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 168 | <p class="important"><strong>Note:</strong> If you want to benchmark anything within your controller
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 169 | functions you must set your own start/end points.</p>
|
| 170 |
|
admin | e26611f | 2006-10-02 21:59:12 +0000 | [diff] [blame] | 171 | <a name="memory"></a>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 172 | <h2>Displaying Memory Consumption</h2>
|
| 173 |
|
| 174 | <p>If your PHP installation is configured with --enable-memory-limit, you can display the amount of memory consumed by the entire
|
| 175 | system using the following code in one of your view file:</p>
|
| 176 |
|
| 177 | <code><?=$this->benchmark->memory_usage();?></code>
|
| 178 | <p>Note: This function can only be used in your view files. The consumpiton will reflect the total memory used by the entire app.</p>
|
| 179 |
|
| 180 | <p>An alternate way to show your memory usage in your view files is to use this pseudo-variable, if you prefer not to use the pure PHP:</p>
|
| 181 | <code>{memory_usage}</code>
|
| 182 |
|
| 183 |
|
admin | e26611f | 2006-10-02 21:59:12 +0000 | [diff] [blame] | 184 |
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 185 |
|
| 186 | </div>
|
| 187 | <!-- END CONTENT -->
|
| 188 |
|
| 189 |
|
| 190 | <div id="footer">
|
| 191 | <p>
|
| 192 | Previous Topic: <a href="../general/alternative_php.html">Alternative PHP Syntax</a>
|
| 193 | ·
|
| 194 | <a href="#top">Top of Page</a> ·
|
| 195 | <a href="../index.html">User Guide Home</a> ·
|
| 196 | Next Topic: <a href="calendar.html">Calendaring Class</a>
|
| 197 | <p>
|
| 198 | <p><a href="http://www.codeigniter.com">Code Igniter</a> · Copyright © 2006 · <a href="http://www.pmachine.com">pMachine, Inc.</a></p>
|
| 199 | </div>
|
| 200 |
|
| 201 | </body>
|
| 202 | </html> |