×

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

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.

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

The library is an important part of the CodeIgniter framework. It has a large collection of libraries files that are used to increase the performance of an application. By default, all CodeIgniter libraries...

4 minutes read.

CodeIgniter Helpers

The Helpers (functions) are stored in the Helper file. It helps CodeIgniter to perform different tasks. The helper file is the collection of many functions that is used to perform specific task. It...

3 minutes read.

CodeIgniter ZIP Encoding Class

ZIP Encoding Class The CodeIgniter provides a Zip encoding class that are used to compress a large file into a Zip archives. And this archives files can be downloaded either to your desktop or...

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

Update a Record CodeIgniter

Update a record In this topic, we will learn how we can update existing records in database tables. To update a record, CodeIgniter provides an update() function used with the set()...

10 minutes read.

CodeIgniter HTML Helper

The HTML helper function is used in CodeIgniter to provide various functionality, such as headings, images, links, list tags, etc. Loading the HTML Helper Before using the html helper function, you must load the...

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

A View is a presentational part which shows the information to the users. It is a complex web page which contains navbar, header, footer, sidebar, etc. A view file cannot be called directly...

5 minutes read.

CodeIgniter Pagination Class

Pagination Class The CodeIgniter framework provides a pagination class that is used in the data list to create pagination links in a web application. Generally, it is used to retrieve data records from...

10 minutes read.

CodeIgniter Security Helper

A Security helper file contains some predefined functions that are used to protect application from unauthorized access. Loading the Helper The following syntax is used to load the security helper in the CodeIgniter application. Syntax $this-> load->...

4 minutes read.

CodeIgniter Calendaring Library

The calendar library contains various functions that are used to create a dynamic calendar. It also enables you to pass the data to calendar cells and 100% control over the calendar design. Load...

7 minutes read.

Database Configuration CodeIgniter

In every application, there is an important part of a database through which developers can connect an application to store users’ data and perform various functions in the database application such as inserting,...

10 minutes read.

HTML Table Class CodeIgniter

HTML Table Class The CodeIgniter framework provides an HTML Table Class that is used to develop an auto-generated table using a defined array or result sets of the database table. Loading an HTML Table Class...

8 minutes read.

Migration Class CodeIgniter

The CodeIgniter framework provides the migration class that helps the web developers to create and handle databases in a structured and streamlined manner. The migration classes are very useful when more than...

6 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 Creating Library

As we know, CodeIgniter has a large collection of library functions that are stored in the system/libraries folder. CodeIgniter also provides some additional functionality in library files. For example, if you want to...

3 minutes read.