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 |
|
Derek Allard | 404e35d | 2007-08-07 01:00:45 +0000 | [diff] [blame] | 5 | <title>CodeIgniter User Guide : Benchmarking Class</title>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 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>
|
Derek Allard | b341237 | 2007-10-25 12:15:16 +0000 | [diff] [blame] | 13 | <script type="text/javascript" src="../nav/user_guide_menu.js"></script>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 14 |
|
| 15 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
| 16 | <meta http-equiv='expires' content='-1' />
|
| 17 | <meta http-equiv= 'pragma' content='no-cache' />
|
| 18 | <meta name='robots' content='all' />
|
| 19 | <meta name='author' content='Rick Ellis' />
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 20 | <meta name='description' content='CodeIgniter User Guide' />
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 21 |
|
| 22 | </head>
|
| 23 | <body>
|
| 24 |
|
| 25 | <!-- START NAVIGATION -->
|
| 26 | <div id="nav"><div id="nav_inner"><script type="text/javascript">create_menu('../');</script></div></div>
|
| 27 | <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>
|
| 28 | <div id="masthead">
|
| 29 | <table cellpadding="0" cellspacing="0" border="0" style="width:100%">
|
| 30 | <tr>
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame^] | 31 | <td><h1>CodeIgniter User Guide Version 1.6.0</h1></td>
|
admin | c0d5d52 | 2006-10-30 19:40:35 +0000 | [diff] [blame] | 32 | <td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 33 | </tr>
|
| 34 | </table>
|
| 35 | </div>
|
| 36 | <!-- END NAVIGATION -->
|
| 37 |
|
| 38 |
|
| 39 | <!-- START BREADCRUMB -->
|
| 40 | <table cellpadding="0" cellspacing="0" border="0" style="width:100%">
|
| 41 | <tr>
|
| 42 | <td id="breadcrumb">
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 43 | <a href="http://www.codeigniter.com/">CodeIgniter Home</a> ›
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 44 | <a href="../index.html">User Guide Home</a> ›
|
| 45 | Benchmarking Class
|
| 46 | </td>
|
Derek Allard | bc03091 | 2007-06-24 18:25:29 +0000 | [diff] [blame] | 47 | <td id="searchbox"><form method="get" action="http://www.google.com/search"><input type="hidden" name="as_sitesearch" id="as_sitesearch" value="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>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 48 | </tr>
|
| 49 | </table>
|
| 50 | <!-- END BREADCRUMB -->
|
| 51 |
|
| 52 | <br clear="all" />
|
| 53 |
|
| 54 |
|
| 55 | <!-- START CONTENT -->
|
| 56 | <div id="content">
|
| 57 |
|
| 58 |
|
| 59 | <h1>Benchmarking Class</h1>
|
| 60 |
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 61 | <p>CodeIgniter has a Benchmarking class that is always active, enabling the time difference between any
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 62 | two marked points to be calculated.</p>
|
| 63 |
|
| 64 | <p class="important"><strong>Note:</strong> This class is initialized automatically by the system so there is no need to do it manually.</p>
|
| 65 |
|
| 66 |
|
| 67 | <p>In addition, the benchmark is always started the moment the framework is
|
| 68 | invoked, and ended by the output class right before sending the final view to the browser, enabling a very accurate
|
| 69 | timing of the entire system execution to be shown.</p>
|
| 70 |
|
admin | e26611f | 2006-10-02 21:59:12 +0000 | [diff] [blame] | 71 |
|
| 72 | <h3>Table of Contents</h3>
|
| 73 |
|
| 74 | <ul>
|
| 75 | <li><a href="#using">Using the Benchmark Class</a></li>
|
admin | 08f6020 | 2006-10-03 05:28:00 +0000 | [diff] [blame] | 76 | <li><a href="#profiler">Profiling Your Benchmark Points</a></li>
|
admin | e26611f | 2006-10-02 21:59:12 +0000 | [diff] [blame] | 77 | <li><a href="#execution">Displaying Total Execution Time</a></li>
|
| 78 | <li><a href="#memory">Displaying Memory Consumption</a></li>
|
admin | e26611f | 2006-10-02 21:59:12 +0000 | [diff] [blame] | 79 | </ul>
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 | <a name="using"></a>
|
| 84 | <h2>Using the Benchmark Class</h2>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 85 |
|
Derek Allard | c644128 | 2007-07-04 23:54:32 +0000 | [diff] [blame] | 86 | <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:</p>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 87 |
|
| 88 | <ol>
|
| 89 | <li>Mark a start point</li>
|
| 90 | <li>Mark an end point</li>
|
| 91 | <li>Run the "elapsed time" function to view the results</li>
|
| 92 | </ol>
|
| 93 |
|
| 94 | <p>Here's an example using real code:</p>
|
| 95 |
|
admin | 08f6020 | 2006-10-03 05:28:00 +0000 | [diff] [blame] | 96 | <code>$this->benchmark->mark('code_start');<br />
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 97 | <br />
|
| 98 | // Some code happens here<br />
|
| 99 | <br />
|
admin | 08f6020 | 2006-10-03 05:28:00 +0000 | [diff] [blame] | 100 | $this->benchmark->mark('code_end');<br />
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 101 | <br />
|
admin | 08f6020 | 2006-10-03 05:28:00 +0000 | [diff] [blame] | 102 | echo $this->benchmark->elapsed_time('code_start', 'code_end');</code>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 103 |
|
admin | 08f6020 | 2006-10-03 05:28:00 +0000 | [diff] [blame] | 104 | <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] | 105 | use any words you want, and you can set multiple sets of markers. Consider this example:</p>
|
| 106 |
|
| 107 | <code>$this->benchmark->mark('dog');<br />
|
| 108 | <br />
|
| 109 | // Some code happens here<br />
|
| 110 | <br />
|
| 111 | $this->benchmark->mark('cat');<br />
|
| 112 | <br />
|
| 113 | // More code happens here<br />
|
| 114 | <br />
|
| 115 | $this->benchmark->mark('bird');<br />
|
| 116 | <br />
|
| 117 | echo $this->benchmark->elapsed_time('dog', 'cat');<br />
|
| 118 | echo $this->benchmark->elapsed_time('cat', 'bird');<br />
|
| 119 | echo $this->benchmark->elapsed_time('dog', 'bird');</code>
|
| 120 |
|
| 121 |
|
admin | 08f6020 | 2006-10-03 05:28:00 +0000 | [diff] [blame] | 122 | <a name="profiler"></a>
|
| 123 | <h2>Profiling Your Benchmark Points</h2>
|
| 124 |
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 125 | <p>If you want your benchmark data to be available to the
|
| 126 | <a href="../general/profiling.html">Profiler</a> all of your marked points must be set up in pairs, and
|
| 127 | 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] | 128 | Each pair of points must otherwise be named identically. Example:</p>
|
| 129 |
|
| 130 | <code>
|
| 131 | $this->benchmark->mark('my_mark<kbd>_start</kbd>');<br />
|
| 132 | <br />
|
| 133 | // Some code happens here...<br />
|
| 134 | <br />
|
| 135 | $this->benchmark->mark('my_mark<kbd>_end</kbd>');
|
| 136 | <br /><br />
|
| 137 |
|
| 138 | $this->benchmark->mark('another_mark<kbd>_start</kbd>');<br />
|
| 139 | <br />
|
| 140 | // Some more code happens here...<br />
|
| 141 | <br />
|
| 142 | $this->benchmark->mark('another_mark<kbd>_end</kbd>');
|
Derek Allard | a06681f | 2007-04-01 16:40:51 +0000 | [diff] [blame] | 143 | </code>
|
admin | 08f6020 | 2006-10-03 05:28:00 +0000 | [diff] [blame] | 144 |
|
| 145 | <p>Please read the <a href="../general/profiling.html">Profiler page</a> for more information.</p>
|
| 146 |
|
| 147 |
|
admin | e26611f | 2006-10-02 21:59:12 +0000 | [diff] [blame] | 148 | <a name="execution"></a>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 149 | <h2>Displaying Total Execution Time</h2>
|
| 150 |
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 151 | <p>If you would like to display the total elapsed time from the moment CodeIgniter starts to the moment the final output
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 152 | is sent to the browser, simply place this in one of your view templates:</p>
|
| 153 |
|
| 154 | <code><?=$this->benchmark->elapsed_time();?></code>
|
| 155 |
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 156 | <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
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 157 | <strong>not</strong> using any parameters. When the parameters are absent, CodeIgniter does not stop the benchmark until right before the final
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 158 | 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>
|
| 159 |
|
| 160 | <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>
|
| 161 | <code>{elapsed_time}</code>
|
| 162 |
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 163 | <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] | 164 | functions you must set your own start/end points.</p>
|
| 165 |
|
admin | e26611f | 2006-10-02 21:59:12 +0000 | [diff] [blame] | 166 | <a name="memory"></a>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 167 | <h2>Displaying Memory Consumption</h2>
|
| 168 |
|
| 169 | <p>If your PHP installation is configured with --enable-memory-limit, you can display the amount of memory consumed by the entire
|
| 170 | system using the following code in one of your view file:</p>
|
| 171 |
|
| 172 | <code><?=$this->benchmark->memory_usage();?></code>
|
Derek Allard | f5b6218 | 2007-04-24 05:54:31 +0000 | [diff] [blame] | 173 | <p>Note: This function can only be used in your view files. The consumption will reflect the total memory used by the entire app.</p>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 174 |
|
| 175 | <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>
|
| 176 | <code>{memory_usage}</code>
|
| 177 |
|
| 178 |
|
admin | e26611f | 2006-10-02 21:59:12 +0000 | [diff] [blame] | 179 |
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 180 |
|
| 181 | </div>
|
| 182 | <!-- END CONTENT -->
|
| 183 |
|
| 184 |
|
| 185 | <div id="footer">
|
| 186 | <p>
|
Derek Allard | 9da4dbc | 2007-04-03 11:39:35 +0000 | [diff] [blame] | 187 | Previous Topic: <a href="../general/security.html"> Security</a>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 188 | ·
|
| 189 | <a href="#top">Top of Page</a> ·
|
| 190 | <a href="../index.html">User Guide Home</a> ·
|
| 191 | Next Topic: <a href="calendar.html">Calendaring Class</a>
|
Derek Allard | c644128 | 2007-07-04 23:54:32 +0000 | [diff] [blame] | 192 | </p>
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 193 | <p><a href="http://www.codeigniter.com">CodeIgniter</a> · Copyright © 2007 · <a href="http://ellislab.com/">Ellislab, Inc.</a></p>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 194 | </div>
|
| 195 |
|
| 196 | </body>
|
| 197 | </html> |