blob: 5b86142dda49d6742366be0572867f307f20bc15 [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
16.. contents:: Table of Contents
17
18Using the Benchmark Class
19=========================
20
21The Benchmark class can be used within your
22:doc:`controllers </general/controllers>`,
23:doc:`views </general/views>`, or your :doc:`models </general/models>`.
24The 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
30Here's an example using real code::
31
32 $this->benchmark->mark('code_start');
Derek Jones70ff9c92011-10-05 16:20:38 -050033
Derek Jones8ede1a22011-10-05 13:34:52 -050034 // Some code happens here
Derek Jones70ff9c92011-10-05 16:20:38 -050035
Derek Jones8ede1a22011-10-05 13:34:52 -050036 $this->benchmark->mark('code_end');
Derek Jones70ff9c92011-10-05 16:20:38 -050037
Derek Jones8ede1a22011-10-05 13:34:52 -050038 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 Jones70ff9c92011-10-05 16:20:38 -050045
Derek Jones8ede1a22011-10-05 13:34:52 -050046 // Some code happens here
Derek Jones70ff9c92011-10-05 16:20:38 -050047
Derek Jones8ede1a22011-10-05 13:34:52 -050048 $this->benchmark->mark('cat');
Derek Jones70ff9c92011-10-05 16:20:38 -050049
Derek Jones8ede1a22011-10-05 13:34:52 -050050 // More code happens here
Derek Jones70ff9c92011-10-05 16:20:38 -050051
Derek Jones8ede1a22011-10-05 13:34:52 -050052 $this->benchmark->mark('bird');
Derek Jones70ff9c92011-10-05 16:20:38 -050053
Derek Jones8ede1a22011-10-05 13:34:52 -050054 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
59Profiling Your Benchmark Points
60===============================
61
62If you want your benchmark data to be available to the
63:doc:`Profiler </general/profiling>` all of your marked points must
64be 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 Jones70ff9c92011-10-05 16:20:38 -050067 $this->benchmark->mark('my_mark_start');
68
Derek Jones8ede1a22011-10-05 13:34:52 -050069 // Some code happens here...
Derek Jones70ff9c92011-10-05 16:20:38 -050070
71 $this->benchmark->mark('my_mark_end');
72
Derek Jones8ede1a22011-10-05 13:34:52 -050073 $this->benchmark->mark('another_mark_start');
Derek Jones70ff9c92011-10-05 16:20:38 -050074
Derek Jones8ede1a22011-10-05 13:34:52 -050075 // Some more code happens here...
Derek Jones70ff9c92011-10-05 16:20:38 -050076
Derek Jones8ede1a22011-10-05 13:34:52 -050077 $this->benchmark->mark('another_mark_end');
78
79Please read the :doc:`Profiler page </general/profiling>` for more
80information.
81
82Displaying Total Execution Time
83===============================
84
85If you would like to display the total elapsed time from the moment
86CodeIgniter starts to the moment the final output is sent to the
87browser, simply place this in one of your view templates::
88
89 <?php echo $this->benchmark->elapsed_time();?>
90
91You'll notice that it's the same function used in the examples above to
92calculate the time between two point, except you are **not** using any
93parameters. When the parameters are absent, CodeIgniter does not stop
94the benchmark until right before the final output is sent to the
95browser. It doesn't matter where you use the function call, the timer
96will continue to run until the very end.
97
98An alternate way to show your elapsed time in your view files is to use
99this 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
106Displaying Memory Consumption
107=============================
108
109If your PHP installation is configured with --enable-memory-limit, you
110can display the amount of memory consumed by the entire system using the
111following 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
118An alternate way to show your memory usage in your view files is to use
119this pseudo-variable, if you prefer not to use the pure PHP::
120
121 {memory_usage}
122