Page 1 of 1

How to find the time an action took

Posted: Sat Mar 04, 2006 9:21 pm
by a94060
Hi,

i would like to know how people are able to display how long it takes for the page to be generated (for example Invision Board)

It says at the bottom X Quires in XXXX Secs. How would i get that to be displayed?

Posted: Sat Mar 04, 2006 9:44 pm
by Buddha443556

Code: Select all

// Beginning of the script
$start_time = microtime();

Code: Select all

// initialize qCount
$qCount = 0;

// replace all mysql_query() with my_query()
function my_query($sql) {
  $result = mysql_query($sql);
  $qCount++;
}

Code: Select all

// End of the Script
$start_time = explode(" ", $start_time);
$end_time = explode(" ", microtime());
$exec_time = ((float)$end_time[1] + (float)$end_time[0]) - ((float)$start_time[1] + (float)$start_time[0]);

Code: Select all

// output the info
echo "$qCount Quires in $exec_time Secs";
No guarantee any of that works, it's pass my bedtime. :oops:

Posted: Sat Mar 04, 2006 10:15 pm
by John Cartwright

Code: Select all

// initialize qCount
$qCount = 0;

// replace all mysql_query() with my_query()
function my_query($sql) {
  $result = mysql_query($sql);
  $qCount++;
}
This simply will not work for the following reasons:

1) $qCount = 0 is not in the same scope as that in the $qCount++
2) the $qCount in the function will not persist through multiple function calls

I generally use a db abstraction layer, combine with a singleton pattern (or look into static variables) in combination with my mysql calls so I can count the queries (sort of like what you did, but properly)

another option would be to use sessions, so something like

Code: Select all

session_start();

//format microtime float
function microtime_float() {
   list($usec, $sec) = explode(" ", microtime());
   return ((float)$usec + (float)$sec);
}

function my_query($sql) {
   $_SESSION['queryCount']++;
   return mysql_query($sql) or die(mysql_error());
}

//reset counter
$_SESSION['queryCount'] = 0;
//initialize benchmark time
$time_start = microtime_float();

/* BENCHMARK STARTS HERE */

$result1 = my_query('SELECT * FROM `users`');
$result2 = my_query('SELECT * FROM `users`');
$result3 = my_query('SELECT * FROM `users`');

/* BENCHMARK ENDS HERE */

//initialize benchmark end
$time_end = microtime_float();
$time = $time_end - $time_start;

echo 'Queries: '. $_SESSION['queryCount'].'. Execution: '. $time .' seconds';

Posted: Sat Mar 04, 2006 10:19 pm
by a94060
ok,well,i try those a little bit later beaucse it is way past my bed time here (11:19 EST here)

later all.

Posted: Mon Mar 06, 2006 10:29 pm
by Buddha443556
Jcart wrote:

Code: Select all

// initialize qCount
$qCount = 0;

// replace all mysql_query() with my_query()
function my_query($sql) {
  $result = mysql_query($sql);
  $qCount++;
}
This simply will not work for the following reasons:

1) $qCount = 0 is not in the same scope as that in the $qCount++
2) the $qCount in the function will not persist through multiple function calls

I generally use a db abstraction layer, combine with a singleton pattern (or look into static variables) in combination with my mysql calls so I can count the queries (sort of like what you did, but properly)

another option would be to use sessions, so something like
Told you it was pass my bedtime. :lol: But yeah I need a global in there.