bench.php
· 9.4 KiB · PHP
Ham
<?php
/*
##########################################################################
# PHP Benchmark Performance Script #
# © 2010 Code24 BV #
# #
# Author : Alessandro Torrisi #
# Author : Sergey Dryabzhinsky #
# Company : Code24 BV, The Netherlands #
# Date : July 2, 2015 #
# version : 1.0.6 #
# License : Creative Commons CC-BY license #
# Website : http://www.php-benchmark-script.com #
# #
##########################################################################
*/
$scriptVersion = '1.0.6';
$stringTest = " the quick <b>brown</b> fox jumps <i>over</i> the lazy dog and eat <span>lorem ipsum</span> Valar morghulis \n\rабыр\nвалар дохаÑÑ€Ð¸Ñ ";
$regexPattern = "/[\s,]+/";
// Need alot of memory - more 1Gb
$doTestArrays = true;
set_time_limit(0);
ini_set('memory_limit', '2048M');
$line = str_pad("-",78,"-");
$padHeader = 76;
$padInfo = 20;
$padLabel = 62;
function get_microtime()
{
$time = microtime(true);
if (is_string($time)) {
list($f, $i) = explode(" ", $time);
$time = intval($i) + floatval($f);
}
return $time;
}
function convert($size)
{
$unit=array('b','kb','mb','gb','tb','pb');
return @round($size/pow(1024,($i=floor(log($size,1024)))),2).' '.$unit[$i];
}
function test_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]);
}
for ($i=0; $i < $count; $i++) {
foreach ($mathFunctions as $function) {
$r = call_user_func_array($function, array($i));
}
}
return number_format(get_microtime() - $time_start, 3);
}
function test_String_Concat($count = 14000000) {
$time_start = get_microtime();
$s = '';
for($i = 0; $i < $count; ++$i) {
$s .= "- Valar moghulis\n";
}
return number_format(get_microtime() - $time_start, 3);
}
function test_String_Simple_Functions($count = 1300000) {
global $stringTest;
$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]);
}
for ($i=0; $i < $count; $i++) {
foreach ($stringFunctions as $function) {
$r = call_user_func_array($function, array($stringTest));
}
}
return number_format(get_microtime() - $time_start, 3);
}
function test_String_Multibyte($count = 130000) {
global $stringTest;
if (!function_exists('mb_strlen')) return '-.---';
$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]);
}
for ($i=0; $i < $count; $i++) {
foreach ($stringFunctions as $function) {
$r = call_user_func_array($function, array($stringTest));
}
}
return number_format(get_microtime() - $time_start, 3);
}
function test_String_Manipulation($count = 1300000) {
global $stringTest;
$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]);
}
for ($i=0; $i < $count; $i++) {
foreach ($stringFunctions as $function) {
$r = call_user_func_array($function, array($stringTest));
}
}
return number_format(get_microtime() - $time_start, 3);
}
function test_Regex($count = 1300000) {
global $stringTest, $regexPattern;
$time_start = get_microtime();
$stringFunctions = array("preg_match", "preg_split");
foreach ($stringFunctions as $key => $function) {
if (!function_exists($function)) unset($stringFunctions[$key]);
}
for ($i=0; $i < $count; $i++) {
foreach ($stringFunctions as $function) {
$r = call_user_func_array($function, array($regexPattern, $stringTest));
}
}
return number_format(get_microtime() - $time_start, 3);
}
function test_Hashing($count = 1300000) {
global $stringTest;
$time_start = get_microtime();
$stringFunctions = array("crc32", "md5", "sha1",);
foreach ($stringFunctions as $key => $function) {
if (!function_exists($function)) unset($stringFunctions[$key]);
}
for ($i=0; $i < $count; $i++) {
foreach ($stringFunctions as $function) {
$r = call_user_func_array($function, array($stringTest));
}
}
return number_format(get_microtime() - $time_start, 3);
}
function test_Json_Encode($count = 1300000) {
global $stringTest;
if (!function_exists('json_encode')) return '-.---';
$time_start = get_microtime();
$data = array(
$stringTest,
123456,
123.456,
array(123456),
null,
false,
);
for ($i=0; $i < $count; $i++) {
foreach ($data as $value) {
$r = json_encode($value);
}
}
return number_format(get_microtime() - $time_start, 3);
}
function test_Json_Decode($count = 1300000) {
global $stringTest;
if (!function_exists('json_decode')) return '-.---';
$time_start = get_microtime();
$data = array(
$stringTest,
123456,
123.456,
array(123456),
null,
false,
);
foreach ($data as $key => $value) {
$data[ $key ] = json_encode($value);
}
for ($i=0; $i < $count; $i++) {
foreach ($data as $value) {
$r = json_decode($value);
}
}
return number_format(get_microtime() - $time_start, 3);
}
function test_Serialize($count = 1300000) {
global $stringTest;
if (!function_exists('serialize')) return '-.---';
$time_start = get_microtime();
$data = array(
$stringTest,
123456,
123.456,
array(123456),
null,
false,
);
for ($i=0; $i < $count; $i++) {
foreach ($data as $value) {
$r = serialize($value);
}
}
return number_format(get_microtime() - $time_start, 3);
}
function test_Unserialize($count = 1300000) {
global $stringTest;
if (!function_exists('unserialize')) return '-.---';
$time_start = get_microtime();
$data = array(
$stringTest,
123456,
123.456,
array(123456),
null,
false,
);
foreach ($data as $key => $value) {
$data[ $key ] = serialize($value);
}
for ($i=0; $i < $count; $i++) {
foreach ($data as $value) {
$r = unserialize($value);
}
}
return number_format(get_microtime() - $time_start, 3);
}
function test_Array_Fill($count = 3000) {
global $doTestArrays;
if (!$doTestArrays) return '-.---';
$time_start = get_microtime();
for($i = 0; $i < $count; ++$i) {
for($j = 0; $j < $count; ++$j) {
$X[ $i ][ $j ] = $i * $j;
}
}
return number_format(get_microtime() - $time_start, 3);
}
function test_Array_Unset($count = 3000) {
global $doTestArrays;
if (!$doTestArrays) return '-.---';
$time_start = get_microtime();
$X = 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 ]);
}
unset($X[ $i ]);
}
return number_format(get_microtime() - $time_start, 3);
}
function test_Loops($count = 190000000) {
$time_start = get_microtime();
for($i = 0; $i < $count; ++$i);
$i = 0; while($i++ < $count);
return number_format(get_microtime() - $time_start, 3);
}
function test_IfElse($count = 90000000) {
$time_start = get_microtime();
for ($i=0; $i < $count; $i++) {
if ($i == -1) {
} elseif ($i == -2) {
} else if ($i == -3) {
} else {
}
}
return number_format(get_microtime() - $time_start, 3);
}
function test_Ternary($count = 90000000) {
$time_start = get_microtime();
for ($i=0; $i < $count; $i++) {
$r = ($i % 2 == 1)
? ( ($i % 3 == 1)
? ( ($i % 5 == 1)
? 3
: 2 )
: 1 )
: 0;
}
return number_format(get_microtime() - $time_start, 3);
}
$total = 0;
$functions = get_defined_functions();
echo "<pre>\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() . "\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";
foreach ($functions['user'] as $user) {
if (preg_match('/^test_/', $user)) {
$result = $user();
$total += $result;
echo str_pad($user, $padLabel) . " : " . $result ." sec.\n";
}
}
echo $line . "\n"
. str_pad("Total time:", $padLabel) . " : " . $total ." 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" : '')
. "</pre>\n";
?>
| 1 | <?php |
| 2 | /* |
| 3 | ########################################################################## |
| 4 | # PHP Benchmark Performance Script # |
| 5 | # © 2010 Code24 BV # |
| 6 | # # |
| 7 | # Author : Alessandro Torrisi # |
| 8 | # Author : Sergey Dryabzhinsky # |
| 9 | # Company : Code24 BV, The Netherlands # |
| 10 | # Date : July 2, 2015 # |
| 11 | # version : 1.0.6 # |
| 12 | # License : Creative Commons CC-BY license # |
| 13 | # Website : http://www.php-benchmark-script.com # |
| 14 | # # |
| 15 | ########################################################################## |
| 16 | */ |
| 17 | |
| 18 | $scriptVersion = '1.0.6'; |
| 19 | |
| 20 | $stringTest = " the quick <b>brown</b> fox jumps <i>over</i> the lazy dog and eat <span>lorem ipsum</span> Valar morghulis \n\rабыр\nвалар дохаÑÑ€Ð¸Ñ "; |
| 21 | $regexPattern = "/[\s,]+/"; |
| 22 | |
| 23 | // Need alot of memory - more 1Gb |
| 24 | $doTestArrays = true; |
| 25 | |
| 26 | set_time_limit(0); |
| 27 | ini_set('memory_limit', '2048M'); |
| 28 | |
| 29 | $line = str_pad("-",78,"-"); |
| 30 | $padHeader = 76; |
| 31 | $padInfo = 20; |
| 32 | $padLabel = 62; |
| 33 | |
| 34 | function get_microtime() |
| 35 | { |
| 36 | $time = microtime(true); |
| 37 | if (is_string($time)) { |
| 38 | list($f, $i) = explode(" ", $time); |
| 39 | $time = intval($i) + floatval($f); |
| 40 | } |
| 41 | return $time; |
| 42 | } |
| 43 | |
| 44 | function convert($size) |
| 45 | { |
| 46 | $unit=array('b','kb','mb','gb','tb','pb'); |
| 47 | return @round($size/pow(1024,($i=floor(log($size,1024)))),2).' '.$unit[$i]; |
| 48 | } |
| 49 | |
| 50 | function test_Math($count = 1400000) { |
| 51 | $time_start = get_microtime(); |
| 52 | $mathFunctions = array("abs", "acos", "asin", "atan", "decbin", "dechex", "decoct", "floor", "exp", "log1p", "sin", "tan", "pi", "is_finite", "is_nan", "sqrt", "rad2deg"); |
| 53 | foreach ($mathFunctions as $key => $function) { |
| 54 | if (!function_exists($function)) unset($mathFunctions[$key]); |
| 55 | } |
| 56 | for ($i=0; $i < $count; $i++) { |
| 57 | foreach ($mathFunctions as $function) { |
| 58 | $r = call_user_func_array($function, array($i)); |
| 59 | } |
| 60 | } |
| 61 | return number_format(get_microtime() - $time_start, 3); |
| 62 | } |
| 63 | |
| 64 | function test_String_Concat($count = 14000000) { |
| 65 | $time_start = get_microtime(); |
| 66 | $s = ''; |
| 67 | for($i = 0; $i < $count; ++$i) { |
| 68 | $s .= "- Valar moghulis\n"; |
| 69 | } |
| 70 | return number_format(get_microtime() - $time_start, 3); |
| 71 | } |
| 72 | |
| 73 | function test_String_Simple_Functions($count = 1300000) { |
| 74 | global $stringTest; |
| 75 | $time_start = get_microtime(); |
| 76 | $stringFunctions = array("strtoupper", "strtolower", "strrev", "strlen", "str_rot13", "ord", "trim"); |
| 77 | foreach ($stringFunctions as $key => $function) { |
| 78 | if (!function_exists($function)) unset($stringFunctions[$key]); |
| 79 | } |
| 80 | for ($i=0; $i < $count; $i++) { |
| 81 | foreach ($stringFunctions as $function) { |
| 82 | $r = call_user_func_array($function, array($stringTest)); |
| 83 | } |
| 84 | } |
| 85 | return number_format(get_microtime() - $time_start, 3); |
| 86 | } |
| 87 | |
| 88 | function test_String_Multibyte($count = 130000) { |
| 89 | global $stringTest; |
| 90 | |
| 91 | if (!function_exists('mb_strlen')) return '-.---'; |
| 92 | |
| 93 | $time_start = get_microtime(); |
| 94 | $stringFunctions = array("mb_strtoupper", "mb_strtolower", "mb_strlen", "mb_strwidth"); |
| 95 | foreach ($stringFunctions as $key => $function) { |
| 96 | if (!function_exists($function)) unset($stringFunctions[$key]); |
| 97 | } |
| 98 | for ($i=0; $i < $count; $i++) { |
| 99 | foreach ($stringFunctions as $function) { |
| 100 | $r = call_user_func_array($function, array($stringTest)); |
| 101 | } |
| 102 | } |
| 103 | return number_format(get_microtime() - $time_start, 3); |
| 104 | } |
| 105 | |
| 106 | function test_String_Manipulation($count = 1300000) { |
| 107 | global $stringTest; |
| 108 | $time_start = get_microtime(); |
| 109 | $stringFunctions = array("addslashes", "chunk_split", "metaphone", "strip_tags", "soundex", "wordwrap"); |
| 110 | foreach ($stringFunctions as $key => $function) { |
| 111 | if (!function_exists($function)) unset($stringFunctions[$key]); |
| 112 | } |
| 113 | for ($i=0; $i < $count; $i++) { |
| 114 | foreach ($stringFunctions as $function) { |
| 115 | $r = call_user_func_array($function, array($stringTest)); |
| 116 | } |
| 117 | } |
| 118 | return number_format(get_microtime() - $time_start, 3); |
| 119 | } |
| 120 | |
| 121 | function test_Regex($count = 1300000) { |
| 122 | global $stringTest, $regexPattern; |
| 123 | $time_start = get_microtime(); |
| 124 | $stringFunctions = array("preg_match", "preg_split"); |
| 125 | foreach ($stringFunctions as $key => $function) { |
| 126 | if (!function_exists($function)) unset($stringFunctions[$key]); |
| 127 | } |
| 128 | for ($i=0; $i < $count; $i++) { |
| 129 | foreach ($stringFunctions as $function) { |
| 130 | $r = call_user_func_array($function, array($regexPattern, $stringTest)); |
| 131 | } |
| 132 | } |
| 133 | return number_format(get_microtime() - $time_start, 3); |
| 134 | } |
| 135 | |
| 136 | function test_Hashing($count = 1300000) { |
| 137 | global $stringTest; |
| 138 | $time_start = get_microtime(); |
| 139 | $stringFunctions = array("crc32", "md5", "sha1",); |
| 140 | foreach ($stringFunctions as $key => $function) { |
| 141 | if (!function_exists($function)) unset($stringFunctions[$key]); |
| 142 | } |
| 143 | for ($i=0; $i < $count; $i++) { |
| 144 | foreach ($stringFunctions as $function) { |
| 145 | $r = call_user_func_array($function, array($stringTest)); |
| 146 | } |
| 147 | } |
| 148 | return number_format(get_microtime() - $time_start, 3); |
| 149 | } |
| 150 | |
| 151 | function test_Json_Encode($count = 1300000) { |
| 152 | global $stringTest; |
| 153 | |
| 154 | if (!function_exists('json_encode')) return '-.---'; |
| 155 | |
| 156 | $time_start = get_microtime(); |
| 157 | $data = array( |
| 158 | $stringTest, |
| 159 | 123456, |
| 160 | 123.456, |
| 161 | array(123456), |
| 162 | null, |
| 163 | false, |
| 164 | ); |
| 165 | for ($i=0; $i < $count; $i++) { |
| 166 | foreach ($data as $value) { |
| 167 | $r = json_encode($value); |
| 168 | } |
| 169 | } |
| 170 | return number_format(get_microtime() - $time_start, 3); |
| 171 | } |
| 172 | |
| 173 | function test_Json_Decode($count = 1300000) { |
| 174 | global $stringTest; |
| 175 | |
| 176 | if (!function_exists('json_decode')) return '-.---'; |
| 177 | |
| 178 | $time_start = get_microtime(); |
| 179 | $data = array( |
| 180 | $stringTest, |
| 181 | 123456, |
| 182 | 123.456, |
| 183 | array(123456), |
| 184 | null, |
| 185 | false, |
| 186 | ); |
| 187 | foreach ($data as $key => $value) { |
| 188 | $data[ $key ] = json_encode($value); |
| 189 | } |
| 190 | for ($i=0; $i < $count; $i++) { |
| 191 | foreach ($data as $value) { |
| 192 | $r = json_decode($value); |
| 193 | } |
| 194 | } |
| 195 | return number_format(get_microtime() - $time_start, 3); |
| 196 | } |
| 197 | |
| 198 | function test_Serialize($count = 1300000) { |
| 199 | global $stringTest; |
| 200 | |
| 201 | if (!function_exists('serialize')) return '-.---'; |
| 202 | |
| 203 | $time_start = get_microtime(); |
| 204 | $data = array( |
| 205 | $stringTest, |
| 206 | 123456, |
| 207 | 123.456, |
| 208 | array(123456), |
| 209 | null, |
| 210 | false, |
| 211 | ); |
| 212 | for ($i=0; $i < $count; $i++) { |
| 213 | foreach ($data as $value) { |
| 214 | $r = serialize($value); |
| 215 | } |
| 216 | } |
| 217 | return number_format(get_microtime() - $time_start, 3); |
| 218 | } |
| 219 | |
| 220 | function test_Unserialize($count = 1300000) { |
| 221 | global $stringTest; |
| 222 | |
| 223 | if (!function_exists('unserialize')) return '-.---'; |
| 224 | |
| 225 | $time_start = get_microtime(); |
| 226 | $data = array( |
| 227 | $stringTest, |
| 228 | 123456, |
| 229 | 123.456, |
| 230 | array(123456), |
| 231 | null, |
| 232 | false, |
| 233 | ); |
| 234 | foreach ($data as $key => $value) { |
| 235 | $data[ $key ] = serialize($value); |
| 236 | } |
| 237 | for ($i=0; $i < $count; $i++) { |
| 238 | foreach ($data as $value) { |
| 239 | $r = unserialize($value); |
| 240 | } |
| 241 | } |
| 242 | return number_format(get_microtime() - $time_start, 3); |
| 243 | } |
| 244 | |
| 245 | function test_Array_Fill($count = 3000) { |
| 246 | global $doTestArrays; |
| 247 | if (!$doTestArrays) return '-.---'; |
| 248 | |
| 249 | $time_start = get_microtime(); |
| 250 | for($i = 0; $i < $count; ++$i) { |
| 251 | for($j = 0; $j < $count; ++$j) { |
| 252 | $X[ $i ][ $j ] = $i * $j; |
| 253 | } |
| 254 | } |
| 255 | return number_format(get_microtime() - $time_start, 3); |
| 256 | } |
| 257 | |
| 258 | function test_Array_Unset($count = 3000) { |
| 259 | global $doTestArrays; |
| 260 | if (!$doTestArrays) return '-.---'; |
| 261 | |
| 262 | $time_start = get_microtime(); |
| 263 | $X = range(0, $count); |
| 264 | for($i = 0; $i < $count; ++$i) { |
| 265 | $X[ $i ] = range(0, $count); |
| 266 | } |
| 267 | for($i = $count-1; $i >= 0; $i--) { |
| 268 | for($j = 0; $j < $count; ++$j) { |
| 269 | unset($X[ $i ][ $j ]); |
| 270 | } |
| 271 | unset($X[ $i ]); |
| 272 | } |
| 273 | return number_format(get_microtime() - $time_start, 3); |
| 274 | } |
| 275 | |
| 276 | function test_Loops($count = 190000000) { |
| 277 | $time_start = get_microtime(); |
| 278 | for($i = 0; $i < $count; ++$i); |
| 279 | $i = 0; while($i++ < $count); |
| 280 | return number_format(get_microtime() - $time_start, 3); |
| 281 | } |
| 282 | |
| 283 | function test_IfElse($count = 90000000) { |
| 284 | $time_start = get_microtime(); |
| 285 | for ($i=0; $i < $count; $i++) { |
| 286 | if ($i == -1) { |
| 287 | } elseif ($i == -2) { |
| 288 | } else if ($i == -3) { |
| 289 | } else { |
| 290 | } |
| 291 | } |
| 292 | return number_format(get_microtime() - $time_start, 3); |
| 293 | } |
| 294 | |
| 295 | function test_Ternary($count = 90000000) { |
| 296 | $time_start = get_microtime(); |
| 297 | for ($i=0; $i < $count; $i++) { |
| 298 | $r = ($i % 2 == 1) |
| 299 | ? ( ($i % 3 == 1) |
| 300 | ? ( ($i % 5 == 1) |
| 301 | ? 3 |
| 302 | : 2 ) |
| 303 | : 1 ) |
| 304 | : 0; |
| 305 | } |
| 306 | return number_format(get_microtime() - $time_start, 3); |
| 307 | } |
| 308 | |
| 309 | $total = 0; |
| 310 | $functions = get_defined_functions(); |
| 311 | echo "<pre>\n$line\n|" |
| 312 | .str_pad("PHP BENCHMARK SCRIPT", $padHeader," ",STR_PAD_BOTH) |
| 313 | ."|\n$line\n" |
| 314 | .str_pad("Start:", $padInfo) . " : ". date("Y-m-d H:i:s") . "\n" |
| 315 | .str_pad("Server:", $padInfo) . " : ".php_uname() . "\n" |
| 316 | .str_pad("PHP version:", $padInfo) . " : " .PHP_VERSION . "\n" |
| 317 | .str_pad("Benchmark version:", $padInfo) . " : ".$scriptVersion . "\n" |
| 318 | .str_pad("Platform:", $padInfo) . " : " .PHP_OS . "\n" |
| 319 | ."$line\n"; |
| 320 | foreach ($functions['user'] as $user) { |
| 321 | if (preg_match('/^test_/', $user)) { |
| 322 | $result = $user(); |
| 323 | $total += $result; |
| 324 | echo str_pad($user, $padLabel) . " : " . $result ." sec.\n"; |
| 325 | } |
| 326 | } |
| 327 | echo $line . "\n" |
| 328 | . str_pad("Total time:", $padLabel) . " : " . $total ." sec.\n" |
| 329 | . str_pad("Current memory usage:", $padLabel) . " : " . convert(memory_get_usage()) .".\n" |
| 330 | . (function_exists('memory_get_peak_usage') ? str_pad("Peak memory usage:", $padLabel) . " : " . convert(memory_get_peak_usage()) .".\n" : '') |
| 331 | . "</pre>\n"; |
| 332 | ?> |