<?php
/*
A cada SQL colcocar essas chamadas, recomendo criar uma função genérica no lugar do mysql_query
Start_SQL_Time_Counter();
$result = mysql_query($sql);
$Query_Time = Stop_SQL_Time_Counter();
Start_SQLLog_Time_Counter();
//Aqui seria para o caso de todos os SQL's serem logados em uma tabela...
Stop_SQLLog_Time_Counter();
$SQL_Total_Execution_Count++;
*/

/* Coloque no Inicio */
$LastTimeCounter = microtime(true);
$StartTimeCounter = microtime(true);
$PHP_Total_Execution_Time = 0;
$SQL_Total_Execution_Time = 0;
$SQL_Total_Execution_Count = 0;
/* Coloque no Inicio - Fim */

function End_SQL_Time_Counter()
{
	global $PHP_Total_Execution_Time,$SQL_Total_Execution_Time,$LastTimeCounter;

	$ActualTime = microtime(true);
	$PHP_Total_Execution_Time = $ActualTime - $LastTimeCounter + $PHP_Total_Execution_Time;
	$LastTimeCounter = $ActualTime;
}

function Start_SQL_Time_Counter()
{
	global $PHP_Total_Execution_Time,$SQL_Total_Execution_Time,$LastTimeCounter;

	$ActualTime = microtime(true);
	$PHP_Total_Execution_Time = $ActualTime - $LastTimeCounter + $PHP_Total_Execution_Time;
	$LastTimeCounter = $ActualTime;
}

function Stop_SQL_Time_Counter()
{
	global $PHP_Total_Execution_Time,$SQL_Total_Execution_Time,$LastTimeCounter;

	$ActualTime = microtime(true);
	$Query_Time = $ActualTime - $LastTimeCounter;
	$SQL_Total_Execution_Time = $Query_Time + $SQL_Total_Execution_Time;
	$LastTimeCounter = $ActualTime;
	return number_format($Query_Time*1, 3, '.', ',');
}

function Start_SQLLog_Time_Counter()
{
	global $PHP_Total_Execution_Time,$SQLLog_Total_Execution_Time,$LastTimeCounter;

	$ActualTime = microtime(true);
	$PHP_Total_Execution_Time = $ActualTime - $LastTimeCounter + $PHP_Total_Execution_Time;
	$LastTimeCounter = $ActualTime;
}

function Stop_SQLLog_Time_Counter()
{
	global $PHP_Total_Execution_Time,$SQLLog_Total_Execution_Time,$LastTimeCounter;

	$ActualTime = microtime(true);
	$SQLLog_Total_Execution_Time = $ActualTime - $LastTimeCounter + $SQLLog_Total_Execution_Time;
	$LastTimeCounter = $ActualTime;
}

function Return_Total_PHP_Time () { global $PHP_Total_Execution_Time; return number_format($PHP_Total_Execution_Time*1, 3, ',', '.'); }
function Return_Total_SQL_Time () { global $SQL_Total_Execution_Time; return number_format($SQL_Total_Execution_Time*1, 3, ',', '.'); }
function Return_Total_SQLLog_Time () { global $SQLLog_Total_Execution_Time; return number_format($SQLLog_Total_Execution_Time*1,3, ',', '.'); }

function Show_Total_Runtime ($debug = 0)
{
	global $StartTimeCounter,$SQL_Total_Execution_Count,$mysqllogArray;
	End_SQL_Time_Counter();
	$aaa="";
	if ((isset($_GET["debug"])) || $debug == 1)
	{
		$aaa = "\r\n".
		'<p style="font-family: Courier New; font-size:13px;">';
		foreach ($mysqllogArray as $key => $sql) $aaa.="$key. $sql<br />\r\n";
		$aaa.="</p>\r\n;";
	}
	echo '<p style="font-size:7px;>Tempo de execução >>> PHP: '.
	Return_Total_PHP_Time()
	."s - SQL: ".
	Return_Total_SQL_Time()
	."s em ".
	$SQL_Total_Execution_Count
	." Comandos - Log: ".
	Return_Total_SQLLog_Time()
	."s - Total: ".
	number_format((microtime(true)-$StartTimeCounter)*1, 3, ',', '.')
	."s</p>".$aaa;
}



sleep(1);
/* No Final só Precisa disso que já exibe em HTML*/
?>
<?php Show_Total_Runtime(); ?>