dbs[] = $db;
}
public function __construct()
{
$this->benchmark = new HC3_Profiler_Benchmark;
// default all sections to display
foreach ($this->_available_sections as $section){
if ( ! isset($config[$section])){
$this->{'_compile_' . $section} = TRUE;
}
}
// $this->set_sections($config);
// if wordpress
if( defined('WPINC') ){
global $wpdb;
$this->add_db( $wpdb );
}
}
function mark($name)
{
$this->benchmark->mark($name);
return $this;
}
public function set_sections($config)
{
foreach ($config as $method => $enable){
if (in_array($method, $this->_available_sections)){
$this->{'_compile_' . $method} = ($enable !== FALSE) ? TRUE : FALSE;
}
}
}
protected function _compile_benchmarks()
{
$profile = array();
foreach ( $this->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->benchmark->marker[$match[1].'_end']) AND isset($this->benchmark->marker[$match[1].'_start'])){
$profile[$match[1]] = $this->benchmark->elapsed_time($match[1].'_start', $key);
}
}
}
$output = "\n\n";
$output .= '
';
$output .= "\n";
$output .= ' '.'profiler_benchmarks'.' ';
$output .= "\n";
$output .= "\n\n\n";
foreach ($profile as $key => $val){
$key = ucwords(str_replace(array('_', '-'), ' ', $key));
$output .= "".$key." ".$val." \n";
}
$output .= "
\n";
$output .= " ";
return $output;
}
protected function _compile_queries()
{
$dbs = $this->dbs;
if( (count($dbs) == 0) && defined('NTS_DEVELOPMENT3') ){
global $wpdb;
$dbs[] = $wpdb;
}
if (count($dbs) == 0){
$output = "\n\n";
$output .= '';
$output .= "\n";
$output .= ' '.'profiler_queries'.' ';
$output .= "\n";
$output .= "\n\n\n";
$output .="".'profiler_no_db'." \n";
$output .= "
\n";
$output .= " ";
return $output;
}
// Key words we want bolded
$highlight = array('SELECT', 'DISTINCT', 'FROM', 'WHERE', 'AND', 'LEFT JOIN', 'ORDER BY', 'GROUP BY', 'LIMIT', 'INSERT', 'INTO', 'VALUES', 'UPDATE', 'OR ', 'HAVING', 'OFFSET', 'NOT IN', 'IN', 'LIKE', 'NOT LIKE', 'COUNT', 'MAX', 'MIN', 'ON', 'AS', 'AVG', 'SUM', '(', ')');
$output = "\n\n";
$count = 0;
foreach ($dbs as $db){
$queries = method_exists($db, 'queries') ? $db->queries() : $db->queries;
if( ! $queries ) $queries = array();
$count++;
$hide_queries = (count($queries) > $this->_query_toggle_count) ? ' display:none' : '';
$show_hide_js = '('.'profiler_section_hide'.' )';
if ($hide_queries != ''){
$show_hide_js = '('.'profiler_section_show'.' )';
}
$output .= '';
$output .= "\n";
$output .= ' '.'profiler_queries'.': '.count($queries).' '.$show_hide_js.' ';
$output .= "\n";
$output .= "\n\n\n";
if (count($queries) == 0){
$output .= "".'profiler_no_queries'." \n";
}
else {
foreach ($queries as $key => $val){
list( $val, $time ) = $val;
$time = number_format( $time, 4);
$output .= "".$time." ".$val." \n";
}
}
$output .= "
\n";
$output .= " ";
}
return $output;
}
protected function _compile_get()
{
$output = "\n\n";
$output .= '';
$output .= "\n";
$output .= ' '.'profiler_get_data'.' ';
$output .= "\n";
if (count($_GET) == 0){
$output .= "".'profiler_no_get'."
";
}
else {
$output .= "\n\n\n";
foreach ($_GET as $key => $val){
if ( ! is_numeric($key)){
$key = "'".$key."'";
}
$output .= "$_GET[".$key."] ";
if (is_array($val)){
$output .= "" . htmlspecialchars(stripslashes(print_r($val, true))) . " ";
}
else {
$output .= htmlspecialchars(stripslashes($val));
}
$output .= " \n";
}
$output .= "
\n";
}
$output .= " ";
return $output;
}
protected function _compile_post()
{
$output = "\n\n";
$output .= '';
$output .= "\n";
$output .= ' '.'profiler_post_data'.' ';
$output .= "\n";
if (count($_POST) == 0){
$output .= "".'profiler_no_post'."
";
}
else {
$output .= "\n\n\n";
foreach ($_POST as $key => $val){
if ( ! is_numeric($key)){
$key = "'".$key."'";
}
$output .= "$_POST[".$key."] ";
if (is_array($val)){
$output .= "" . htmlspecialchars(stripslashes(print_r($val, TRUE))) . " ";
}
else {
$output .= htmlspecialchars(stripslashes($val));
}
$output .= " \n";
}
$output .= "
\n";
}
$output .= " ";
return $output;
}
protected function _compile_uri_string()
{
return;
$output = "\n\n";
$output .= '';
$output .= "\n";
$output .= ' '.'profiler_uri_string'.' ';
$output .= "\n";
if ($this->CI->uri->uri_string == ''){
$output .= "".'profiler_no_uri'."
";
}
else {
$output .= "".$this->CI->uri->uri_string."
";
}
$output .= " ";
return $output;
}
protected function _compile_controller_info()
{
return;
$output = "\n\n";
$output .= '';
$output .= "\n";
$output .= ' '.'profiler_controller_info'.' ';
$output .= "\n";
$output .= "".$this->CI->router->fetch_class()."/".$this->CI->router->fetch_method()."
";
$output .= " ";
return $output;
}
protected function _compile_memory_usage()
{
$output = "\n\n";
$output .= '';
$output .= "\n";
$output .= ' '.'profiler_memory_usage'.' ';
$output .= "\n";
if (function_exists('memory_get_usage') && ($usage = memory_get_usage()) != ''){
$output .= "".number_format($usage).' bytes
';
}
else {
$output .= "".'profiler_no_memory'."
";
}
$output .= " ";
return $output;
}
protected function _compile_http_headers()
{
$output = "\n\n";
$output .= '";
return $output;
}
protected function _compile_config()
{
return;
}
private function _compile_session_data()
{
return;
}
public function render( $content )
{
$out = $content . $this->run();
return $out;
}
public function run()
{
$output = "";
$fields_displayed = 0;
foreach ($this->_available_sections as $section){
if ($this->{'_compile_' . $section} !== FALSE){
$func = "_compile_{$section}";
$output .= $this->{$func}();
$fields_displayed++;
}
}
if ($fields_displayed == 0){
$output .= '
'.'profiler_no_profiles'.'
';
}
$output .= '
';
return $output;
}
}
class HC3_Profiler_Benchmark
{
var $marker = array();
function mark($name)
{
$this->marker[$name] = microtime();
}
function elapsed_time($point1 = '', $point2 = '', $decimals = 4)
{
if ($point1 == ''){
return '{elapsed_time}';
}
if ( ! isset($this->marker[$point1])){
return '';
}
if ( ! isset($this->marker[$point2])){
$this->marker[$point2] = microtime();
}
list($sm, $ss) = explode(' ', $this->marker[$point1]);
list($em, $es) = explode(' ', $this->marker[$point2]);
return number_format(($em + $es) - ($sm + $ss), $decimals);
}
function memory_usage()
{
return '{memory_usage}';
}
}