×

CodeIgniter Benchmarking Class

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

elapsed_time ( [ $point1 = ’’ [, $point2 =
‘ ‘ [, $decimals = 3 ] ] ]);

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.

<?php 
    $this->benchmark->mark('start_point');
    // write your code within the start and end point  
    $this->benchmark->mark('end_point');
    echo $this->benchmark->elapsed_time ('start_point', ' end_point'); 
 ?> 

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

Learn.php

<?php
 class Learn extends CI_Controller {
 public function marked() // it is the public function that call on the URL 
 {
  echo "<title> Tutorial and example </title>";
 $this->benchmark->mark('start_point');
 for  ($i=0; $i<=500; $i++)
  echo $i."\t";
 $this->benchmark->mark('end_point');
 echo "<br>";
 $data = $this->benchmark->elapsed_time('start_point', 'end_point');
 echo "Total elapsed time by two end points is : ".$data;
 }
 }
 ?> 

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

$this->benchmark ->memory_usage();

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

Learn.php

<?php
 class Learn extends CI_Controller {
 public function marked() // it is the public function that call on the URL 
 {
  echo "<title> Tutorial and example </title>";
 $this->benchmark->mark('start_point');
 for  ($i=0; $i<=500; $i++)
  echo $i."\t";
 $this->benchmark->mark('end_point');
 echo "<br>"; 
 $data = $this->benchmark->elapsed_time('start_point', 'end_point');
 echo "Total elapsed time by two end points is : ". $data;
 $this->load->view('Learn/memory');
 }
 }
 ?> 

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.

<?php 
 $id =  $this->benchmark->memory_usage();
 echo "<br><h2> Memory Usage </h2>".$id;
 ?> 

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:

$this->load->driver( ‘cache’ );

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

save ($id, $data[, $ttl = 120 [, $raw = False ] ] );

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

<?php
 class Learn extends CI_Controller {
 public function savefile()
     {  
         echo "<title> Tutorial and example </title>";
         $this->load->driver('cache', array('adapter' => 'apc', 'backup' => 'file')); 
         $data = 'This is the first example of creating a cache files in the application/cache';
         // Save $data into the cache for 5 minutes
         $this->cache->save('cachefiles', $data, 300);  /* cachefiles is the name of cache file in cache folder */
         echo "<h2> cache file has been created </h2>";
     } 
 } 
 ?> 

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

get ($id);

$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

<?php
 class Learn extends CI_Controller {
 public function fetchfile()
 { 
    echo "<title> Tutorial and example </title>";
     $this->load->driver('cache', array('adapter' => 'apc', 'backup' => 'file'));
  if ( ! $data = $this->cache->get('cachefiles'))
 {
 }
 echo $data;
 }
 } 
 ?> 

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

delete ($id);

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

Learn.php

<?php
 class Learn extends CI_Controller {
 public function deletefile()
 {
       $this->load->driver('cache', array('adapter' => 'apc', 'backup' => 'file'));
               $data = $this->cache->delete('cachefiles'); // It takes file name to be deleted 
               if($data== true) {
                           echo "Your file has been successfully deleted";
                 }
 }
 }
  ?> 

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

increment ($id [, $offset = 5 ]);

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

<?php
 class Learn extends CI_Controller {
 public function increment_value()
             {   
        echo "<title> Tutorial and example </title>";
        $this->load->driver('cache', array('adapter' => 'apc', 'backup' => 'file'));
         echo $this->cache->increment ( 'Hello World' );
         echo"<br>";
    echo $this->cache->increment( 'tutorialandeaxmple', 10);
             }
 }
  ?> 

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

decrement ($id [, $offset = 5 ]);

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

<?php
 class Learn extends CI_Controller {
 public function decrement_value()
             {   
        echo "<title> Tutorial and example </title>";
        $this->load->driver('cache', array('adapter' => 'apc', 'backup' => 'file'));
         echo $this->cache->decrement ( 'Hello World' );
         echo"<br>";
    echo $this->cache->decrement( 'tutorialandeaxmple', 10); 
             }
 }
  ?> 

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

cache_info();

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

Learn.php

<?php
 class Learn extends CI_Controller {
 public function info_file()
    {  
     echo "<title> Tutorial and example </title>";
         $this->load->driver('cache', array('adapter' => 'apc', 'backup' => 'file'));
   var_dump ($this-> cache-> cache_info()); // it shows all the information about the file
     }
 }
 ?> 

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

clean(); 

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

Learn.php

<?php
 class Learn extends CI_Controller {
 public function clean_file()
    {   
    echo "<title> Tutorial and example </title>";
         $this->load->driver('cache', array('adapter' => 'apc', 'backup' => 'file'));
     $this->cache->clean();         // clean the cache file from the cache folder
     echo "Your file has been removed from cache folder";
     }
 }
 ?> 

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

get_metadata ($id);

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

Learn.php

<?php
 class Learn extends CI_Controller {
 public function getMetadata()
    {  
     echo "<title> Tutorial and example </title>";
         $this->load->driver('cache', array('adapter' => 'apc', 'backup' => 'file'));
         var_dump($this->cache->get_metadata ('cachefiles')); // it takes file name
     }
 }
 ?> 

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

Related Topics

Directory Helper Codeigniter

Directory Helper CodeIgniter: provides a directory helper function that works with the directory. It shows the structure of the file, hidden file in the folder, and the folder in...

3 minutes read.

Form Validation Library Codeigniter

Form Validation Library The Codeigniter provides a form validation library that contains predefined rules to validate the form fields. It also helps in optimizing the form validation code, which reduces the effort and time...

31 minutes read.

CodeIgniter URI Class

URI Class The CodeIgniter contains a URI class that is used to retrieve information from URI strings. It also provides suitability to get information from the re-routed segments. The system automatically initializes this...

6 minutes read.

CodeIgniter Email Class

CodeIgniter provides an e-mail library that helps to send a message from one application to another. You can create and send text messages easily in CodeIgniter application even you can set email...

9 minutes read.

CodeIgniter Form Helper

A form helper file contains different functions that works with forms. It is used to create various functionality in a form such as dropdown, radio button, password, upload, hidden data, etc. These functionalities...

12 minutes read.

Codeigniter Session Library

Session Library The session is an essential part of any application. It contains various functions to manage the users' status and track their activity while browsing on the site. For example,...

13 minutes read.

CodeIgniter Date helper

A date helper file contains some predefined functions that work with date functions. Before using the date function in CodeIgniter, you have to load the date helper in the controller. For example: $this->load->helper(‘date’); Functions of...

12 minutes read.

CodeIgniter Benchmarking Class

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...

7 minutes read.

How to remove an index.php file from the Codeigniter URLs?

As we know, a URL in CodeIgniter is designed in such a way that it would be search engine and human friendly, too, rather than using a standard approach to create a...

2 minutes read.

Versions of CodeIgniter

CodeIgniter v3.1.11 (This is the current version which we will used in this tutorial)CodeIgniter v3.1.10CodeIgniter v3.1.9CodeIgniter v3.1.8CodeIgniter v3.1.7CodeIgniter v3.1.6CodeIgniter v3.1.5CodeIgniter v3.1.4CodeIgniter v3.1.3CodeIgniter v3.1.2CodeIgniter v3.1.1CodeIgniter v3.1.0CodeIgniter v3.0.6CodeIgniter...

1 minute read.

CodeIgniter Encrypt Class

The Codeigniter provides an encrypt class used to protect confidential data stored on computer systems or data transmitted over a network. It provides two-way data encryption techniques such as Mcrypt PHP extension and...

4 minutes read.

CodeIgniter Tutorial

What is CodeIgniter? CodeIgniter is an open-source framework for developing a dynamic web application with the use of PHP. It uses the MVC framework to develop the project, which is much...

3 minutes read.

CodeIgniter String Helper

The string helper file contains functions that allows separate operations with strings in CodeIgniter. Loading the String Helper Before using the string helper in the CodeIgniter application, you must load it in the controller...

7 minutes read.

CodeIgniter Smiley Helper

The smiley helper file contains smiley images that are used in the application for showing emotions and comments while chatting with others. Load a Smiley Helper Before using Smiley Helper in the CodeIgniter application, you...

4 minutes read.

CodeIgniter URL Helper

The helper file contains some predefined functions that are used to work with URL, such as generate the current file path, transfer control, redirect, create a link, etc. Load the URL Helper Before using...

7 minutes read.

CodeIgniter Cookie Helper

A cookie is a small set of files sent from the web server to the end-user system. In Helpers, the cookie helper file has some predefined functions that are used to the...

4 minutes read.

CodeIgniter Security Class

Security Class The security class provides various function that helps to create a secure application and process input data securely to the application. This class is preloaded in the CodeIgniter application, so you...

4 minutes read.

CodeIgniter File Helper

The file helper function is used to perform various tasks with files such as accessing files, writing data to files, deleting files, and more. Loading the File Helper Before using the file helper, you must...

7 minutes read.

Structure of Codeigniter

The File structure of CodeIgniter is divided into three parts: ApplicationSystemuser_guide Application -: As the name represents Application, which contains all the main parts of the project like a controller, libraries, view, models, and others....

3 minutes read.

CodeIgniter Controller

As its name suggest that it is a Controller that controls the web application, and it works as an intermediary between the Models and the View. A controller takes requests from the user...

3 minutes read.