speedtest.php
[code]
<?php
// Totaal aantal requests per URL
$iRequestLimit = 20;

$arrURLS[] = "http://www.google.nl";
// $arrURLS[] = ; // Voeg een willekeurig aantal urls in

$arrTimes = array();

foreach($arrURLS as $URL){
	set_time_limit(500);
	
	$oTimer = new Div_Timer();
	
	for($i=0; $i<$iRequestLimit; $i++){
		$URL1 = $URL.'?'.md5(rand());
		if(($fp = fopen($URL1, "rb")) !== false){
			// Downloadcode verwijderd, fopen wacht totdat de pagina compleet is.
			fclose($fp);
		}
	}
	
	$fTime = $oTimer->GetTime();
	
	$arrTimes[] = $fTime;
}

array_multisort($arrTimes, SORT_ASC, SORT_NUMERIC, $arrURLS);

echo '
Total requests: '.$iRequestLimit.'<br />
Ordered by time (fastest above)<br />
<table cellpadding="2" border="1">
<tr><td>URL</td><td>Total Req. time</td><td>AVG Req time</td><td>Index</td></tr>
';

for($i=0; $i<count($arrTimes); $i++){
	
	if($i==0){
		$fIndexedTime = $arrTimes[$i];
	}
	
	echo '<tr><td>'.$arrURLS[$i].'</td><td>'.$arrTimes[$i].' s</td><td>'.($arrTimes[$i]/$iRequestLimit).' s</td><td>'.($arrTimes[$i]/$fIndexedTime*100).'</td></tr>';
}

echo '</table>';


class Div_Timer{
	
	private $_fStartTime;
	private $_fEndTime;
	private $_blnFinished = false;
	private $_strFile;

	/**
	 * Constructor. Autostart the timer.
	 *
	 */
	function __construct($strFile = null){
		if(!is_null($strFile)) $this->SetFile($strFile);
		$this->Start();
	}

	/**
	 * Set the filename to save statistics in
	 *
	 * @param unknown_type $strFile
	 */
	function SetFile($strFile){
		$this->_strFile = $strFile;
	}
	
	/**
	 * Start the internal timer. Can also be used for a restart
	 *
	 */
	function Start(){
		$this->_fStartTime = $this->MicrotimeAsFloat();
		$this->_blnFinished = false;
	}

	/**
	 * Can be used static
	 *
	 * @return unknown
	 */
	function MicrotimeAsFloat(){
	    list($usec, $sec) = explode(" ", microtime());
	    return ((float)$usec + (float)$sec);
	}

	/**
	 * Stop the timer. The stoptime will be remembered.
	 *  If a file isset, then the time will be saved.
	 *
	 */
	function Stop(){
		if(!$this->_blnFinished){
			$this->_fEndTime = $this->MicrotimeAsFloat();
			$this->_blnFinished = true;
			
			if(!is_null($this->_strFile)){
				$strPerfData = '';
				$strMyFilename = $_SERVER['REQUEST_URI'];
				$fTimeDiff = ($this->_fEndTime - $this->_fStartTime);
				
				if(file_exists($this->_strFile) && filesize($this->_strFile)){
					$f = fopen($this->_strFile, "r");
					$strPerfData = fread($f, filesize($this->_strFile));
					fclose($f);
				}
				
				if(strpos($strPerfData, $strMyFilename) !== false){
					$iLinestart = strpos($strPerfData, $strMyFilename);
					$iLineend = strpos($strPerfData, ';', $iLinestart);
					
					$strLineData = substr($strPerfData, $iLinestart, $iLineend-$iLinestart);
					list($strFile, $iCount, $fAvgTime) = explode(':', $strLineData);
					$iCount = intval($iCount) + 1;
					$fAvgTime = (floatval($fAvgTime) * ($iCount-1) + $fTimeDiff) / $iCount;
					
					$strPerfData = substr_replace($strPerfData, $strMyFilename.':'.$iCount.':'.$fAvgTime, $iLinestart, $iLineend-$iLinestart);
				}
				else {
					$strPerfData .= $strMyFilename.':1:'.$fTimeDiff.";\r\n";
					
				}
				
				$f = fopen($this->_strFile, "w+");
				fwrite($f, $strPerfData);
				fclose($f);
			}
		}
	}

	/**
	 * Stop the timer and reset the data
	 *
	 */
	function Reset(){
		$this->_fStartTime = 0;
		$this->_fEndTime=0;
		$this->_blnFinished = true;
	}

	/**
	 * Get the time in seconds
	 *
	 * @return timing time in seconds
	 */
	function GetTime(){
		if($this->_blnFinished){
			return ($this->_fEndTime - $this->_fStartTime);
		}
		else{
			return ($this->MicrotimeAsFloat() - $this->_fStartTime);
		}
	}
}

?>
[/code]