diff --git a/bench.php b/bench.php index c89dec9..4d13e06 100644 --- a/bench.php +++ b/bench.php @@ -25,7 +25,7 @@ set_time_limit(0); ini_set('memory_limit', '512M'); -$line = str_pad("-",78,"-"); +$line = str_pad("-", 78, "-"); $padHeader = 76; $padInfo = 18; $padLabel = 31; @@ -44,21 +44,21 @@ function get_microtime() function convert($size) { - $unit=array('b','kb','Mb','Gb','Tb','Pb','Eb'); - return @round($size/pow(1024,($i=floor(log($size,1024)))),2).' '.$unit[$i]; + $unit = array('b', 'kb', 'Mb', 'Gb', 'Tb', 'Pb', 'Eb'); + return @round($size / pow(1024, ($i = floor(log($size, 1024)))), 2) . ' ' . $unit[$i]; } function prefix_si($size) { - $unit=array(' ', 'k', 'M', 'G', 'T', 'P', 'E', -3 => 'm', -6 => 'u'); - $i=floor(log($size,1000)); + $unit = array(' ', 'k', 'M', 'G', 'T', 'P', 'E', -3 => 'm', -6 => 'u'); + $i = floor(log($size, 1000)); if ($i < 0) { if ($i <= -6) { $i = -6; } elseif ($i <= -3) { $i = -3; } else { - $i=0; + $i = 0; } } return $unit[$i]; @@ -66,30 +66,31 @@ function prefix_si($size) function convert_si($size) { - $i=floor(log($size,1000)); + $i = floor(log($size, 1000)); if ($i < 0) { if ($i <= -6) { $i = -6; } elseif ($i <= -3) { $i = -3; } else { - $i=0; + $i = 0; } } - return @round($size/pow(1000, $i),2); + return @round($size / pow(1000, $i), 2); } /** * Read /proc/cpuinfo, fetch some data */ -function getCpuInfo($fireUpCpu=false) +function getCpuInfo($fireUpCpu = false) { $cpu = array( 'model' => '', 'cores' => 0, - 'mhz' => 0.0, - 'mips' => 0.0 + 'mhz' => 0.0, + 'mips' => 0.0 ); + if (!is_readable('/proc/cpuinfo')) { $cpu['model'] = 'Unknown'; $cpu['cores'] = 1; @@ -98,22 +99,20 @@ function getCpuInfo($fireUpCpu=false) // Code from https://github.com/jrgp/linfo/blob/master/src/Linfo/OS/Linux.php // Adopted - if ($fireUpCpu) { // Fire up CPU $i = 100000000; - while ($i--); + while ($i--) ; } $cpuData = explode("\n", file_get_contents('/proc/cpuinfo')); - foreach ($cpuData as $line) { - $line = explode(':', $line, 2); if (!array_key_exists(1, $line)) { continue; } + $key = trim($line[0]); $value = trim($line[1]); @@ -123,28 +122,34 @@ function getCpuInfo($fireUpCpu=false) case 'model name': case 'cpu': case 'Processor': - if (empty($cpu['model'])) + if (empty($cpu['model'])) { $cpu['model'] = $value; + } break; // Speed in MHz case 'cpu MHz': - if (empty($cpu['mhz']) || $cpu['mhz'] < (float)$value) + if (empty($cpu['mhz']) || $cpu['mhz'] < (float)$value) { $cpu['mhz'] = (float)$value; + } break; case 'Cpu0ClkTck': // Old sun boxes - if (empty($cpu['mhz'])) + if (empty($cpu['mhz'])) { $cpu['mhz'] = (int)hexdec($value) / 1000000.0; + } break; case 'bogomips': // twice of MHz usualy - if (empty($cpu['mhz'])) + if (empty($cpu['mhz'])) { $cpu['mhz'] = (float)$value / 2.0; - if (empty($cpu['mips'])) + } + if (empty($cpu['mips'])) { $cpu['mips'] = (float)$value / 2.0; + } break; // cores case 'cpu cores': - if (empty($cpu['cores'])) + if (empty($cpu['cores'])) { $cpu['cores'] = (int)$value; + } break; } } @@ -152,16 +157,13 @@ function getCpuInfo($fireUpCpu=false) return $cpu; } - $cpuInfo = getCpuInfo(); // CPU throttling? -if (abs($cpu['mips'] - $cpu['mhz']) > 400) { +if (abs($cpuInfo['mips'] - $cpuInfo['mhz']) > 400) { print("
\n<<< WARNING >>>\nCPU is in powersaving mode? Set CPU governor to 'performance'!\nFire up CPU and recalculate MHz!\n
" . PHP_EOL); $cpuInfo = getCpuInfo(true); } - - /** * @return array((int)seconds, (str)seconds, (str)operations/sec), (str)opterations/MHz) */ @@ -181,9 +183,9 @@ function format_result_test($diffSeconds, $opCount) $opmhz_u = prefix_si($opmhz); return array($diffSeconds, number_format($diffSeconds, 3, '.', ''), - number_format($ops_v, 2, '.', '') . ' '.$ops_u, - number_format($opmhz_v, 2, '.', '') . ' '.$opmhz_u, - ); + number_format($ops_v, 2, '.', '') . ' ' . $ops_u, + number_format($opmhz_v, 2, '.', '') . ' ' . $opmhz_u, + ); } else { return array(0, '0.000', 'x.xx ', 'x.xx '); } @@ -194,9 +196,11 @@ function test_01_Math($count = 1400000) $time_start = get_microtime(); $mathFunctions = array('abs', 'acos', 'asin', 'atan', 'decbin', 'dechex', 'decoct', 'floor', 'exp', 'log1p', 'sin', 'tan', 'pi', 'is_finite', 'is_nan', 'sqrt', 'rad2deg'); foreach ($mathFunctions as $key => $function) { - if (!function_exists($function)) unset($mathFunctions[$key]); + if (!function_exists($function)) { + unset($mathFunctions[$key]); + } } - for ($i=0; $i < $count; $i++) { + for ($i = 0; $i < $count; $i++) { foreach ($mathFunctions as $function) { $r = call_user_func_array($function, array($i)); } @@ -208,7 +212,7 @@ function test_02_String_Concat($count = 14000000) { $time_start = get_microtime(); $s = ''; - for($i = 0; $i < $count; ++$i) { + for ($i = 0; $i < $count; ++$i) { $s .= '- Valar moghulis' . PHP_EOL; } return format_result_test(get_microtime() - $time_start, $count); @@ -220,9 +224,11 @@ function test_03_String_Simple_Functions($count = 1300000) $time_start = get_microtime(); $stringFunctions = array('strtoupper', 'strtolower', 'strrev', 'strlen', 'str_rot13', 'ord', 'trim'); foreach ($stringFunctions as $key => $function) { - if (!function_exists($function)) unset($stringFunctions[$key]); + if (!function_exists($function)) { + unset($stringFunctions[$key]); + } } - for ($i=0; $i < $count; $i++) { + for ($i = 0; $i < $count; $i++) { foreach ($stringFunctions as $function) { $r = call_user_func_array($function, array($stringTest)); } @@ -232,16 +238,20 @@ function test_03_String_Simple_Functions($count = 1300000) function test_04_String_Multibyte($count = 130000) { - global $stringTest; + global $stringTest, $emptyResult; - if (!function_exists('mb_strlen')) return $emptyResult; + if (!function_exists('mb_strlen')) { + return $emptyResult; + } $time_start = get_microtime(); $stringFunctions = array('mb_strtoupper', 'mb_strtolower', 'mb_strlen', 'mb_strwidth'); foreach ($stringFunctions as $key => $function) { - if (!function_exists($function)) unset($stringFunctions[$key]); + if (!function_exists($function)) { + unset($stringFunctions[$key]); + } } - for ($i=0; $i < $count; $i++) { + for ($i = 0; $i < $count; $i++) { foreach ($stringFunctions as $function) { $r = call_user_func_array($function, array($stringTest)); } @@ -255,9 +265,11 @@ function test_05_String_Manipulation($count = 1300000) $time_start = get_microtime(); $stringFunctions = array('addslashes', 'chunk_split', 'metaphone', 'strip_tags', 'soundex', 'wordwrap'); foreach ($stringFunctions as $key => $function) { - if (!function_exists($function)) unset($stringFunctions[$key]); + if (!function_exists($function)) { + unset($stringFunctions[$key]); + } } - for ($i=0; $i < $count; $i++) { + for ($i = 0; $i < $count; $i++) { foreach ($stringFunctions as $function) { $r = call_user_func_array($function, array($stringTest)); } @@ -271,9 +283,11 @@ function test_06_Regex($count = 1300000) $time_start = get_microtime(); $stringFunctions = array('preg_match', 'preg_split'); foreach ($stringFunctions as $key => $function) { - if (!function_exists($function)) unset($stringFunctions[$key]); + if (!function_exists($function)) { + unset($stringFunctions[$key]); + } } - for ($i=0; $i < $count; $i++) { + for ($i = 0; $i < $count; $i++) { foreach ($stringFunctions as $function) { $r = call_user_func_array($function, array($regexPattern, $stringTest)); } @@ -287,9 +301,11 @@ function test_07_1_Hashing($count = 1300000) $time_start = get_microtime(); $stringFunctions = array('crc32', 'md5', 'sha1'); foreach ($stringFunctions as $key => $function) { - if (!function_exists($function)) unset($stringFunctions[$key]); + if (!function_exists($function)) { + unset($stringFunctions[$key]); + } } - for ($i=0; $i < $count; $i++) { + for ($i = 0; $i < $count; $i++) { foreach ($stringFunctions as $function) { $r = call_user_func_array($function, array($stringTest)); } @@ -303,11 +319,13 @@ function test_07_2_Crypt($count = 10000) $time_start = get_microtime(); $stringFunctions = array('crypt'); foreach ($stringFunctions as $key => $function) { - if (!function_exists($function)) unset($stringFunctions[$key]); + if (!function_exists($function)) { + unset($stringFunctions[$key]); + } } - for ($i=0; $i < $count; $i++) { + for ($i = 0; $i < $count; $i++) { foreach ($stringFunctions as $function) { - $r = call_user_func_array($function, array($stringTest)); + $r = call_user_func_array($function, array($stringTest, '_J9..rasm')); } } return format_result_test(get_microtime() - $time_start, $count); @@ -315,9 +333,11 @@ function test_07_2_Crypt($count = 10000) function test_08_Json_Encode($count = 1300000) { - global $stringTest; + global $stringTest, $emptyResult; - if (!function_exists('json_encode')) return $emptyResult; + if (!function_exists('json_encode')) { + return $emptyResult; + } $time_start = get_microtime(); $data = array( @@ -328,7 +348,7 @@ function test_08_Json_Encode($count = 1300000) null, false, ); - for ($i=0; $i < $count; $i++) { + for ($i = 0; $i < $count; $i++) { foreach ($data as $value) { $r = json_encode($value); } @@ -338,9 +358,11 @@ function test_08_Json_Encode($count = 1300000) function test_09_Json_Decode($count = 1300000) { - global $stringTest; + global $stringTest, $emptyResult; - if (!function_exists('json_decode')) return $emptyResult; + if (!function_exists('json_decode')) { + return $emptyResult; + } $time_start = get_microtime(); $data = array( @@ -352,9 +374,9 @@ function test_09_Json_Decode($count = 1300000) false, ); foreach ($data as $key => $value) { - $data[ $key ] = json_encode($value); + $data[$key] = json_encode($value); } - for ($i=0; $i < $count; $i++) { + for ($i = 0; $i < $count; $i++) { foreach ($data as $value) { $r = json_decode($value); } @@ -364,9 +386,11 @@ function test_09_Json_Decode($count = 1300000) function test_10_Serialize($count = 1300000) { - global $stringTest; + global $stringTest, $emptyResult; - if (!function_exists('serialize')) return $emptyResult; + if (!function_exists('serialize')) { + return $emptyResult; + } $time_start = get_microtime(); $data = array( @@ -377,7 +401,7 @@ function test_10_Serialize($count = 1300000) null, false, ); - for ($i=0; $i < $count; $i++) { + for ($i = 0; $i < $count; $i++) { foreach ($data as $value) { $r = serialize($value); } @@ -387,9 +411,11 @@ function test_10_Serialize($count = 1300000) function test_11_Unserialize($count = 1300000) { - global $stringTest; + global $stringTest, $emptyResult; - if (!function_exists('unserialize')) return $emptyResult; + if (!function_exists('unserialize')) { + return $emptyResult; + } $time_start = get_microtime(); $data = array( @@ -401,9 +427,9 @@ function test_11_Unserialize($count = 1300000) false, ); foreach ($data as $key => $value) { - $data[ $key ] = serialize($value); + $data[$key] = serialize($value); } - for ($i=0; $i < $count; $i++) { + for ($i = 0; $i < $count; $i++) { foreach ($data as $value) { $r = unserialize($value); } @@ -414,47 +440,48 @@ function test_11_Unserialize($count = 1300000) function test_12_Array_Fill($count = 300) { $time_start = get_microtime(); - for($n = 0; $n < $count; ++$n) { - for($i = 0; $i < $count; ++$i) { - for($j = 0; $j < $count; ++$j) { - $X[ $i ][ $j ] = $i * $j; + for ($n = 0; $n < $count; ++$n) { + for ($i = 0; $i < $count; ++$i) { + for ($j = 0; $j < $count; ++$j) { + $X[$i][$j] = $i * $j; } } } - return format_result_test(get_microtime() - $time_start, pow($count,3)); + return format_result_test(get_microtime() - $time_start, pow($count, 3)); } function test_13_Array_Unset($count = 300) { $time_start = get_microtime(); - for($n = 0; $n < $count; ++$n) { + for ($n = 0; $n < $count; ++$n) { $X = range(0, $count); - for($i = 0; $i < $count; ++$i) { - $X[ $i ] = range(0, $count); + for ($i = 0; $i < $count; ++$i) { + $X[$i] = range(0, $count); } - for($i = $count-1; $i >= 0; $i--) { - for($j = 0; $j < $count; ++$j) { - unset($X[ $i ][ $j ]); + for ($i = $count - 1; $i >= 0; $i--) { + for ($j = 0; $j < $count; ++$j) { + unset($X[$i][$j]); } - unset($X[ $i ]); + unset($X[$i]); } } - return format_result_test(get_microtime() - $time_start, pow($count,3)); + return format_result_test(get_microtime() - $time_start, pow($count, 3)); } function test_14_Loops($count = 190000000) { $time_start = get_microtime(); - for($i = 0; $i < $count; ++$i); - $i = 0; while($i++ < $count); - return format_result_test(get_microtime() - $time_start, $count*2); + for ($i = 0; $i < $count; ++$i) ; + $i = 0; + while ($i++ < $count) ; + return format_result_test(get_microtime() - $time_start, $count * 2); } function test_15_Loop_IfElse($count = 90000000) { $time_start = get_microtime(); - for ($i=0; $i < $count; $i++) { + for ($i = 0; $i < $count; $i++) { if ($i == -1) { } elseif ($i == -2) { } else if ($i == -3) { @@ -467,13 +494,13 @@ function test_15_Loop_IfElse($count = 90000000) function test_16_Loop_Ternary($count = 90000000) { $time_start = get_microtime(); - for ($i=0; $i < $count; $i++) { + for ($i = 0; $i < $count; $i++) { $r = ($i % 2 == 1) - ? ( ($i % 3 == 1) - ? ( ($i % 5 == 1) + ? (($i % 3 == 1) + ? (($i % 5 == 1) ? 3 - : 2 ) - : 1 ) + : 2) + : 1) : 0; } return format_result_test(get_microtime() - $time_start, $count); @@ -484,7 +511,7 @@ function test_17_1_Loop_Defined_Access($count = 20000000) $time_start = get_microtime(); $a = array(0 => 1, 1 => 0); $r = 0; - for ($i=0; $i < $count; $i++) { + for ($i = 0; $i < $count; $i++) { $r += $a[$i % 2]; } return format_result_test(get_microtime() - $time_start, $count); @@ -495,7 +522,7 @@ function test_17_2_Loop_Undefined_Access($count = 20000000) $time_start = get_microtime(); $a = array(); $r = 0; - for ($i=0; $i < $count; $i++) { + for ($i = 0; $i < $count; $i++) { $r += @$a[$i % 2] ? 0 : 1; } return format_result_test(get_microtime() - $time_start, $count); @@ -506,42 +533,39 @@ function test_17_2_Loop_Undefined_Access($count = 20000000) include_once 'php5.inc'; } - - $total = 0; $functions = get_defined_functions(); sort($functions['user']); echo "
\n$line\n|"
-	.str_pad("PHP BENCHMARK SCRIPT", $padHeader," ",STR_PAD_BOTH)
-	."|\n$line\n"
-	.str_pad("Start:", $padInfo) . " : ". date("Y-m-d H:i:s") . "\n"
-	.str_pad("Server:", $padInfo) . " : ".php_uname('s') . '/' . php_uname('r') . ' ' . php_uname('m') . "\n"
-	.str_pad("CPU:", $padInfo) . " :\n"
-	.str_pad("model", $padInfo, ' ', STR_PAD_LEFT) . " : ". $cpuInfo['model'] . "\n"
-	.str_pad("cores", $padInfo, ' ', STR_PAD_LEFT) . " : ". $cpuInfo['cores'] . "\n"
-	.str_pad("MHz", $padInfo, ' ', STR_PAD_LEFT) . " : ". $cpuInfo['mhz'] . 'MHz' . "\n"
-	.str_pad("PHP version:", $padInfo) . " : " .PHP_VERSION . "\n"
-	.str_pad("Benchmark version:", $padInfo) . " : ".$scriptVersion . "\n"
-	.str_pad("Platform:", $padInfo) . " : " .PHP_OS . "\n"
-	."$line\n"
-	.str_pad('TEST NAME', $padLabel) . " :"
-	.str_pad('SECONDS', 8+4, ' ', STR_PAD_LEFT)." |".str_pad('OP/SEC', 9+4, ' ', STR_PAD_LEFT)." |".str_pad('OP/SEC/MHz', 9+7, ' ', STR_PAD_LEFT)."\n"
-	."$line\n"
-	;
+	. str_pad("PHP BENCHMARK SCRIPT", $padHeader, " ", STR_PAD_BOTH)
+	. "|\n$line\n"
+	. str_pad("Start:", $padInfo) . " : " . date("Y-m-d H:i:s") . "\n"
+	. str_pad("Server:", $padInfo) . " : " . php_uname('s') . '/' . php_uname('r') . ' ' . php_uname('m') . "\n"
+	. str_pad("CPU:", $padInfo) . " :\n"
+	. str_pad("model", $padInfo, ' ', STR_PAD_LEFT) . " : " . $cpuInfo['model'] . "\n"
+	. str_pad("cores", $padInfo, ' ', STR_PAD_LEFT) . " : " . $cpuInfo['cores'] . "\n"
+	. str_pad("MHz", $padInfo, ' ', STR_PAD_LEFT) . " : " . $cpuInfo['mhz'] . 'MHz' . "\n"
+	. str_pad("PHP version:", $padInfo) . " : " . PHP_VERSION . "\n"
+	. str_pad("Benchmark version:", $padInfo) . " : " . $scriptVersion . "\n"
+	. str_pad("Platform:", $padInfo) . " : " . PHP_OS . "\n"
+	. "$line\n"
+	. str_pad('TEST NAME', $padLabel) . " :"
+	. str_pad('SECONDS', 8 + 4, ' ', STR_PAD_LEFT) . " |" . str_pad('OP/SEC', 9 + 4, ' ', STR_PAD_LEFT) . " |" . str_pad('OP/SEC/MHz', 9 + 7, ' ', STR_PAD_LEFT) . "\n"
+	. "$line\n";
 
 foreach ($functions['user'] as $user) {
 	if (preg_match('/^test_/', $user)) {
-		$testName = str_replace('test_','',$user);
+		$testName = str_replace('test_', '', $user);
 		echo str_pad($testName, $padLabel) . " :";
 		list($resultSec, $resultSecFmt, $resultOps, $resultOpMhz) = $user();
 		$total += $resultSec;
-		echo str_pad($resultSecFmt, 8, ' ', STR_PAD_LEFT)." sec |".str_pad($resultOps, 9, ' ', STR_PAD_LEFT)."Op/s |".str_pad($resultOpMhz, 9, ' ', STR_PAD_LEFT)."Ops/MHz"."\n";
+		echo str_pad($resultSecFmt, 8, ' ', STR_PAD_LEFT) . " sec |" . str_pad($resultOps, 9, ' ', STR_PAD_LEFT) . "Op/s |" . str_pad($resultOpMhz, 9, ' ', STR_PAD_LEFT) . "Ops/MHz" . "\n";
 	}
 }
 
 echo $line . "\n"
-. str_pad("Total time:", $padLabel) . " : " . number_format($total, 3) ." sec.\n"
-. str_pad("Current memory usage:", $padLabel) . " : " . convert(memory_get_usage()) .".\n"
-. (function_exists('memory_get_peak_usage') ? str_pad("Peak memory usage:", $padLabel) . " : " . convert(memory_get_peak_usage())  .".\n" : '')
-. "
\n"; + . str_pad("Total time:", $padLabel) . " : " . number_format($total, 3) . " sec.\n" + . str_pad("Current memory usage:", $padLabel) . " : " . convert(memory_get_usage()) . ".\n" + . (function_exists('memory_get_peak_usage') ? str_pad("Peak memory usage:", $padLabel) . " : " . convert(memory_get_peak_usage()) . ".\n" : '') + . "\n"; diff --git a/php5.inc b/php5.inc index f63b9a1..e50c36e 100644 --- a/php5.inc +++ b/php5.inc @@ -1,7 +1,9 @@