tests user functions for speed

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php
/**
* tests user functions for speed
*
* @param string $medthod_name - user func
* @param array $args - arguments
* @param int $iterations - number of calls
* @param bool $showArguments - print argument to output hash. It can be usefull if arguments has a lof of text
* @param bool $printOutput - print output of user funcs or not
* @param bool $flushReturns - flushing methods return for memory cleaning if return makes a lot of stuff
* @return array - middle time of all iterations and total time of test
*/
function speedTest($medthod_name, $args = array(), $iterations = 1000, $showArguments = false, $printOutput = false, $flushReturns = false){
if(!is_callable($medthod_name))
{
print "Method $medthod_name isn't callable!";
die;
}
$ttime = microtime(true);
$times = $output = array();
for ($i = 0; $i < $iterations; ++$i)
{
ob_start();
$st = microtime(true);
$output[] = call_user_func_array($medthod_name,$args);
$times[] = microtime(true) - $st;
if($printOutput == true)
{
$content[] = ob_get_flush();
}else{
$content[] = ob_get_clean();
}
if($flushReturns == true) $output = array();
}
$mtime = array_sum($times) / count($times);
$ttime = microtime(true) - $ttime;
$arr = array(
'medthodName' => $medthod_name,
'arguments' => $args,
'iteratioons' => $iterations,
'middleTime' => $mtime,
'totalTime' => $ttime,
'output' => $output,
'content' => $content
);
if($showArguments == false) unset($arr['arguments']);
return $arr;
}