PDA

View Full Version : Page Generation Time and Number of Queries.


guvenck
02-06-2006, 08:49 PM
I would like to add to my page footer my page generation time.

I would like to also add how many MySQL queries have been executed during page generation.

So the format is: Page Generation Time: x seconds, Queries executed: y

For page generation time, I think I should define a variable at the beginning of the page and one at the end using microtime() as time() won't include the microseconds. It's not a big deal.

But have no idea about the query count. Maybe MySQL has something built-in which I am not aware about.

Rappa
02-07-2006, 01:25 AM
//put at begining of file
<?php
function utime (){
$time = explode( " ", microtime());
$usec = (double)$time[0];
$sec = (double)$time[1];
return $sec + $usec;
}
$start = utime();
?>
//put at end of page before /body and /h // tml tags
<?php
$end = utime(); $run = $end - $start; echo "Page loaded in " . substr($run, 0, 5) . " secs.";
?>

guvenck
02-07-2006, 12:10 PM
Yes, thanks for the code. I made it working. What about the number of queries :confused:

NancyJ
02-07-2006, 01:31 PM
why not just increment a global variable every time a query is run

dumpfi
02-07-2006, 06:07 PM
You can use microtime(TRUE) to get the current time as a float, e.g:$startTime = microtime(TRUE);
// do your stuff here
echo 'Took ',microtime(TRUE) - $startTime,' seconds.';
To get the query count you can do something like:$GLOBALS['_MYSQL_QUERY_COUNT'] = 0;
function exec_mysql_query($sql)
{
++$GLOBALS['_MYSQL_QUERY_COUNT'];
return mysql_query($sql);
}
/*
do your stuff here
replace all calls to mysql_query() with exec_mysql_query()
*/
echo 'Total MySQL queries: ',$_MYSQL_QUERY_COUNT;dumpfi

Fixed an error.

marek_mar
02-07-2006, 06:51 PM
Shouldn't the $GLOBALS part be inside the function?

dumpfi
02-07-2006, 07:35 PM
Yes, I've corrected it.

dumpfi

guvenck
02-07-2006, 09:17 PM
Thanks for the answer, does this output the total of queries for the same page?

My goal is to display "X MySQL queries have been executed to generate this page" kind of thing at the footer.