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 | |
| 16 | .. contents:: Table of Contents |
| 17 | |
| 18 | Using the Benchmark Class |
| 19 | ========================= |
| 20 | |
| 21 | The Benchmark class can be used within your |
| 22 | :doc:`controllers </general/controllers>`, |
| 23 | :doc:`views </general/views>`, or your :doc:`models </general/models>`. |
| 24 | The process for usage is this: |
| 25 | |
| 26 | #. Mark a start point |
| 27 | #. Mark an end point |
| 28 | #. Run the "elapsed time" function to view the results |
| 29 | |
| 30 | Here's an example using real code:: |
| 31 | |
| 32 | $this->benchmark->mark('code_start'); |
Derek Jones | 70ff9c9 | 2011-10-05 16:20:38 -0500 | [diff] [blame] | 33 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 34 | // Some code happens here |
Derek Jones | 70ff9c9 | 2011-10-05 16:20:38 -0500 | [diff] [blame] | 35 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 36 | $this->benchmark->mark('code_end'); |
Derek Jones | 70ff9c9 | 2011-10-05 16:20:38 -0500 | [diff] [blame] | 37 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 38 | echo $this->benchmark->elapsed_time('code_start', 'code_end'); |
| 39 | |
| 40 | .. note:: The words "code_start" and "code_end" are arbitrary. They |
| 41 | are simply words used to set two markers. You can use any words you |
| 42 | want, and you can set multiple sets of markers. Consider this example:: |
| 43 | |
| 44 | $this->benchmark->mark('dog'); |
Derek Jones | 70ff9c9 | 2011-10-05 16:20:38 -0500 | [diff] [blame] | 45 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 46 | // Some code happens here |
Derek Jones | 70ff9c9 | 2011-10-05 16:20:38 -0500 | [diff] [blame] | 47 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 48 | $this->benchmark->mark('cat'); |
Derek Jones | 70ff9c9 | 2011-10-05 16:20:38 -0500 | [diff] [blame] | 49 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 50 | // More code happens here |
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 | $this->benchmark->mark('bird'); |
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 | echo $this->benchmark->elapsed_time('dog', 'cat'); |
| 55 | echo $this->benchmark->elapsed_time('cat', 'bird'); |
| 56 | echo $this->benchmark->elapsed_time('dog', 'bird'); |
| 57 | |
| 58 | |
| 59 | Profiling Your Benchmark Points |
| 60 | =============================== |
| 61 | |
| 62 | If you want your benchmark data to be available to the |
| 63 | :doc:`Profiler </general/profiling>` all of your marked points must |
| 64 | be set up in pairs, and each mark point name must end with _start and |
| 65 | _end. Each pair of points must otherwise be named identically. Example:: |
| 66 | |
Derek Jones | 70ff9c9 | 2011-10-05 16:20:38 -0500 | [diff] [blame] | 67 | $this->benchmark->mark('my_mark_start'); |
| 68 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 69 | // Some code happens here... |
Derek Jones | 70ff9c9 | 2011-10-05 16:20:38 -0500 | [diff] [blame] | 70 | |
| 71 | $this->benchmark->mark('my_mark_end'); |
| 72 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 73 | $this->benchmark->mark('another_mark_start'); |
Derek Jones | 70ff9c9 | 2011-10-05 16:20:38 -0500 | [diff] [blame] | 74 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 75 | // Some more code happens here... |
Derek Jones | 70ff9c9 | 2011-10-05 16:20:38 -0500 | [diff] [blame] | 76 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 77 | $this->benchmark->mark('another_mark_end'); |
| 78 | |
| 79 | Please read the :doc:`Profiler page </general/profiling>` for more |
| 80 | information. |
| 81 | |
| 82 | Displaying Total Execution Time |
| 83 | =============================== |
| 84 | |
| 85 | If you would like to display the total elapsed time from the moment |
| 86 | CodeIgniter starts to the moment the final output is sent to the |
| 87 | browser, simply place this in one of your view templates:: |
| 88 | |
| 89 | <?php echo $this->benchmark->elapsed_time();?> |
| 90 | |
| 91 | You'll notice that it's the same function used in the examples above to |
| 92 | calculate the time between two point, except you are **not** using any |
| 93 | parameters. When the parameters are absent, CodeIgniter does not stop |
| 94 | the benchmark until right before the final output is sent to the |
| 95 | browser. It doesn't matter where you use the function call, the timer |
| 96 | will continue to run until the very end. |
| 97 | |
| 98 | An alternate way to show your elapsed time in your view files is to use |
| 99 | this pseudo-variable, if you prefer not to use the pure PHP:: |
| 100 | |
| 101 | {elapsed_time} |
| 102 | |
| 103 | .. note:: If you want to benchmark anything within your controller |
| 104 | functions you must set your own start/end points. |
| 105 | |
| 106 | Displaying Memory Consumption |
| 107 | ============================= |
| 108 | |
| 109 | If your PHP installation is configured with --enable-memory-limit, you |
| 110 | can display the amount of memory consumed by the entire system using the |
| 111 | following code in one of your view file:: |
| 112 | |
| 113 | <?php echo $this->benchmark->memory_usage();?> |
| 114 | |
| 115 | .. note:: This function can only be used in your view files. The consumption |
| 116 | will reflect the total memory used by the entire app. |
| 117 | |
| 118 | An alternate way to show your memory usage in your view files is to use |
| 119 | this pseudo-variable, if you prefer not to use the pure PHP:: |
| 120 | |
| 121 | {memory_usage} |
| 122 | |