Hello world. Let us time some code.
In order to use debuglog() all you need to do is include the section of code in your script. Then at critical sections of code add a call to debuglog() passing in a message to yourself. At the end of the script call debuglog_print() and you will be able to trace the timings of different sections of your code. Enjoy.
This script took 0.028160 seconds to complete.
If you are really interested you can see the logs in full:
Script started at: 2009/01/06 04:48:48 0 ms: +0.000 ms: Here we are at the top of our script. 0 ms: +0.000 ms: Page head has been sent 0 ms: +0.000 ms: Calling SQL query: SELECT id FROM sometable WHERE something > 20 (not really, we are faking it) 28.048 ms: +28.048 ms: End of SQL query 28.121 ms: +0.073 ms: Just had a little sleep for 25279 microseconds
Or in HTML format:
| Script started at | 2009/01/06 04:48:48 | |
| 0 ms | +0.000 ms | Here we are at the top of our script. |
| 0 ms | +0.000 ms | Page head has been sent |
| 0 ms | +0.000 ms | Calling SQL query: SELECT id FROM sometable WHERE something > 20 (not really, we are faking it) |
| 28.048 ms | +28.048 ms | End of SQL query |
| 28.121 ms | +0.073 ms | Just had a little sleep for 25279 microseconds |
<?php
/*
* Sample code to demonstrate a cool way yo debug PHP code including timing
* details.
*
* Open Source Code: If you use this code on your site for public
* access (i.e. on the Internet) then you must attribute the author and
* source web site:
* http://www.pgregg.com/projects/php/code/code_debug_timing.php
* http://www.pgregg.com/projects/php/debuglog/debuglog.inc.phps
*
* (c) Paul Gregg, 2007
* 4th April 2007
* http://www.pgregg.com/projects/
*
*/
include '../debuglog/debuglog.inc.php';
// Now here is our regular page contents
// Lets do some stuff and see how long it takes.
debuglog('Here we are at the top of our script.');
header("Content-Type: text/html");
echo <<<EOHEAD
<html>
<head><title>Page debug example</title></head>
EOHEAD;
debuglog('Page head has been sent');
echo '<body><p>Hello world. Let us time some code.</p>';
$sql = 'SELECT id FROM sometable WHERE something > 20';
debuglog('Calling SQL query: ', $sql, ' (not really, we are faking it)');
$delay = rand(1000,100000);
usleep($delay);
debuglog('End of SQL query');
debuglog('Just had a little sleep for ', $delay, ' microseconds');
echo '<p>In order to use debuglog() all you need to do is include the section of code in your script. Then at critical sections of code add a call to debuglog() passing in a message to yourself. At the end of the script call debuglog_print() and you will be able to trace the timings of different sections of your code. Enjoy.</p>';
echo '<p><a href="http://www.pgregg.com/projects/php/debuglog/debuglog.inc.phps">Download debuglog.inc.php</a></p>';
echo '<p>This script took ', debuglog_timediff(), ' seconds to complete.</p>';
if ($DO_DEBUG) {
echo '<hr><p>If you are really interested you can see the logs in full:<pre>';
debuglog_print();
echo '</pre>';
echo '<p>Or in HTML format:';
echo '<table border="1">';
debuglog_print(false,'<tr><td>','</td><td>','</td></tr>');
echo '</table></p>';
}
echo '<hr>';
highlight_file(__FILE__);
?>