ExpressionEngine® 3 User Guide

Legacy Documentation

You are using the documentation for version 3.5.17. Go here for the latest version or check here for your available upgrades to the latest version.

Benchmarking Class

The benchmarking class allows you to time the difference between any two marked points.

Using the Benchmark Class

The Benchmark class can be used within your controllers, views, or your models. The process for usage is this:

  1. Mark a start point
  2. Mark an end point
  3. Run the “elapsed time” function to view the results

Here’s an example using real code:

ee()->benchmark->mark('code_start');

// Some code happens here

ee()->benchmark->mark('code_end');

echo ee()->benchmark->elapsed_time('code_start', 'code_end');

Note

The words “code_start” and “code_end” are arbitrary. They are simply words used to set two markers. You can use any words you want, and you can set multiple sets of markers. Consider this example

ee()->benchmark->mark('dog');

// Some code happens here

ee()->benchmark->mark('cat');

// More code happens here

ee()->benchmark->mark('bird');

echo ee()->benchmark->elapsed_time('dog', 'cat');
echo ee()->benchmark->elapsed_time('cat', 'bird');
echo ee()->benchmark->elapsed_time('dog', 'bird');

Displaying Total Execution Time

If you would like to display the total elapsed time from the moment ExpressionEngine starts to the moment the final output is sent to the browser, simply place this in one of your view templates:

<?php echo ee()->benchmark->elapsed_time();?>

You’ll notice that it’s the same function used in the examples above to calculate the time between two point, except you are not using any parameters. When the parameters are absent, ExpressionEngine does not stop the benchmark until right before the final 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.

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:

{elapsed_time}

Note

If you want to benchmark anything within your controller functions you must set your own start/end points.

Displaying Memory Consumption

If your PHP installation is configured with --enable-memory-limit, you can display the amount of memory consumed by the entire system using the following code in one of your view file:

<?php echo ee()->benchmark->memory_usage();?>

Note

This function can only be used in your view files. The consumption will reflect the total memory used by the entire app.

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:

{memory_usage}

Class Reference

class EE_Benchmark

The library is loaded using the following code:

ee()->load->library('benchmark');
EE_Benchmark::mark($name)
Parameters:
  • $name (string) – the name you wish to assign to your marker
Return type:

void

Sets a benchmark marker.

EE_Benchmark::elapsed_time([$point1 = ''[, $point2 = ''[, $decimals = 4]]])
Parameters:
  • $point1 (string) – a particular marked point
  • $point2 (string) – a particular marked point
  • $decimals (int) – number of decimal places for precision
Returns:

Elapsed time

Return type:

string

Calculates and returns the time difference between two marked points.

If the first parameter is empty this function instead returns the {elapsed_time} pseudo-variable. This permits the full system execution time to be shown in a template. The output class will swap the real value for this variable.

EE_Benchmark::memory_usage()
Returns:Memory usage info
Return type:string

Simply returns the {memory_usage} marker.

This permits it to be put it anywhere in a template without the memory being calculated until the end. The Output Class will swap the real value for this variable.