Pagina snelheid testen
Nee he, niet weer zo'n suf timing script, toch wel :D! Wat doet het? Dit script doet een request willekeurige URLs, die je onbeperkt kunt toevoegen. Het aantal requests in instelbaar. Hiervan zal de tijd bepaald worden en kun je dus zien hoe snel je script/site is. Waarom? Soms wil je experimenteren met de snelheid van je script, omdat een loopje met een test niet altijd even betrouwbaar is en niet bruikbaar in het geval van includes, een bestand kun je nooit 100x includen. Uitslag De uitslag zal netjes worden weergegeven in een tabel, gesorteerd van snel naar langzaam, inclusief index. 100 is dus het snelste script, 150 betekent dat het volgende script (url) 50% langzamer is. Wat niet? Ga geen andere sites belasten met 100-en requests, dit zullen de beheerders van de website niet waarderen, de server zal het hier zwaar mee krijgen. Update Ik heb nog even het eea zitten testen, nu blijkt dat een fopen wacht met het openen van de connectie totdat de pagina klaar is. Hierdoor kan de download bespaard blijven, dit scheelt bandbreedte, en maakt de test minder afhankelijk van je i-net verbinding.
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]
Reacties
0