Location: PHPKode > scripts > Script timer > script-timer/examples/script_timer_example.php
<?php

/**
 * Example file to demonstrate use of script_timer class
 *
 * File: example/example.php
 * @package script_timer
 */

// Record a start time at the very beginning of your script
$time_start = microtime();


// It is good practice to only use this kind of timing for debugging, so set a switch which
// you can use whenever you add debugging functionality.
// If you are not used to this practice, set this to false and see what happens
define( 'DEBUG',	true );


// Manually record some markers before including and instantiating the class

usleep( 1600 ); // doing something more
if( DEBUG === true ) $time_array[] = array( microtime(), 'Manually recorded marker 1' );

usleep( 500 ); // doing something
if( DEBUG === true ) $time_array[] = array( microtime(), 'Manually recorded marker 2' );


// Include the class
if( DEBUG === true ) include( '../script_timer.inc.php' );
if( DEBUG === true ) $time_array[] = array( microtime(), 'Manually recorded marker 3 - included the timer class' );


// Instantiate the class
if( DEBUG === true ) $timer = new script_timer();


// Add marker recorded before script instantiation to the class
if( DEBUG === true ) $timer->add_markers( $time_array, $time_start, true, 'Class instantiation' );



// Do some things and record some markers

header( 'Content-type: text/html; charset=utf-8' );
if( DEBUG === true ) $timer->set_marker( 'Headers send', 'Script output - top' );


print '
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html dir="ltr" lang="en-UK">
<head>

<title>Example file for script_timer class</title>

  <meta name="title" content="Example file for script_timer class :: Advies en zo - Meedenken en -doen" />
  <meta name="author" content="Juliette Reinders Folmer, Advies en zo, http://www.adviesenzo.nl/" />
  <meta name="owner" content="Advies en zo, http://www.adviesenzo.nl/" />
  <meta name="design" content="Advies en zo, http://www.adviesenzo.nl/" />
  <meta name="publisher" content="Advies en zo, http://www.adviesenzo.nl/" />
  <meta name="copyright" content="Copyright Advies en zo 2006 - all rights reserved" />
  <meta name="language" content="en-UK" />
  <meta name="distribution" content="global" />
  <meta name="rating" content="General" />
  <meta http-equiv="Charset" content="ISO-8859-1" />

  <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
  <meta http-equiv="Content-Language" content="English" />
  <meta http-equiv="Content-Style-Type" content="text/css" />
  <meta http-equiv="Content-Script-type" content="text/javascript" />
  <meta http-equiv="window-target" content="_top" />
  <meta http-equiv="imagetoolbar" content="no" />
  <meta name="MSSmartTagsPreventParsing" content="true" />
  <meta name="robots" content="index,follow" />
  <meta name="revisit-after" content="28 days" />

<link rel="shortcut icon" href="http://www.adviesenzo.nl/favicon.ico" />
<link rel="bookmark icon" href="http://www.adviesenzo.nl/favicon.ico" />
<link rel="icon" href="http://www.adviesenzo.nl/favicon.ico" type="image/ico" />

<!-- The below CSS uses the id #csslayout to distinquish between the first and the second table -->
<!-- In normal use, you would not need this id -->
<style type="text/css">
	BODY { background-color: #FFFFFF; color: #000000; font-family: verdana; font-size: 10pt; }
	BODY { background-color: #FFFFFF; color: #000000; margin: 0px; padding: 0px; }

	IMG { border: 0px none; }

	CODE, PRE { display: block; margin: 6px 0px 15px 30px; padding: 10px 10px 20px 15px; border-left: 1px solid #CC0000; font-size: 9pt; line-height: 110%; color: #000000; background-color: #EEEEEE; width: 90%; overflow: auto; overflow-y: hidden; }
	html>body CODE, html>body PRE { padding-bottom: 8px; }

	DIV.head { color: #FFFFFF; background-color: #CC0000; padding: 16px 20px 16px 20px; }
	DIV.inhoud { margin: 30px 40px 0px 108px; }
	H2 { font-size: 12pt; font-weight: bold; margin: 12px 0px 0px 0px; }
	H3 { font-size: 10pt; font-weight: bold; margin: 12px 0px 0px 0px; }
	P { margin: 5px 0px 0px 0px; }

	#csslayout .scripttiming TH.marker,
	#csslayout .scripttiming .markerrow TD.marker,
	#csslayout .scripttiming .script { border-left: 1px solid #C0C0C0; }

	#csslayout .scripttiming TH { padding: 1px 5px; border-right: 1px solid #C0C0C0; border-top: 2px solid #C0C0C0; border-bottom: 2px solid #C0C0C0; width: 90px; font-size: 90%; }
	#csslayout .scripttiming TD { padding: 1px 10px; border-right: 1px solid #C0C0C0; border-bottom: 1px solid #C0C0C0; width: 90px; }

	#csslayout .scripttiming TD.numeric { text-align: right; padding: 1px 10px; }
	#csslayout .scripttiming TR.marker_scriptrow TD { color: #000000; background-color: #EEEEEE; font-size: 80%; }
	#csslayout .scripttiming TR .marker { width: 350px; }
	#csslayout .scripttiming TR.marker_scriptrow TD.script { width: 130px; }
	#csslayout .scripttiming TR.marker_scriptrow TD.marker { width: 220px; }
</style>

</head>
<body>

<div class="head">
	<a href="http://www.adviesenzo.nl/index.html" title="Visit the homepage of Advies en zo"><img src="http://www.adviesenzo.nl/images/logo_dpi120.gif" width="411" height="80" alt="Logo Advies en zo, Meedenken en -doen" /></a>
</div>

<div class="inhoud">';

if( DEBUG === true ) $timer->set_marker( 'HTML head send', 'Script output - top' );


// Just do some more things to record some markers

$string = 'this is just an example';
$string = substr( $string, strpos( $string, 'is' ), strlen( 'is' ) );
if( DEBUG === true ) $timer->set_marker( 'Processing something 1', 'Retrieving content' );


ob_start();
var_dump();
ob_end_clean();
if( DEBUG === true ) $timer->set_marker( 'Processing something 2', 'Retrieving content' );


if( @file_exists( __FILE__ ) ) {
	filesize( __FILE__ );
	fileatime( __FILE__ );
	filemtime( __FILE__ );
}
if( DEBUG === true ) $timer->set_marker( 'Processing something 3', 'Retrieving content' );

for( $i=0; $i < 20000; $i++ ) {;}
if( DEBUG === true ) $timer->set_marker( 'Do something else' );

for( $i=0; $i < 5000; $i++ ) {;}
if( DEBUG === true ) $timer->set_marker( 'Do something more and pause the timer', null, true );

// This will not be visible as the timer is paused
sleep( 2 );

// Unpause the timer
if( DEBUG === true ) $timer->unpause();
for( $i=0; $i < 10; $i++ ) { usleep( 50000 ); }
if( DEBUG === true ) $timer->set_marker( 'This usleep() is visible again as we unpaused the timer' );

for( $i=0; $i < 10000; $i++ ) {;}
if( DEBUG === true ) $timer->set_marker( 'Do yet some more' );


print'
	<h2>Examples of output from the script_timer class</h2>
	<p>
		Have a look at the php source code of this file to see how to use this class.
	</p>
	<p>
		Have a look at the HTML source code of this page to see how you can change the layout of the
		results using CSS.
	</p>
	<p>
		For lots more information on the available methods and variables in this class, have a look at the extensive documentation which was provided with this class.
	</p>';

if( DEBUG === true ) $timer->set_marker( 'Introduction printed', 'Script output - content' );

if( DEBUG === true ) {
	// Display the timer results
	print '<h3>Automatic display with precision 4</h3>';
	$timer->get_output( true, 4, null, true, 'End marker', 'Script output - content' );


	// Oh hang on, let's retrieve it as a string instead and display it ourselves
	$htmlstring = $timer->get_output( false, 6 );
	print '<h3>Manual display with precision 6 and CSS adjusted layout</h3><div id="csslayout">' . $htmlstring . '</div>';

	// Oh heck, let's also just retrieve the result array and dump it to the screen
	// so you know what it looks like
	print '<h3>Result array dump</h3><pre>';
	print_r( $timer->scripttiming );
	print '</pre>';
}
else {
	print 'Nothing here, we are not in debugging mode';
}

print '</div></body></html>';

?>
Return current item: Script timer