Test name |
Loop #1 |
Loop #2 |
Loop #3 |
Average |
' . htmlentities ($name, ENT_COMPAT, 'utf-8') . ' | ';
$failed = false;
$sum = 0;
for ($i = 0; $i < 3; ++$i)
{
$time = microtime (true);
if (call_user_func_array ($callback, $params))
{
$diff = microtime (true) - $time;
$sum += $diff;
echo '
' . round ($diff * 1000) . ' ms | ';
}
else
{
$failed = true;
echo '
failed | ';
}
}
if ($failed)
echo '
failed |
';
else
echo '
' . round ($sum * 1000 / 3) . ' ms |
';
}
function bench_skip ($name)
{
echo '
' . htmlentities ($name, ENT_COMPAT, 'utf-8') . ' |
skipped |
skipped |
skipped |
skipped |
';
}
function test_proc_loop ($loops)
{
for ($i = 0; $i < $loops; )
++$i;
return true;
}
function test_disk_o ($loops, $size)
{
$buffer = str_repeat ('$', $size);
for ($i = 0; $i < $loops; ++$i)
{
$f = fopen ('dummy', 'wb');
fwrite ($f, $buffer);
fclose ($f);
}
unlink ('dummy');
return true;
}
function test_disk_r ($loops, $size)
{
$f = fopen ('dummy', 'wb');
fwrite ($f, str_repeat ('$', $size));
fclose ($f);
for ($i = 0; $i < $loops; ++$i)
{
$f = fopen ('dummy', 'rb');
fread ($f, $size);
fclose ($f);
}
unlink ('dummy');
return true;
}
function test_disk_w ($loops, $size)
{
$buffer = str_repeat ('$', $size);
for ($i = 0; $i < $loops; ++$i)
{
$f = fopen ('dummy', 'wb');
fwrite ($f, $buffer);
fclose ($f);
unlink ('dummy');
}
return true;
}
function test_mem_alloc ($loops, $size)
{
for ($i = 0; $i < $loops; ++$i)
{
$buffer = str_repeat ('$', $size);
unset ($buffer);
}
return true;
}
function test_net_dl ($loops, $size)
{
for ($i = 0; $i < $loops; ++$i)
{
$ch = curl_init ('http://test-debit.free.fr/image.iso');
if ($ch === false)
return false;
curl_setopt ($ch, CURLOPT_RANGE, '0-' . ($size - 1));
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
if (curl_exec ($ch) === false)
return false;
curl_close ($ch);
}
return true;
}
function test_net_ul ($loops, $size)
{
$buffer = str_repeat ('$', $size);
for ($i = 0; $i < $loops; ++$i)
{
$ch = curl_init ('http://test-debit.free.fr');
if ($ch === false)
return false;
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, array ('buffer' => $buffer));
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
if (curl_exec ($ch) === false)
return false;
curl_close ($ch);
}
return true;
}
bench_perform ('Memory : allocate and release 1 MB, 100 times', 'test_mem_alloc', array (100, 1024 * 1024));
bench_perform ('Memory : allocate and release 8 MB, 10 times', 'test_mem_alloc', array (10, 8 * 1024 * 1024));
if (function_exists ('curl_init'))
{
bench_perform ('Network : download 100 B, 10 times', 'test_net_dl', array (10, 100));
bench_perform ('Network : download 10 KB, 5 times', 'test_net_dl', array (5, 10 * 1024));
bench_perform ('Network : upload 100 B, 10 times', 'test_net_ul', array (10, 100));
bench_perform ('Network : upload 10 KB, 5 times', 'test_net_ul', array (5, 10 * 1024));
}
else
bench_skip ('Network : cannot perform, cURL not available');
bench_perform ('Processor : empty loop, 10^5 iterations', 'test_proc_loop', array (100000));
bench_perform ('Processor : empty loop, 10^6 iterations', 'test_proc_loop', array (1000000));
bench_perform ('Storage : read 100 B from file, 20 times', 'test_disk_r', array (20, 100));
bench_perform ('Storage : read 10 KB from file, 10 times', 'test_disk_r', array (10, 10 * 1024));
bench_perform ('Storage : read 1 MB from file, 5 times', 'test_disk_r', array (5, 1024 * 1024));
bench_perform ('Storage : write 100 B without flush, 20 times', 'test_disk_w', array (20, 100));
bench_perform ('Storage : write 10 KB without flush, 10 times', 'test_disk_w', array (10, 10 * 1024));
bench_perform ('Storage : write 1 MB without flush, 5 times', 'test_disk_w', array (5, 1024 * 1024));
bench_perform ('Storage : write 100 B with flush, 20 times', 'test_disk_o', array (20, 100));
bench_perform ('Storage : write 10 KB with flush, 10 times', 'test_disk_o', array (10, 10 * 1024));
bench_perform ('Storage : write 1 MB with flush, 5 times', 'test_disk_o', array (5, 1024 * 1024));
?>