<?php
// Inspired from http://stackoverflow.com/questions/555523/file-get-contents-vs-curl-what-has-better-performance
$memory = array();
$errors = array("FGC" => 0, "cURL" => 0);
declare(ticks=1);
register_tick_function(function()
{
global $memory;
$mem = memory_get_usage() - $memory["over"];
$memory["high"] = max($memory["high"], $mem);
$memory["low"] = min($memory["low"], $mem);
$memory["avg"] = ($memory["avg"] * $memory["iterations"] + $mem) / ($memory["iterations"] + 1);
$memory["iterations"]++;
});
function resetMemoryInfo()
{
global $memory;
$memory = array("over" => memory_get_usage(),"high" => 0, "low" => 0, "iterations" => 0, "avg" => 0);
}
function Benchmark($function, $arguments = null, $iterations = 10000, $report = TRUE)
{
set_time_limit(0);
global $memory;
$start = array();
print "\r\n!!! Benchmarking function $function for $iterations iteration (args:".json_encode($arguments).")...\r\n";
resetMemoryInfo();
if (is_callable($function) === true)
{
for ($i = 1; $i <= $iterations; ++$i)
{
$start["time"] = microtime(true);
resetMemoryInfo();
call_user_func_array($function, (array) $arguments);
$end["memory"][] = $memory["high"];
resetMemoryInfo();
$end["time"][] = round(microtime(true) - $start["time"], 8);
if($i % 1000 == 0 && $iterations > 1000) print "--- Done $i iterations...\r\n";
else if($i % 100 == 0 && $iterations > 100 && $iterations < 1001) print "--- Done $i iterations...\r\n";
}
foreach(array("time", "memory") as $label)
{
$ret[$label]["total"] = array_sum($end[$label]);
$ret[$label]["min"] = min($end[$label]);
$ret[$label]["max"] = max($end[$label]);
$ret[$label]["avg"] = $ret[$label]["total"] / $iterations;
}
if($report)
{
print "===================\r\n";
print "Results:\r\n";
print "===================\r\n";
foreach($ret as $label => $data)
{
foreach($data as $subLabel => $subData)
print "$label $subLabel:\t\t$subData\r\n";
}
}
"Benchmark completed!\r\n";
return $ret;
}
return false;
}