Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 1 | ################## |
| 2 | Benchmarking Class |
| 3 | ################## |
| 4 | |
| 5 | CodeIgniter has a Benchmarking class that is always active, enabling the |
| 6 | time difference between any two marked points to be calculated. |
| 7 | |
| 8 | .. note:: This class is initialized automatically by the system so there |
| 9 | is no need to do it manually. |
| 10 | |
| 11 | In addition, the benchmark is always started the moment the framework is |
| 12 | invoked, and ended by the output class right before sending the final |
| 13 | view to the browser, enabling a very accurate timing of the entire |
| 14 | system execution to be shown. |
| 15 | |
Derek Jones | da67168 | 2013-07-21 13:34:18 -0700 | [diff] [blame] | 16 | .. contents:: |
| 17 | :local: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 18 | |
Derek Jones | da67168 | 2013-07-21 13:34:18 -0700 | [diff] [blame] | 19 | .. raw:: html |
| 20 | |
| 21 | <div class="custom-index container"></div> |
| 22 | |
| 23 | ************************* |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 24 | Using the Benchmark Class |
Derek Jones | da67168 | 2013-07-21 13:34:18 -0700 | [diff] [blame] | 25 | ************************* |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 26 | |
| 27 | The Benchmark class can be used within your |
| 28 | :doc:`controllers </general/controllers>`, |
| 29 | :doc:`views </general/views>`, or your :doc:`models </general/models>`. |
| 30 | The process for usage is this: |
| 31 | |
| 32 | #. Mark a start point |
| 33 | #. Mark an end point |
| 34 | #. Run the "elapsed time" function to view the results |
| 35 | |
| 36 | Here's an example using real code:: |
| 37 | |
| 38 | $this->benchmark->mark('code_start'); |
Derek Jones | 70ff9c9 | 2011-10-05 16:20:38 -0500 | [diff] [blame] | 39 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 40 | // Some code happens here |
Derek Jones | 70ff9c9 | 2011-10-05 16:20:38 -0500 | [diff] [blame] | 41 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 42 | $this->benchmark->mark('code_end'); |
Derek Jones | 70ff9c9 | 2011-10-05 16:20:38 -0500 | [diff] [blame] | 43 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 44 | echo $this->benchmark->elapsed_time('code_start', 'code_end'); |
| 45 | |
| 46 | .. note:: The words "code_start" and "code_end" are arbitrary. They |
| 47 | are simply words used to set two markers. You can use any words you |
| 48 | want, and you can set multiple sets of markers. Consider this example:: |
| 49 | |
| 50 | $this->benchmark->mark('dog'); |
Derek Jones | 70ff9c9 | 2011-10-05 16:20:38 -0500 | [diff] [blame] | 51 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 52 | // Some code happens here |
Derek Jones | 70ff9c9 | 2011-10-05 16:20:38 -0500 | [diff] [blame] | 53 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 54 | $this->benchmark->mark('cat'); |
Derek Jones | 70ff9c9 | 2011-10-05 16:20:38 -0500 | [diff] [blame] | 55 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 56 | // More code happens here |
Derek Jones | 70ff9c9 | 2011-10-05 16:20:38 -0500 | [diff] [blame] | 57 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 58 | $this->benchmark->mark('bird'); |
Derek Jones | 70ff9c9 | 2011-10-05 16:20:38 -0500 | [diff] [blame] | 59 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 60 | echo $this->benchmark->elapsed_time('dog', 'cat'); |
| 61 | echo $this->benchmark->elapsed_time('cat', 'bird'); |
| 62 | echo $this->benchmark->elapsed_time('dog', 'bird'); |
| 63 | |
| 64 | |
| 65 | Profiling Your Benchmark Points |
| 66 | =============================== |
| 67 | |
| 68 | If you want your benchmark data to be available to the |
| 69 | :doc:`Profiler </general/profiling>` all of your marked points must |
| 70 | be set up in pairs, and each mark point name must end with _start and |
| 71 | _end. Each pair of points must otherwise be named identically. Example:: |
| 72 | |
Derek Jones | 70ff9c9 | 2011-10-05 16:20:38 -0500 | [diff] [blame] | 73 | $this->benchmark->mark('my_mark_start'); |
| 74 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 75 | // Some code happens here... |
Derek Jones | 70ff9c9 | 2011-10-05 16:20:38 -0500 | [diff] [blame] | 76 | |
Derek Jones | da67168 | 2013-07-21 13:34:18 -0700 | [diff] [blame] | 77 | $this->benchmark->mark('my_mark_end'); |
Derek Jones | 70ff9c9 | 2011-10-05 16:20:38 -0500 | [diff] [blame] | 78 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 79 | $this->benchmark->mark('another_mark_start'); |
Derek Jones | 70ff9c9 | 2011-10-05 16:20:38 -0500 | [diff] [blame] | 80 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 81 | // Some more code happens here... |
Derek Jones | 70ff9c9 | 2011-10-05 16:20:38 -0500 | [diff] [blame] | 82 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 83 | $this->benchmark->mark('another_mark_end'); |
| 84 | |
| 85 | Please read the :doc:`Profiler page </general/profiling>` for more |
| 86 | information. |
| 87 | |
| 88 | Displaying Total Execution Time |
| 89 | =============================== |
| 90 | |
| 91 | If you would like to display the total elapsed time from the moment |
| 92 | CodeIgniter starts to the moment the final output is sent to the |
| 93 | browser, simply place this in one of your view templates:: |
| 94 | |
| 95 | <?php echo $this->benchmark->elapsed_time();?> |
| 96 | |
| 97 | You'll notice that it's the same function used in the examples above to |
| 98 | calculate the time between two point, except you are **not** using any |
| 99 | parameters. When the parameters are absent, CodeIgniter does not stop |
| 100 | the benchmark until right before the final output is sent to the |
| 101 | browser. It doesn't matter where you use the function call, the timer |
| 102 | will continue to run until the very end. |
| 103 | |
| 104 | An alternate way to show your elapsed time in your view files is to use |
| 105 | this pseudo-variable, if you prefer not to use the pure PHP:: |
| 106 | |
| 107 | {elapsed_time} |
| 108 | |
| 109 | .. note:: If you want to benchmark anything within your controller |
| 110 | functions you must set your own start/end points. |
| 111 | |
| 112 | Displaying Memory Consumption |
| 113 | ============================= |
| 114 | |
| 115 | If your PHP installation is configured with --enable-memory-limit, you |
| 116 | can display the amount of memory consumed by the entire system using the |
| 117 | following code in one of your view file:: |
| 118 | |
| 119 | <?php echo $this->benchmark->memory_usage();?> |
| 120 | |
| 121 | .. note:: This function can only be used in your view files. The consumption |
| 122 | will reflect the total memory used by the entire app. |
| 123 | |
| 124 | An alternate way to show your memory usage in your view files is to use |
| 125 | this pseudo-variable, if you prefer not to use the pure PHP:: |
| 126 | |
| 127 | {memory_usage} |
| 128 | |
Derek Jones | da67168 | 2013-07-21 13:34:18 -0700 | [diff] [blame] | 129 | |
| 130 | *************** |
| 131 | Class Reference |
| 132 | *************** |
| 133 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 134 | .. php:class:: CI_Benchmark |
Derek Jones | da67168 | 2013-07-21 13:34:18 -0700 | [diff] [blame] | 135 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 136 | .. php:method:: mark($name) |
Derek Jones | da67168 | 2013-07-21 13:34:18 -0700 | [diff] [blame] | 137 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 138 | :param string $name: the name you wish to assign to your marker |
| 139 | :rtype: void |
Derek Jones | da67168 | 2013-07-21 13:34:18 -0700 | [diff] [blame] | 140 | |
| 141 | Sets a benchmark marker. |
| 142 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 143 | .. php:method:: elapsed_time([$point1 = ''[, $point2 = ''[, $decimals = 4]]]) |
Derek Jones | da67168 | 2013-07-21 13:34:18 -0700 | [diff] [blame] | 144 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 145 | :param string $point1: a particular marked point |
| 146 | :param string $point2: a particular marked point |
| 147 | :param int $decimals: number of decimal places for precision |
| 148 | :returns: Elapsed time |
| 149 | :rtype: string |
Derek Jones | da67168 | 2013-07-21 13:34:18 -0700 | [diff] [blame] | 150 | |
| 151 | Calculates and returns the time difference between two marked points. |
| 152 | |
| 153 | If the first parameter is empty this function instead returns the |
| 154 | ``{elapsed_time}`` pseudo-variable. This permits the full system |
| 155 | execution time to be shown in a template. The output class will |
| 156 | swap the real value for this variable. |
| 157 | |
| 158 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 159 | .. php:method:: memory_usage() |
Derek Jones | da67168 | 2013-07-21 13:34:18 -0700 | [diff] [blame] | 160 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 161 | :returns: Memory usage info |
| 162 | :rtype: string |
Derek Jones | da67168 | 2013-07-21 13:34:18 -0700 | [diff] [blame] | 163 | |
| 164 | Simply returns the ``{memory_usage}`` marker. |
| 165 | |
| 166 | This permits it to be put it anywhere in a template without the memory |
| 167 | being calculated until the end. The :doc:`Output Class <output>` will |
| 168 | swap the real value for this variable. |