Location: PHPKode > scripts > HostingBenchmark > hostingbenchmark/includes/tools.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('VERSION',1);
define('DIRECTORY_TESTS','tests/');
define('DEFAULT_PASS',10);
define('URL_RESULTS','http://hostingbenchmark.org/send_results.php');

include_once(DIRECTORY_TESTS.'AbstractTest.php');

function getLang() {
  if(!isset($_SESSION['language']))
    return 'english';
  return $_SESSION['language'];
}

function getAvailableLangs() {
  return array('english','french');
}

function addError($newError) {
  global $error;
  if($error != '')
    $error .= " ; ";
  $error .= $newError;
}

function compareTest($a,$b) {
  $va = 0;
  $vb = 0;
  if($a->isBenchmark()) $va+=1000;
  if($b->isBenchmark()) $vb+=1000;
  $va+=ord(substr($a->getTitle(),0,1));
  $vb+=ord(substr($b->getTitle(),0,1));
  if($va>$vb)
    return 1;
  else if($va<$vb)
    return -1;
  else
    return 0;
}

function getAvailableTests() {
  $res = array();
  $dh = opendir(DIRECTORY_TESTS);
  while ($dh && ($fileName = readdir($dh))) {
    if(strstr($fileName,'.php')!='') {
      include_once(DIRECTORY_TESTS.$fileName);
      $testName = substr($fileName,0,-4);
      $className = $testName; 
      if(class_exists($className)&&$className!='AbstractTest') {
        $res[$testName] = new $className();
        $res[$testName]->init();
      }
    }
  }
  if($dh!==false)
    closedir($dh);
  uasort($res,"compareTest");
  return $res;
}

function microtime_float() {
	list($usec, $sec) = explode(" ", microtime());
	return ((float)$usec + (float)$sec);
}

function getAvgResult($test, $results) {    
  $sum = 0;
  if(!is_array($results)||count($results)==0)
    return -1;
  sort($results); 
/*    $lim = count($_SESSION['results'][$k]);
    if($_SESSION['forms']['setmainparameters']['ignoreworst'])
*/    $lim = ceil(count($results)*0.8);
  $nb = 0;
  for($i=0;$i<$lim;$i++) {
    $nb ++;
    $sum += $results[$i]; 
  }
  if ($nb==0)
    return -1;
  else
    return $sum/$nb;
}

function cutString($s,$limit) {
  if(strlen($s)>$limit)
    return substr($s,0,$limit)."\n".cutString(substr($s,$limit),$limit);
  return $s;
}

function resetResults() {
  $_SESSION['currentTest'] = 0;
  $_SESSION['currentPass'] = 0;
  $_SESSION['results'] = array();   
}

function getStringParameters() {
  $res = array();
  $res['tests'] = array(); 
  foreach($_SESSION['tests'] as $k=>$test) {
    $res['tests'][$k]['title'] = $test->getTitle();
    $res['tests'][$k]['release'] = $test->getRelease();
    $res['tests'][$k]['parameters'] = $test->getParameters();        
  }
  $res['main']['pass'] = $_SESSION['forms']['setmainparameters']['pass'];
  $res['main']['time'] = time();
  return base64_encode(serialize($res));
}

function setStringParameters($stringParameters) {
  global $availableTest, $error, $lang;
  $stringParameters = str_replace(
    array("\n","\r","\t"," "),array("","","",""),
    $stringParameters);
  $tmpTab = base64_decode($stringParameters);
  if($tmpTab===false) {
    addError($lang['bad_parameters_string']);
    return;
  }    
  $tab = unserialize($tmpTab);
  if($tab===false||!is_array($tab)||count($tab)<2) {
    addError($lang['bad_parameters_string']);
    return;
  }    
  resetResults();
  $_SESSION['tests'] = array();
  $notFound = array();
  foreach($tab['tests'] as $test) {
    $found = false;
    foreach($availableTest as $atest) {      
      if($atest->getTitle()==$test['title'] &&
          $atest->getRelease()==$test['release']) {
        $found = true;
        $atest->setParameters($test['parameters']);
        $_SESSION['tests'][] = $atest;
      } 
    }
    if(!$found)
      $notFound[] = getReleaseVersionTxt($test['title'],$test['release']);
  }
  $_SESSION['forms']['setmainparameters']['pass'] = $tab['main']['pass'];
  if(count($notFound)>0) 
    addError(sprintf($lang['not_found_tests'],implode(",", $notFound)));
}

function getStringResults() {
  $res = array();
  $res['tests'] = array(); 
  foreach($_SESSION['tests'] as $k=>$test) {
    $res['tests'][$k]['title'] = $test->getTitle();
    $res['tests'][$k]['release'] = $test->getRelease();
    $res['tests'][$k]['parameters'] = $test->getParameters();
    $res['tests'][$k]['isbenchmark'] = $test->isBenchmark();
    $res['tests'][$k]['isvalid'] = $test->isValid();
    $res['tests'][$k]['txtresults'] = $test->getTxtResults();
    $res['tests'][$k]['duration'] = getAvgResult($test,$_SESSION['results'][$k]);        
  }
  $res['main']['pass'] = $_SESSION['forms']['setmainparameters']['pass'];
  $res['main']['time'] = time();
  $res['main']['ip'] = $_SERVER['SERVER_ADDR'];
  return base64_encode(serialize($res));
}
/* no sense
function setUpAll(){
  foreach($_SESSION['tests'] as $test)
    $test->setUp();
}

function tearDownAll(){
  foreach($_SESSION['tests'] as $test)
    $test->tearDown();
}
*/

?>
Return current item: HostingBenchmark