\n\n"; echo " "; echo print_benchmark_result($benchmarkResult, $showServerName); echo "\n"; exit; // ----------------------------------------------------------------------------- // Benchmark functions // ----------------------------------------------------------------------------- function test_benchmark($settings) { $result = array(); $result['version'] = '1.2'; $result['sysinfo']['time'] = date('Y-m-d H:i:s'); $result['sysinfo']['php_version'] = PHP_VERSION; $result['sysinfo']['platform'] = PHP_OS; $result['sysinfo']['server_name'] = $_SERVER['SERVER_NAME']; $result['sysinfo']['server_addr'] = $_SERVER['SERVER_ADDR']; $result['sysinfo']['xdebug'] = in_array('xdebug', get_loaded_extensions()); $timeStart = microtime(true); test_math($result); test_string($result); test_loops($result); test_ifelse($result); $result['benchmark']['calculation_total'] = timer_diff($timeStart) . ' sec.'; if (isset($settings['db.host'])) { test_mysql($result, $settings); } $result['benchmark']['total'] = timer_diff($timeStart) . ' sec.'; return $result; } function test_math(&$result, $count = 99999) { $timeStart = microtime(true); $mathFunctions = array("abs", "acos", "asin", "atan", "bindec", "floor", "exp", "sin", "tan", "pi", "is_finite", "is_nan", "sqrt"); for ($i = 0; $i < $count; $i++) { foreach ($mathFunctions as $function) { call_user_func_array($function, array($i)); } } $result['benchmark']['math'] = timer_diff($timeStart) . ' sec.'; } function test_string(&$result, $count = 99999) { $timeStart = microtime(true); $stringFunctions = array("addslashes", "chunk_split", "metaphone", "strip_tags", "md5", "sha1", "strtoupper", "strtolower", "strrev", "strlen", "soundex", "ord"); $string = 'the quick brown fox jumps over the lazy dog'; for ($i = 0; $i < $count; $i++) { foreach ($stringFunctions as $function) { call_user_func_array($function, array($string)); } } $result['benchmark']['string'] = timer_diff($timeStart) . ' sec.'; } function test_loops(&$result, $count = 999999) { $timeStart = microtime(true); for ($i = 0; $i < $count; ++$i) { } $i = 0; while ($i < $count) { ++$i; } $result['benchmark']['loops'] = timer_diff($timeStart) . ' sec.'; } function test_ifelse(&$result, $count = 999999) { $timeStart = microtime(true); for ($i = 0; $i < $count; $i++) { if ($i == -1) { } elseif ($i == -2) { } else { if ($i == -3) { } } } $result['benchmark']['ifelse'] = timer_diff($timeStart) . ' sec.'; } function test_mysql(&$result, $settings) { $timeStart = microtime(true); $link = mysqli_connect($settings['db.host'], $settings['db.user'], $settings['db.pw']); $result['benchmark']['mysql_connect'] = timer_diff($timeStart) . ' sec.'; mysqli_select_db($link, $settings['db.name']); $result['benchmark']['mysql_select_db'] = timer_diff($timeStart) . ' sec.'; $dbResult = mysqli_query($link, 'SELECT VERSION() as version;'); $arr_row = mysqli_fetch_array($dbResult); $result['sysinfo']['mysql_version'] = $arr_row['version']; $result['benchmark']['mysql_query_version'] = timer_diff($timeStart) . ' sec.'; $query = "SELECT BENCHMARK(1000000,ENCODE('hello',RAND()));"; mysqli_query($link, $query); $result['benchmark']['mysql_query_benchmark'] = timer_diff($timeStart) . ' sec.'; mysqli_close($link); $result['benchmark']['mysql_total'] = timer_diff($timeStart) . ' sec.'; return $result; } function timer_diff($timeStart) { return number_format(microtime(true) - $timeStart, 3); } function print_benchmark_result($data, $showServerName = true) { $result = ''; $result .= ''; $result .= ''; $result .= ''; $result .= ''; if (!empty($data['sysinfo']['xdebug'])) { // You are running the benchmark with xdebug enabled. This has a major impact on runtime performance. $result .= ''; } $result .= ''; $result .= ''; if ($showServerName == true) { $result .= ''; $result .= ''; } $result .= ''; $result .= ''; $result .= ''; $result .= ''; $result .= ''; $result .= ''; $result .= ''; $result .= ''; if (isset($data['sysinfo']['mysql_version'])) { $result .= ''; $result .= ''; $result .= ''; $result .= ''; $result .= ''; $result .= ''; $result .= ''; $result .= ''; $result .= ''; } $result .= ''; $result .= '
System Info
Version' . h($data['version']) . '
Time' . h($data['sysinfo']['time']) . '
Xdebug' . h('Warning: Xdebug is enabled!') . '
PHP Version' . h($data['sysinfo']['php_version']) . '
Platform' . h($data['sysinfo']['platform']) . '
Server name' . h($data['sysinfo']['server_name']) . '
Server address' . h($data['sysinfo']['server_addr']) . '
Benchmark
String' . h($data['benchmark']['string']) . '
Loops' . h($data['benchmark']['loops']) . '
If Else' . h($data['benchmark']['ifelse']) . '
Calculation total' . h($data['benchmark']['calculation_total']) . '
MySQL
MySQL Version' . h($data['sysinfo']['mysql_version']) . '
MySQL Connect' . h($data['benchmark']['mysql_connect']) . '
MySQL Select DB' . h($data['benchmark']['mysql_select_db']) . '
MySQL Query Version' . h($data['benchmark']['mysql_query_version']) . '
MySQL Benchmark' . h($data['benchmark']['mysql_query_benchmark']) . '
MySQL Total' . h($data['benchmark']['mysql_total']) . '
Total' . h($data['benchmark']['total']) . '
'; return $result; } function h($v) { return htmlentities($v); }