The CodeIgniter contains the Benchmarking class that is used to calculate the time difference between two points or the memory usage of the passed statements in the application. By default, the Benchmarking class is already loaded in the CodeIgniter application, so you don’t need to load in the controller file. It can be used in any class, such as the controller, view, and model.

Functions of the Benchmarking Class

  1. mark(): The mark() function contains the variable name to mark the starting and ending point in the benchmark class.
  2. elapsed_time() : It is used to calculate the execution time difference between two marked points.

Syntax

Parameters:

$point1 (string): It defines the start marked point.

$point2 (string): It defines the end marked point.

$decimals (int): It takes a numeric value to show decimal places for precision.

Example: Create a Learn.php file in application/controller folder and write the following program:

Learn.php

When you execute the above program in the localhost by invoking the URL localhost/CodeIgniter-3.1.11/index.php/learn/marked function, it shows the output, as shown below.

CodeIgniter Benchmarking Class
  • memory_usage() : It is used to return the total space consumed by any application. You can use this function in any file to check the consumed space in the application.

Syntax

Example: Create a Learn.php file in application/controller folder and write the following program:

Learn.php

After creating the Learn.php controller file, now create a memory.php file in the application/views/Learn/ folder and write the following code in the view file.

When you execute the above program in the localhost by invoking the URL localhost/CodeIgniter-3.1.11/index.php/learn/marked function, it returns the output, as shown below.

CodeIgniter Benchmarking Class

Cache Driver

CodeIgniter provides a cache driver that is used to access the web page from the server quickly. It also allows working with file-based caching, for which it requires a specific server requirement, and if the server requirements are not met with file-based caching, it throws an exception.

Load a Cache driver

Before using the cache driver, you must load it in the controller file by following the method:

Functions of the caching Driver

These are the following functions of the caching driver, as shown below:

  1. save(): It is used to save a data item to the cache-store. And if the file is not saved in the defined cache store, it returns a FALSE value.

Syntax

Parameters:

$id: It represents the name of the cache file in the cache-store.

$data: It defines the data you want to save in the cache file.

$ttl: It stands for Time to Live, which means how long your data must remain in the cache.

$raw: It passes a Boolean value to store raw data in a save() function.

Example: Create a Learn.php file in application/controller folder and write the following program:

Learn.php

When you execute the above program in the localhost by invoking the URL localhost/CodeIgniter-3.1.11/index.php/Learn/savefile function, it returns the output, as shown below.

CodeIgniter Benchmarking Class

Then it creates a file named cachefiles in the application’s cache folder, as shown below.

CodeIgniter Benchmarking Class
  • get(): It is used to fetch the data from the cache file. And if the data do not exist, it returns a FALSE.

Syntax

$id: It represents the cache filename, which is used to fetch data from the cache-store.

Example: Create a Learn.php file in application/controller folder and write the following program:

Learn.php

When you execute the above program in localhost by invoking the URL localhost/CodeIgniter-3.1.11/index.php/learn/fetchfile, it returns the output, as shown below.

CodeIgniter Benchmarking Class
  • delete(): A delete cache function is used to delete a specific cache file from the cache-store. And if it is unable to delete the file, it returns a FALSE value.

Syntax

Example: Create a Learn.php file in application/controller folder and write the following program:

Learn.php

When you execute the above program in the localhost by invoking the URL localhost/CodeIgniter-3.1.11/index.php/Learn/deletefile, it returns the output, as shown below.

CodeIgniter Benchmarking Class
  • increment(): It is used for the atomic increment of the raw stored data value in the cache.

Syntax

Parameters:

$id (string): It represents the cache id that stored in the cache files.

$offset (int): This takes a numeric value for the atomic increment of a raw stored cache value.

Example: Create a Learn.php file in application/controller folder and write the following program:

Learn.php

When you execute the above program in the localhost by invoking the URL localhost/CodeIgniter-3.1.11/index.php/Learn/increment_value, it returns the output, as shown below.

CodeIgniter Benchmarking Class

          After that, it creates a cache file named “Hello World” and “Tutorialandexample” in the cache folder to store the raw data.

CodeIgniter Benchmarking Class
  • decrement() : It is used for atomic decrementation of the raw stored data value.

Syntax

Parameters:

$id (string): It represents the raw cache id that stored in the cache files.

$offset (int): This takes a numeric value for the atomic decrement of a raw stored cache value.

Example: Create a Learn.php file in application/controller folder and write the following program:

Learn.php

When you execute the above program in localhost by invoking the URL localhost/CodeIgniter-3.1.11/index.php/learn/decrement_value, it returns the output, as shown below.

CodeIgniter Benchmarking Class
  • cache_info(): It is used to return all the basic information of the cache files stored in your Codeigniter application. 

Syntax

Example: Create a Learn.php file in application/controller folder and write the following program:

Learn.php

When you execute the above program in the localhost by invoking the URL localhost/CodeIgniter-3.1.11/index.php/Learn/info_file, it returns the output, as shown below.

CodeIgniter Benchmarking Class
  • clean(): As the name defines, this function is used to clean all the cache data from the cache files, and if it fails to clear all cache files, it returns a FALSE value.

Syntax

Example: Create a Learn.php file in application/controller folder and write the following program:

Learn.php

When you execute the above program in the localhost by invoking the URL localhost/CodeIgniter-3.1.11/index.php/Learn/clean_file, it returns the output, as shown below.

CodeIgniter Benchmarking Class
  • get_metadata(): It is used to get detailed information of a particular item in the cache file.

Syntax

Example: Create a Learn.php file in application/controller folder and write the following program:

Learn.php

When you execute the above program in the localhost by invoking the URL localhost/CodeIgniter-3.1.11/index.php/Learn/getMetadata, it returns the output, as shown below.

CodeIgniter Benchmarking Class

Pin It on Pinterest

Share This