blob: 8fc06be124e2498adba1a940319d1cc2da236684 [file] [log] [blame]
Derek Jones8ede1a22011-10-05 13:34:52 -05001##################
2Benchmarking Class
3##################
4
5CodeIgniter has a Benchmarking class that is always active, enabling the
6time 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
11In addition, the benchmark is always started the moment the framework is
12invoked, and ended by the output class right before sending the final
13view to the browser, enabling a very accurate timing of the entire
14system execution to be shown.
15
Derek Jonesda671682013-07-21 13:34:18 -070016.. contents::
17 :local:
Derek Jones8ede1a22011-10-05 13:34:52 -050018
Derek Jonesda671682013-07-21 13:34:18 -070019.. raw:: html
20
21 <div class="custom-index container"></div>
22
23*************************
Derek Jones8ede1a22011-10-05 13:34:52 -050024Using the Benchmark Class
Derek Jonesda671682013-07-21 13:34:18 -070025*************************
Derek Jones8ede1a22011-10-05 13:34:52 -050026
27The Benchmark class can be used within your
28:doc:`controllers </general/controllers>`,
29:doc:`views </general/views>`, or your :doc:`models </general/models>`.
30The 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
36Here's an example using real code::
37
38 $this->benchmark->mark('code_start');
Derek Jones70ff9c92011-10-05 16:20:38 -050039
Derek Jones8ede1a22011-10-05 13:34:52 -050040 // Some code happens here
Derek Jones70ff9c92011-10-05 16:20:38 -050041
Derek Jones8ede1a22011-10-05 13:34:52 -050042 $this->benchmark->mark('code_end');
Derek Jones70ff9c92011-10-05 16:20:38 -050043
Derek Jones8ede1a22011-10-05 13:34:52 -050044 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 Jones70ff9c92011-10-05 16:20:38 -050051
Derek Jones8ede1a22011-10-05 13:34:52 -050052 // Some code happens here
Derek Jones70ff9c92011-10-05 16:20:38 -050053
Derek Jones8ede1a22011-10-05 13:34:52 -050054 $this->benchmark->mark('cat');
Derek Jones70ff9c92011-10-05 16:20:38 -050055
Derek Jones8ede1a22011-10-05 13:34:52 -050056 // More code happens here
Derek Jones70ff9c92011-10-05 16:20:38 -050057
Derek Jones8ede1a22011-10-05 13:34:52 -050058 $this->benchmark->mark('bird');
Derek Jones70ff9c92011-10-05 16:20:38 -050059
Derek Jones8ede1a22011-10-05 13:34:52 -050060 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
65Profiling Your Benchmark Points
66===============================
67
68If you want your benchmark data to be available to the
69:doc:`Profiler </general/profiling>` all of your marked points must
70be 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 Jones70ff9c92011-10-05 16:20:38 -050073 $this->benchmark->mark('my_mark_start');
74
Derek Jones8ede1a22011-10-05 13:34:52 -050075 // Some code happens here...
Derek Jones70ff9c92011-10-05 16:20:38 -050076
Derek Jonesda671682013-07-21 13:34:18 -070077 $this->benchmark->mark('my_mark_end');
Derek Jones70ff9c92011-10-05 16:20:38 -050078
Derek Jones8ede1a22011-10-05 13:34:52 -050079 $this->benchmark->mark('another_mark_start');
Derek Jones70ff9c92011-10-05 16:20:38 -050080
Derek Jones8ede1a22011-10-05 13:34:52 -050081 // Some more code happens here...
Derek Jones70ff9c92011-10-05 16:20:38 -050082
Derek Jones8ede1a22011-10-05 13:34:52 -050083 $this->benchmark->mark('another_mark_end');
84
85Please read the :doc:`Profiler page </general/profiling>` for more
86information.
87
88Displaying Total Execution Time
89===============================
90
91If you would like to display the total elapsed time from the moment
92CodeIgniter starts to the moment the final output is sent to the
93browser, simply place this in one of your view templates::
94
95 <?php echo $this->benchmark->elapsed_time();?>
96
97You'll notice that it's the same function used in the examples above to
98calculate the time between two point, except you are **not** using any
99parameters. When the parameters are absent, CodeIgniter does not stop
100the benchmark until right before the final output is sent to the
101browser. It doesn't matter where you use the function call, the timer
102will continue to run until the very end.
103
104An alternate way to show your elapsed time in your view files is to use
105this 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
112Displaying Memory Consumption
113=============================
114
115If your PHP installation is configured with --enable-memory-limit, you
116can display the amount of memory consumed by the entire system using the
117following 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
124An alternate way to show your memory usage in your view files is to use
125this pseudo-variable, if you prefer not to use the pure PHP::
126
127 {memory_usage}
128
Derek Jonesda671682013-07-21 13:34:18 -0700129
130***************
131Class Reference
132***************
133
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200134.. php:class:: CI_Benchmark
Derek Jonesda671682013-07-21 13:34:18 -0700135
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200136 .. php:method:: mark($name)
Derek Jonesda671682013-07-21 13:34:18 -0700137
Andrey Andreev28c2c972014-02-08 04:27:48 +0200138 :param string $name: the name you wish to assign to your marker
139 :rtype: void
Derek Jonesda671682013-07-21 13:34:18 -0700140
141 Sets a benchmark marker.
142
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200143 .. php:method:: elapsed_time([$point1 = ''[, $point2 = ''[, $decimals = 4]]])
Derek Jonesda671682013-07-21 13:34:18 -0700144
Andrey Andreev28c2c972014-02-08 04:27:48 +0200145 :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 Jonesda671682013-07-21 13:34:18 -0700150
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 Andreevcd3d9db2015-02-02 13:41:01 +0200159 .. php:method:: memory_usage()
Derek Jonesda671682013-07-21 13:34:18 -0700160
Andrey Andreev28c2c972014-02-08 04:27:48 +0200161 :returns: Memory usage info
162 :rtype: string
Derek Jonesda671682013-07-21 13:34:18 -0700163
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.