Location: PHPKode > scripts > HostingBenchmark > hostingbenchmark/tests/HttpDownloadTest.php
<?php

/***************************************************************
*  (c) 2006-2007 Askywhale (hosting[a]askywhale.com)
*  All rights reserved
*
*  This script is part of HostingBenchmark.com. This project is
*  free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License, or
*  (at your option) any later version.
*
*  The GNU General Public License can be found at
*  http://www.gnu.org/copyleft/gpl.html.
*  A copy is found in the textfile GPL.txt and important notices to the license
*  from the author is found in LICENSE.txt distributed with these scripts.
*
*  This script is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
***************************************************************/

define('HTTPDOWNTEST_BASE_ITERATIONS',1);
define('HTTPDOWNTEST_FAILED_DURATION',1000);
define('HTTPDOWNTEST_BASE_BYTES',1024*32); //in order to have a 1s test showing "1s"

class HttpDownloadTest extends AbstractTest {
  var $totalTime;
  function getTitle(){ return "HTTP Download Test"; }
  function getDescription(){ return "Will download from other big websites. Connection times are not observed."; }
  function getDefaultOn(){ return true; }
  function isBenchmark(){ return true; }
  function doTest(){ 
    $this->txtResult = OK_STR;
    //alexa's top 500 websites
    $websites = array(
      'http://www.google.com/images/logo.gif'=>5000, //max 5000
      'http://www.msn.com'=>5000, //max 20000
      'http://www.myspace.com'=>10000, //max 20000
      'http://www.yahoo.com'=>5000, //max 5000
      //too slow here 'http://post.baidu.com/f?kw=baidu'=>5000,
      //too slow here 'http://www.qq.com/'=>10000,
      'http://www.youtube.com/'=>5000, //max 5000
      'http://search.live.com/results.aspx?q=live'=>4000, //max 4000
      'http://www.megaupload.com'=>5000, //max 5000
      'http://images3.orkut.com/img/tr4.gif'=>2000, //max 2000      
    );
    $times = array();
    for($i=0;$i<HTTPDOWNTEST_BASE_ITERATIONS;$i++) {      
      foreach($websites as $website=>$maxlength) {
        if(!isset($times[$website]) || $times[$website] != HTTPDOWNTEST_FAILED_DURATION) {
          $f = @fopen($website,'rb');
          if($f==false) {
            $this->addError("Failed reaching ".$website);
            $times[$website] = HTTPDOWNTEST_FAILED_DURATION;
            continue;
          }
          $length = 0;
          $startTime = microtime_float();
          while(($s = @fgets($f,256)) && ($length<$maxlength))
            $length += strlen($s);        
          if($length<$maxlength) {
            $this->addError("Failed reading ".$website);
            $times[$website] = HTTPDOWNTEST_FAILED_DURATION;
            continue;
          }
          if(!isset($times[$website]))
            $times[$website] = 0;
          $times[$website] += microtime_float()-$startTime;
          fclose($f);
        }
      }
    }
    //counting only 3/4 the times, the quicker
    $bpss = array();   
    foreach($websites as $website=>$maxlength)
      $bpss[$website] = $maxlength/$times[$website];
    arsort($bpss);
    $j = 0;
    $nbDone = 0;
    $this->totalTime = 0;
    foreach($bpss as $bps) {
      if($j<=count($bpss)*.75) {
        //final time is NOT real time, but time it would take to...
        //$this->totalTime+=$times[$website];
        $this->totalTime+=HTTPDOWNTEST_BASE_BYTES/$bps;
        $nbDone++;
      }  
      $j++; 
    }   
    $this->totalTime*=$j/$nbDone;     
  }
  function doBenchmark() {
    $this->doTest();
    return $this->totalTime;
  }
  function isValid(){ return $this->totalTime<HTTPDOWNTEST_FAILED_DURATION; }
  function getTxtResults(){ 
    return $this->txtResult; 
  }
  
  //private
  function addError($str){  
    if(strpos($this->txtResult,$str)!==false)
      return;
    if($this->txtResult==OK_STR)
      $this->txtResult="";
    else
      $this->txtResult .= ", "; 
    $this->txtResult .= $str;  
  }
}

?>
Return current item: HostingBenchmark