<?php
/*
Dit is enkel de source, gebruik de downloads om te testen!
*/
?>

======bench_main.phpcli======
<?php
//constants
require ('bench_conf.phpcli');

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

$basestart = getmicrotime();

function hugevar() {
  $nr = 0;
  while ($nr < (1024*1024)) { //1 meg var
    $hugevar .= rand(0, 9);
    $nr++;
  }
  return $hugevar;
}

function leet($key) {  
  $known = array('o', 'e', '<', '>', 'w', 'a');
  $leet = array('0', '3', '*', '*', '&#092;&#092;\'', '4');
  if(rand(0, 1) == 1) {
    $key = strtoupper($key);
  } else {
    $key = strtolower($key);
  }
  $key = str_replace($known, $leet, $key);
  return stripslashes($key);
}

function leetsite ($url) { //original in http://www.phphulp.nl/php/scripts/7/303/
    $root = $_SERVER['DOCUMENT_ROOT'];
    $cont = file($url);
    if ($cont) {
      foreach($cont as $line) {
        $line = strtolower($line);
        $line = str_replace('src="/', 'src="', $line);
        $line = str_replace('href="/', 'href="', $line);
        $line = str_replace('background="/', 'background="', $line);
        $line = preg_replace('/src="(?!http:\/\/|ftp:\/\/)(.+?)"/', 'src="'.$root.'\\1"', $line);
        $line = preg_replace('/href="(?!http:\/\/|ftp:\/\/|mailto:)(.+?)"/', 'href="'.$root.'\\1"', $line);
        $line = preg_replace('/background="(?!http:\/\/)(.+?)"/', 'background="'.$root.'\\1"', $line);
        $bytes = preg_split('//', str_replace('&nbsp;', ' ', $line));
        foreach ($bytes as $byte) {
          if ($byte == '<') {
            $tagopen = 'yes';
          }
          if ($tagopen == 'yes' && $byte == '>') {
            $tagopen = 'no';
          }
          if ($tagopen == 'no' && $byte != '<' && $byte != '>') {
            $out =  leet($byte);
          }
          if ($byte == '<' || $byte == '>' || $tagopen == 'yes') {
            $out =  $byte;
          }
        }
      }
    }
    return $out;
  }

function runrandtest($def_val, $def_range, $def_deci) {
  $start = getmicrotime();
  $ref2 = $def_range/2;
  $i = 0;
  $total = 0;
  while ($i < $def_val) {
    $val = rand(0, $def_range);
    $total += $val;
    $i++;
  }
  $ref = $total/$def_val;
  if ($ref > $ref2) {
    $dev = 100-$ref2/$ref*100;
  } elseif ($ref < $ref2) {
    $dev = 100-$ref/$ref2*100;
  } else {
    $dev = 0;
  }
  $micro = getmicrotime()-$start;
  return array('dev' => $dev, 'micro' => $micro);
}

echo "\r\n==PHP CLI Benchmark v0.1==\r\n";

$varbegin = getmicrotime();
echo "\r\nCreating variables";
$var1 = hugevar();
echo ".";
$var2 = hugevar();
echo ".";
$varend = getmicrotime();
$vartime = round($varend-$varbegin, $def_deci);

echo "\r\nRunning rand() deviation test";
$j = 0;
while($j < $def_numtests) {
  $funcout = runrandtest($def_val, $def_range, $def_deci);
  echo ".";
  $devs += $funcout['dev'];
  $micros += $funcout['micro'];
  $j++;
}

echo "\r\nRunning md5 sha1 hash creation test";
$k = 0;
while ($k < $def_numtests) {
  $countmd5 = 0;
  while ($countmd5 < $def_md5) {
    $md5start = getmicrotime();
    $newhash = md5($var1);
    $newhash2 = sha1($var2);
    unset($newhash, $newhash2);
    $countmd5++;
    $md5time += getmicrotime()-$md5start;
  }
  echo ".";
  $k++;
}

echo "\r\nRunning math test";
$l = 0;
while ($l < $def_numtests) {
  $countmath = 0;
  while ($countmath < $def_math) {
    $mathstart = getmicrotime();
    $math1 = bindec(decbin(hexdec(dechex(octdec(decoct($var1))))));
    $math2 = pow(sqrt($var2), $var1);
    unset($math1, $math2);
    $countmath++;
    $mathtime += getmicrotime()-$mathstart;
  }
  echo ".";
  $l++;
}

echo "\r\nRunning 1337-alizer (parsing) test";
$m = 0;
while ($m < $def_numtests) {
  $countleet = 0;
  while ($countleet < $def_leet) {
    $leetstart = getmicrotime();
    $leet = leetsite('bench_sitedump.html');
    unset($leet);
    $countleet++;
    $leettime += getmicrotime()-$leetstart;
  }
  echo ".";
  $m++;
}

$plusdev = round($devs/$def_numtests, $def_deci);
$plusmicro = round($micros/$def_numtests, $def_deci);
$plusmd5 = round($md5time/($def_numtests*$def_md5), $def_deci);
$plusmath = round($mathtime/($def_numtests*$def_math), $def_deci);
$plusleet = round($leettime/($def_numtests*$def_leet), $def_deci);

echo "\r\n\r\n";

echo "==while() variable creation test===============================\r\n";
echo "2 1meg variables created in: ".$vartime."s\r\n";
echo "==rand() deviation test========================================\r\n";
echo "Avg deviation: ".$plusdev.". Avg Comptime: ".$plusmicro."s\r\n";
echo "==while() md5 sha1 test========================================\r\n";
echo "Avg hash creation: ".$plusmd5."s\r\n";
echo "==math test====================================================\r\n";
echo "Avg calculation time: ".$plusmath."s\r\n";
echo "==1337-alizer (parsing) test===================================\r\n";
echo "Avg parsing time: ".$plusleet."s\r\n";
echo "\r\nDone in ".round(getmicrotime()-$basestart, $def_deci)."s.\r\n\r\n";
?>

======bench_conf.phpcli======
<?php
//PHP CLI benchmark config
$def_val = 1000000; //numer of rand()'s in one test
$def_range = 1000;  //the range for the rand() function
$def_deci = 10;     //decimals for the results
$def_numtests = 10; //number of tests on each test
$def_leet = 4;      //Number of 1337-alizer runs each test
$def_math = 70;     //Number of math runs each test
$def_md5 = 100;     //Number of hash runs each test
?>