×

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 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 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 Array Helper

The Array Helper contains some predefined functions that are used to perform the various operation with array. Load Array helper It is used to load the helper class. We can pass the helper function in the...

6 minutes read.

Calendaring Class CodeIgniter

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

6 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 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 User Agent Class

User Agent Class The CodeIgniter provides a user agent class that helps to identify and collect information about the browser, mobile devices, or robots visiting your site. Besides this, it is also used...

4 minutes read.

CodeIgniter Language Class

Language Class The CodeIgniter’s language class contains the various functionality that is used to access the language-specific files and text line for the purposes of internationalization. This class provides you suitability to add or...

4 minutes read.

CodeIgniter Input Class

Input Class The CodeIgniter framework provides an input class that includes various input functions to pre-process global input data for protection; furthermore, it also provides some helper functions to obtain input data. This...

11 minutes read.

Installation of CodeIgniter

Before installing CodeIgniter, you need to make sure that the wamp or xampp server in the system is preinstalled. The installation procedure for CodeIgniter is very easy. You can download CodeIgniter by following these...

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

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.

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 Text Helper

The text helper contains the various function that is used to operate with text such as define a word limit and characters limit, conversion of word from ASCII to entities, etc. Loading the text...

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

CodeIgniter Models

Models are the classes that deals with backend operations. It is used to fetch the data from the database and send it to the Controller. All the database related information provided inside...

5 minutes read.

CodeIgniter Shopping Cart Class

It contains the various function that enables the user to add or update, display, and delete data item from the shopping carts while browsing the site. The items added in the cart...

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