<?php
function calctime($start,$end)
{
list($msec,$sec)=explode(' ',$start);
$start=(float)$msec+(float)$sec;
list($msec,$sec)=explode(' ',$end);
$end=(float)$msec+(float)$sec;
$time=($end-$start);
return $time;
}
function loop_bench($count=50000)
{
echo '<tr><th colspan="2">Loops</th></tr>';
$start=microtime();
for ($x=0;$x<$count;$x++)
{
$b=$x;
}
$end=microtime();
echo '<tr><td>FOR-Loop from 1 to '.$count.'</td><td>'.calctime($start,$end).'</td></tr>';
$start=microtime();
$x=0;
while ($x<$count)
{
$x++;
}
$end=microtime();
echo '<tr><td>WHILE-Loop from 1 to '.$count.'</td><td>'.calctime($start,$end).'</td></tr>';
}
function sql_bench($host,$username,$password,$rows=5000)
{
echo '<tr><th colspan="2">MySQL</th></tr>';
$db=mysql_connect($host,$username,$password);
$start=microtime();
mysql_query("create database benchtestdb",$db);
$end=microtime();
echo '<tr><td>Create database</td><td>'.calctime($start,$end).'</td></tr>';
$start=microtime();
mysql_select_db("benchtestdb",$db);
$end=microtime();
echo '<tr><td>Select database</td><td>'.calctime($start,$end).'</td></tr>';
$start=microtime();
mysql_query("create table benchtest (a INT(8) UNSIGNED NOT NULL,b INT(8) UNSIGNED NOT NULL,c INT(8) UNSIGNED NOT NULL,d INT(8) UNSIGNED NOT NULL,e INT(8) UNSIGNED NOT NULL)",$db);
$end=microtime();
echo '<tr><td>Create table</td><td>'.calctime($start,$end).'</td></tr>';
$start=microtime();
for ($x=0;$x<$rows;$x++)
{
mysql_query("insert into benchtest (a,b,c,d,e) values ('$x','$x','$x','$x','$x')",$db);
}
$end=microtime();
echo '<tr><td>Write '.$rows.' rows</td><td>'.calctime($start,$end).'</td></tr>';
$start=microtime();
$result=mysql_query("select * from benchtest",$db);
$end=microtime();
echo '<tr><td>Query Table ('.$rows.' rows)</td><td>'.calctime($start,$end).'</td></tr>';
$start=microtime();
while ($row=mysql_fetch_assoc($result))
{
$b=$row;
}
$end=microtime();
echo '<tr><td>Read '.$rows.' rows</td><td>'.calctime($start,$end).'</td></tr>';
$start=microtime();
mysql_query("drop database benchtestdb",$db);
$end=microtime();
echo '<tr><td>Drop database</td><td>'.calctime($start,$end).'</td></tr>';
mysql_close($db);
}
function fs_bench($path,$bytes=50000,$dirs=1000)
{
echo '<tr><th colspan="2">Files</th></tr>';
$file=fopen($path."/benchtest.tmp","w");
$start=microtime();
for ($x=0;$x<$bytes;$x++)
{
fwrite($file,'x');
}
$end=microtime();
echo '<tr><td>Write '.$bytes.' Bytes</td><td>'.calctime($start,$end).'</td></tr>';
fclose($file);
$file=fopen($path."/benchtest.tmp","r");
$start=microtime();
while (!feof($file))
{
$x=fread($file,1);
}
$end=microtime();
echo '<tr><td>Read '.$bytes.' Bytes</td><td>'.calctime($start,$end).'</td></tr>';
fclose($file);
$start=microtime();
unlink($path."/benchtest.tmp");
$end=microtime();
echo '<tr><td>Unlink file</td><td>'.calctime($start,$end).'</td></tr>';
$start=microtime();
for ($x=0;$x<$dirs;$x++)
{
mkdir($path."/benchtest".$x);
}
$end=microtime();
echo '<tr><td>Create '.$dirs.' directories</td><td>'.calctime($start,$end).'</td></tr>';
$start=microtime();
for ($x=0;$x<$dirs;$x++)
{
rmdir($path."/benchtest".$x);
}
$end=microtime();
echo '<tr><td>Remove '.$dirs.' directories</td><td>'.calctime($start,$end).'</td></tr>';
}
echo 'PHP '.phpversion().'<br>';
echo '<table border="1">';
$start=microtime();
loop_bench();
sql_bench("localhost","root","collector");
fs_bench("/var/www/htdocs/test/imap/mailtemp");
$end=microtime();
echo '</table>';
echo 'Total benchmark: '.calctime($start,$end);
?>