benchMark.php
Code (php)
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
/*
Function benchMark
- serverside benchmark
- short_summary will also clean up the global $benchMark
*/
function benchMark($action, $name = FALSE, $show = FALSE) {
global $benchMarks;
$timeNow = microtime(true);
$timeDiff = 0;
$memoryNow = memory_get_usage();
$memoryDiff = 0;
switch($action) {
case "start": // start benchmark
$benchMarks[$name]["start_time"] = $timeNow;
$benchMarks[$name]["start_memory"] = $memoryNow;
return (!$show)? TRUE : "Benchmark ".$name." (".$action.") - Time Expired: ".round($timeNow,3)." seconds, Memory Usage: ".convertBytes($memoryNow)."\r\n";
break;
case "status": // show status of benchmark til now without stopping it
$timeDiff = ($timeNow - $benchMarks[$name]["start_time"]);
$memoryDiff = ($memoryNow - $benchMarks[$name]["start_memory"]);
return (!$show)? TRUE : "Benchmark ".$name." (".$action.") - Time Expired: ".round($timeDiff,3)." seconds, Memory Usage: ".convertBytes($memoryDiff)."\r\n";
break;
case "stop": // stop the benchmark
$benchMarks[$name]["end_time"] = $timeNow;
$benchMarks[$name]["end_memory"] = $memoryNow;
$timeDiff = ($benchMarks[$name]["end_time"] - $benchMarks[$name]["start_time"]);
$memoryDiff = ($benchMarks[$name]["end_memory"] - $benchMarks[$name]["start_memory"]);
return (!$show)? TRUE : "Benchmark ".$name." (".$action.") - Time Expired: ".round($timeDiff,3)." seconds, Memory Usage: ".convertBytes($memoryDiff)."\r\n";
break;
case 'short_summary': // calculate benchmark totals, all benchmark have to be stopted
case 'long_summary': // show each benchmark (separate), all benchmark have to be stopted
default:
$returnValue = '';
foreach($benchMarks AS $key => $valueArray) {
/* check if items are set */
if(!isset($valueArray["start_time"])) {
return 'Benchmark start time not set for '.$key."\r\n";
} elseif(!isset($valueArray["end_time"])) {
return 'Benchmark end time not set for '.$key."\r\n";
} elseif(!isset($valueArray["start_memory"])) {
return 'Benchmark start memory not set for '.$key."\r\n";
} elseif(!isset($valueArray["end_memory"])) {
return 'Benchmark end memory not set for '.$key."\r\n";
}
if($action == 'short_summary') {
$timeDiff += ($valueArray["end_time"] - $valueArray["start_time"]);
$memoryDiff += ($valueArray["end_memory"] - $valueArray["start_memory"]);
} elseif($action == 'long_summary') {
$timeDiff = ($valueArray["end_time"] - $valueArray["start_time"]);
$memoryDiff = ($valueArray["end_memory"] - $valueArray["start_memory"]);
$returnValue .= "Benchmark ".$key." (".$action.") - Time Expired: ".round($timeDiff,3)." seconds, Memory Usage: ".convertBytes($memoryDiff)."\r\n";
} else {
return 'Benchmark Error: missing action!'."\r\n";
}
}
if($action == 'short_summary') {
$benchMarks = ''; // little clean up
return "Benchmark ".$action." - Time Expired: ".round($timeDiff,3)." seconds, Memory usage: ".convertBytes($memoryDiff)."\r\n";
} elseif($action == 'long_summary') {
return $returnValue;
} else {
return 'Benchmark Error!'."\r\n";
}
break;
}
}
/*
Function convertBytes
- bytes to the highest storage capacity type)
*/
function convertBytes($size) {
$unit=array('b','kb','mb','gb','tb','pb');
return @round($size/pow(1024,($i=floor(log($size,1024)))),2).' '.$unit[$i];
}
?>
/*
Function benchMark
- serverside benchmark
- short_summary will also clean up the global $benchMark
*/
function benchMark($action, $name = FALSE, $show = FALSE) {
global $benchMarks;
$timeNow = microtime(true);
$timeDiff = 0;
$memoryNow = memory_get_usage();
$memoryDiff = 0;
switch($action) {
case "start": // start benchmark
$benchMarks[$name]["start_time"] = $timeNow;
$benchMarks[$name]["start_memory"] = $memoryNow;
return (!$show)? TRUE : "Benchmark ".$name." (".$action.") - Time Expired: ".round($timeNow,3)." seconds, Memory Usage: ".convertBytes($memoryNow)."\r\n";
break;
case "status": // show status of benchmark til now without stopping it
$timeDiff = ($timeNow - $benchMarks[$name]["start_time"]);
$memoryDiff = ($memoryNow - $benchMarks[$name]["start_memory"]);
return (!$show)? TRUE : "Benchmark ".$name." (".$action.") - Time Expired: ".round($timeDiff,3)." seconds, Memory Usage: ".convertBytes($memoryDiff)."\r\n";
break;
case "stop": // stop the benchmark
$benchMarks[$name]["end_time"] = $timeNow;
$benchMarks[$name]["end_memory"] = $memoryNow;
$timeDiff = ($benchMarks[$name]["end_time"] - $benchMarks[$name]["start_time"]);
$memoryDiff = ($benchMarks[$name]["end_memory"] - $benchMarks[$name]["start_memory"]);
return (!$show)? TRUE : "Benchmark ".$name." (".$action.") - Time Expired: ".round($timeDiff,3)." seconds, Memory Usage: ".convertBytes($memoryDiff)."\r\n";
break;
case 'short_summary': // calculate benchmark totals, all benchmark have to be stopted
case 'long_summary': // show each benchmark (separate), all benchmark have to be stopted
default:
$returnValue = '';
foreach($benchMarks AS $key => $valueArray) {
/* check if items are set */
if(!isset($valueArray["start_time"])) {
return 'Benchmark start time not set for '.$key."\r\n";
} elseif(!isset($valueArray["end_time"])) {
return 'Benchmark end time not set for '.$key."\r\n";
} elseif(!isset($valueArray["start_memory"])) {
return 'Benchmark start memory not set for '.$key."\r\n";
} elseif(!isset($valueArray["end_memory"])) {
return 'Benchmark end memory not set for '.$key."\r\n";
}
if($action == 'short_summary') {
$timeDiff += ($valueArray["end_time"] - $valueArray["start_time"]);
$memoryDiff += ($valueArray["end_memory"] - $valueArray["start_memory"]);
} elseif($action == 'long_summary') {
$timeDiff = ($valueArray["end_time"] - $valueArray["start_time"]);
$memoryDiff = ($valueArray["end_memory"] - $valueArray["start_memory"]);
$returnValue .= "Benchmark ".$key." (".$action.") - Time Expired: ".round($timeDiff,3)." seconds, Memory Usage: ".convertBytes($memoryDiff)."\r\n";
} else {
return 'Benchmark Error: missing action!'."\r\n";
}
}
if($action == 'short_summary') {
$benchMarks = ''; // little clean up
return "Benchmark ".$action." - Time Expired: ".round($timeDiff,3)." seconds, Memory usage: ".convertBytes($memoryDiff)."\r\n";
} elseif($action == 'long_summary') {
return $returnValue;
} else {
return 'Benchmark Error!'."\r\n";
}
break;
}
}
/*
Function convertBytes
- bytes to the highest storage capacity type)
*/
function convertBytes($size) {
$unit=array('b','kb','mb','gb','tb','pb');
return @round($size/pow(1024,($i=floor(log($size,1024)))),2).' '.$unit[$i];
}
?>