Codeginiter profiler with FirePHP

March 25, 2010 by: Xavier Perez

I have added to my own Profiler the hability to display results on Firebug. I have two form of display data: firebar or window.  Window display data on a div over the code, allowing to read enterilly all the page, meanehilr firebar display data on the firebug console (must to have instaled firephp )

——————————————————————————————————————————————–

Window display data on a div over the code, allowing to read enterilly all the page

Firebar display data on the firebug console (must to have instaled firephp ) See : http://addons.mozilla.org/en-US/firefox/addon/6149

To install MY_Profiler:

Download FirePHPCore : http://www.firephp.org/HQ/Install.htm

PLUGIN INSTALL

Unpack and copy FirePHPCore directory under your application/plugin directory

Create a file under application/plugin directory as:  firephp_pi.php with this content:

<?php
$Base_pathinfo = pathinfo(__FILE__);
include $Base_pathinfo[‘dirname’].DIRECTORY_SEPARATOR.”FirePHPCore”.DIRECTORY_SEPARATOR.”fb.php”;

Change your autoload.php file (in config folder), to load firePHP:

$autoload[‘plugin’] = array(‘FirePHP’);

LIBRARY INSTALL

Unpack and copy FirePHPCore directory under your application/libraries directory

If you install as library, remember to add this line to the MY_profile.php before firePHP call:

$this->load->library('firephp');

This method allows to load firephp library only when you want, or when IP detected, or… your choice.

NEXT STEEP:

Add a config line to your config.php file:

$config[‘firePHP’] = ‘firebar’; // Options:   window or firebar

Be sure your libraries are prefized by “MY_ ”  or change the filename to your own prefix:

$config[‘subclass_prefix’] = ‘MY_’;

Download MY_profiler and copy to your libraries folder:  Download My_profiler

Remember to enable profiler in your controler/model:

$this->output->enable_profiler(TRUE);

Post your comments if you like it !!

<?php if (!defined(‘BASEPATH’)) exit(‘No direct script access allowed’);

class MY_Profiler extends CI_Profiler
{
function MY_profiler()
{
parent::CI_Profiler();
}

function run()
{
if ($this->CI->config->item(“firePHP”) == “window”)
{
// START JQueryLoad
// Remove if you load Jquery else where
$output = ‘<script type=”text/Javascript” src=”/js/jquery.js”></script>’;
$output = ‘<script type=”text/Javascript” src=”http://code.jquery.com/jquery.min.js”></script>’;
// End JQueryLoad
$i_currentDIV = rand(10,99).substr(md5($_SERVER[“REQUEST_URI”]),0,6);
$output .= “<div id=’debugTrace”.$i_currentDIV.”X’ style=’position:absolute;top:0px;left:0px;width:20px;height:30px;border:#FFFFEB solid 2px;filter:progid:DXImageTransform.Microsoft.gradient(startColorstr=#80000000,endColorstr=#10000000);background: rgba(135, 135, 135, 0.6);color:red;z-index:9999;’>”;
$output .= “<a href=’Javascript:;’ onclick=’$(\”#debugTrace”.$i_currentDIV.”\”).show();$(\”.codeigniter_profiler\”).slideUp();’ style=’text-decoration:none;font-size: 1em;’> <font color=’red’ size=’5′>X</font> </a>”;
$output .= “</div>”;
$output .= “<div id=’debugTrace”.$i_currentDIV.”‘ style=’position:absolute;top:0px;left:20px;width:100%;border:#FFFFEB solid 2px;filter:progid:DXImageTransform.Microsoft.gradient(startColorstr=#80000000,endColorstr=#10000000);background: rgba(135, 135, 135, 0.6);color:red;z-index:9999;’>”;
$output .= “<table align=’center’ border=’0′ width=’98%’>”;
$output .= “<tr>”;
$output .= “<td align=’left’ width=’10%’><a href=’Javascript:;’ onclick=’$(\”.codeigniter_profiler\”).slideUp();$(\”#codeigniter_profiler_URI\”).slideDown();’ style=’text-decoration:none;font-size: 1em;color:#fff’> URI </a></td>”;
$output .= “<td align=’left’ width=’10%’><a href=’Javascript:;’ onclick=’$(\”.codeigniter_profiler\”).slideUp();$(\”#codeigniter_profiler_CTRL\”).slideToggle();’ style=’text-decoration:none;font-size: 1em;color:#fff’> Controler </a></td>”;
$output .= “<td align=’left’ width=’10%’><a href=’Javascript:;’ onclick=’$(\”.codeigniter_profiler\”).slideUp();$(\”#codeigniter_profiler_MEM\”).slideToggle();’ style=’text-decoration:none;font-size: 1em;color:#fff’> Memory </a></td>”;
$output .= “<td align=’left’ width=’10%’><a href=’Javascript:;’ onclick=’$(\”.codeigniter_profiler\”).slideUp();$(\”#codeigniter_profiler_BENCH\”).slideToggle();’ style=’text-decoration:none;font-size: 1em;color:#fff’> Benchmarks </a></td>”;
$output .= “<td align=’left’ width=’10%’><a href=’Javascript:;’ onclick=’$(\”.codeigniter_profiler\”).slideUp();$(\”#codeigniter_profiler_VARS\”).slideToggle();’ style=’text-decoration:none;font-size: 1em;color:#fff’> Variables </a></td>”;
$output .= “<td align=’left’ width=’10%’><a href=’Javascript:;’ onclick=’$(\”.codeigniter_profiler\”).slideUp();$(\”#codeigniter_profiler_SQL\”).slideToggle();’ style=’text-decoration:none;font-size: 1em;color:#fff’> SQL </a></td>”;
$output .= “<td align=’right’ width=’80%’><a href=’Javascript:;’ onclick=’$(\”.codeigniter_profiler\”).slideUp();$(\”#debugTrace”.$i_currentDIV.”\”).hide();’ style=’text-decoration:none;font-size: 1em;color:#fff’> <strong>&lt;&lt;&lt;</strong> </a></td>”;
$output .= “<td align=’right’>&nbsp;&nbsp;</td>”;
$output .= “</tr>”;
$output .= “<tr>”;
$output .= “<td colspan=’99’ onclick=’$(\”.codeigniter_profiler\”).slideUp();’>”;
$output .= “<div id=’codeigniter_profiler_URI’ class=’codeigniter_profiler’ style=’position:relative;top:0px;padding:10px;width:90%;text-align:left;background-color:#fff;border:1px solid silver;z-index:9999;display:none;color:silver;’>”;
$output .= $this->_compile_uri_string();
$output .= “</div>”;
$output .= “<div id=’codeigniter_profiler_CTRL’ class=’codeigniter_profiler’ style=’position:relative;top:0px;padding:10px;width:90%;text-align:left;background-color:#fff;border:1px solid silver;z-index:9999;display:none;color:silver;’>”;
$output .= $this->_compile_controller_info();
$output .= “</div>”;
$output .= “<div id=’codeigniter_profiler_MEM’ class=’codeigniter_profiler’ style=’position:relative;top:0px;padding:10px;width:90%;text-align:left;background-color:#fff;border:1px solid silver;z-index:9999;display:none;color:silver;’>”;
$output .= $this->_compile_memory_usage();
$output .= “</div>”;
$output .= “<div id=’codeigniter_profiler_BENCH’ class=’codeigniter_profiler’ style=’position:relative;top:0px;padding:10px;width:90%;text-align:left;background-color:#fff;border:1px solid silver;z-index:9999;display:none;color:silver;’>”;
$output .= $this->_compile_benchmarks();
$output .= “</div>”;
$output .= “<div id=’codeigniter_profiler_VARS’ class=’codeigniter_profiler’ style=’position:relative;top:0px;padding:10px;width:90%;text-align:left;background-color:#fff;border:1px solid silver;z-index:9999;display:none;color:silver;’>”;
$output .= $this->_compile_get();
$output .= $this->_compile_post();
$output .= “</div>”;
$output .= “<div id=’codeigniter_profiler_SQL’ class=’codeigniter_profiler’ style=’position:relative;top:0px;padding:10px;width:90%;text-align:left;background-color:#fff;border:1px solid silver;z-index:9999;display:none;color:silver;’>”;
$output .= $this->_compile_queries();
$output .= “</div>”;
$output .= “</td>”;
$output .= “</tr>”;
$output .= “</table>”;
$output .= “</div>”;
return $output;
}
if ($this->CI->config->item(“firePHP”) == “firebar”)
$this->firePHP();
}

function firePHP()
{
/**
* Add toolbar data to FirePHP console
*/

$firephp = FirePHP::getInstance(TRUE);

$firephp->fb(‘FirePHP DEBUG TOOLBAR:’);

// Global VARS
$globals = array(
‘Post’    => empty($_POST)    ? array() : $_POST,
‘Get’     => empty($_GET)     ? array() : $_GET,
‘Cookie’  => empty($_COOKIE)  ? array() : $_COOKIE,
‘Session’ => empty($_SESSION) ? array() : $_SESSION
);

$tableT = array();
$tableT[] = array(“Vars”,”Values”);
$numVars = 0;
foreach ($globals as $name => $global)
{
$table = array();

foreach((array)$global as $key => $value)
{
if (is_object($value))
{
$value = get_class($value).’ [object]’;
}

$table[][$key]= $value;
$numVars ++;
}

$message = “$name: “.count($global).’ variables’;
$tableT[] = array($message,$table);
}

$message = “VARS: “.$numVars.’ variables’;

$firephp->fb(array($message,$tableT), FirePHP::TABLE);

// Benchmarks
$benchmarks = $this->compile_benchmarks();
$tableT = array();
$tableT[] = array(“Type”,”Time”);
$totalTime = 0;
foreach ($benchmarks as $key => $val)
{
$tableT[] = array($key,$val);
$totalTime = $val;
}

$firephp->fb(array(“Benchmarks: “.number_format($totalTime,4),$tableT), FirePHP::TABLE);

// SQL statements

$dbs = array();

// Let’s determine which databases are currently connected to
foreach (get_object_vars($this->CI) as $CI_object)
{
if (is_object($CI_object) && is_subclass_of(get_class($CI_object), ‘CI_DB’) )
{
$dbs[] = $CI_object;
}
}

$tableT = array();
$tableT[] = array(“SQL”,”Time”);
$totalTime = 0;
foreach ($dbs as $key => $val)
{
foreach ($val->queries as $key2 => $val2)
{
$tableT[] = array($val2,number_format($val->query_times[$key2],4));
$totalTime += $val->query_times[$key2];
}
}
$tableT[] = array(“Total: “,number_format($totalTime,4));
$firephp->fb(array(“SQL : “.number_format($totalTime,4),$tableT),FirePHP::TABLE);

$tableT = array();
$tableT[] = array(“Memory”,”Time”);
$tableT[] = array(“Program”,number_format(memory_get_usage()));
$tableT[] = array(“Max”,number_format(memory_get_peak_usage()));
$firephp->fb(array(“Memory”,$tableT),FirePHP::TABLE);

$tableT = array();
$tableT[] = array(“Class”,”Function”,”File”);
$tableT = array_merge($tableT,debug_backtrace());
$firephp->fb(array(“Files: “,$tableT), FirePHP::TABLE);

return;
}

function compile_benchmarks()
{
$profile = array();
foreach ($this->CI->benchmark->marker as $key => $val)
{
// We match the “end” marker so that the list ends
// up in the order that it was defined
if (preg_match(“/(.+?)_end/i”, $key, $match))
{
if (isset($this->CI->benchmark->marker[$match[1].’_end’]) AND isset($this->CI->benchmark->marker[$match[1].’_start’]))
{
$profile[$match[1]] = $this->CI->benchmark->elapsed_time($match[1].’_start’, $key);
}
}
}

return $profile;
}
}

Leave a Reply

You must be logged in to post a comment.