function getTime() {
$currTime = microtime();
$time = explode(" ", $currTime);
$tot = $time[1] + $time[0];
return $tot;
}
function getQueryCount() {
$sql = "SHOW SESSION STATUS LIKE 'Questions'";
$res = $mysql_query($sql);
return $res['Value'];
}The function getTime returns the current time accurate upto the millisecond. We record the starting and ending time of the script. The difference is the execution time. Similarly, we note the number of queries executed at the start of the session and then at the end. The difference is the queries executed. Finding out the number itself is 1 query.
Note: mysql_connect() must be called before using getQueriesCount().
Below is a test script. Save it as test.php and run it from Apache. You could fill in the corresponding values at the top of the script.
$db_user = "test";$db_pass = "test";$db_db = "test";$query[0] = "select * from posts";$query[1] = "select * from categories"; function getTime() { $currTime = microtime();$time = explode(" ", $currTime); $tot = $time[1] + $time[0];return $tot; } function getQueryCount() { $sql = "SHOW SESSION STATUS LIKE 'Questions'"; $res = mysql_query($sql);$row = mysql_fetch_array($res); return $row['Value']; }// Begin recording$startTime = getTime();
Its output is.mysql_connect("localhost", "test", "test");mysql_select_db("test");$startQueries = getQueryCount();// Start of scriptforeach($query as $q) {echo "Running query: " . $q . " ";$res = mysql_query($q);}// End of script$endQueries = getQueryCount();$totalTime = round(getTime() - $startTime, 5);$totalQueries = $endQueries - $startQueries - 1;// End recording echo " Page Loaded in " . $totalTime . " seconds.Total ". $totalQueries ." queries.";?>
Running query: select * from posts
Running query: select * from categories
Page Loaded in 0.51291 seconds. Total 2 queries.
No comments:
Post a Comment