Skip to content

Instantly share code, notes, and snippets.

@neenjaw
Created April 17, 2021 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neenjaw/e5390a187c2d742cb458ae6a3a5d9960 to your computer and use it in GitHub Desktop.
Save neenjaw/e5390a187c2d742cb458ae6a3a5d9960 to your computer and use it in GitHub Desktop.
bool benchmark
<?php
// derivitive of https://github.com/vanilla-php/benchmark-php/blob/master/benchmark.php
/**
* PHP Script to benchmark PHP and MySQL-Server.
*
* inspired by / thanks to:
* - www.php-benchmark-script.com (Alessandro Torrisi)
* - www.webdesign-informatik.de
*
* @license MIT
*/
// -----------------------------------------------------------------------------
// Setup
// -----------------------------------------------------------------------------
set_time_limit(120); // 2 minutes
$options = [];
// Show or hide the server name and IP address
$showServerName = false;
// Optional: mysql performance test
//$options['db.host'] = '';
//$options['db.user'] = '';
//$options['db.pw'] = '';
//$options['db.name'] = '';
// -----------------------------------------------------------------------------
// Main
// -----------------------------------------------------------------------------
// check performance
$benchmarkResult = test_benchmark($options);
// html output
echo "<!DOCTYPE html>\n<html><head>\n";
echo "<style>
table a:link {
color: #666;
font-weight: bold;
text-decoration:none;
}
table a:visited {
color: #999999;
font-weight:bold;
text-decoration:none;
}
table a:active,
table a:hover {
color: #bd5a35;
text-decoration:underline;
}
table {
font-family:Arial, Helvetica, sans-serif;
color:#666;
font-size:12px;
text-shadow: 1px 1px 0px #fff;
background:#eaebec;
margin:20px;
border:#ccc 1px solid;
-moz-border-radius:3px;
-webkit-border-radius:3px;
border-radius:3px;
-moz-box-shadow: 0 1px 2px #d1d1d1;
-webkit-box-shadow: 0 1px 2px #d1d1d1;
box-shadow: 0 1px 2px #d1d1d1;
}
table th {
padding:8px 15px 8px 8px;
border-top:1px solid #fafafa;
border-bottom:1px solid #e0e0e0;
text-align: left;
background: #ededed;
background: -webkit-gradient(linear, left top, left bottom, from(#ededed), to(#ebebeb));
background: -moz-linear-gradient(top, #ededed, #ebebeb);
}
table th:first-child {
text-align: left;
padding-left:10px;
}
table tr:first-child th:first-child {
-moz-border-radius-topleft:3px;
-webkit-border-top-left-radius:3px;
border-top-left-radius:3px;
}
table tr:first-child th:last-child {
-moz-border-radius-topright:3px;
-webkit-border-top-right-radius:3px;
border-top-right-radius:3px;
}
table tr {
padding-left:10px;
}
table td:first-child {
text-align: left;
padding-left:10px;
border-left: 0;
}
table td {
padding:8px;
border-top: 1px solid #ffffff;
border-bottom:1px solid #e0e0e0;
border-left: 1px solid #e0e0e0;
background: #fafafa;
background: -webkit-gradient(linear, left top, left bottom, from(#fbfbfb), to(#fafafa));
background: -moz-linear-gradient(top, #fbfbfb, #fafafa);
}
table tr.even td {
background: #f6f6f6;
background: -webkit-gradient(linear, left top, left bottom, from(#f8f8f8), to(#f6f6f6));
background: -moz-linear-gradient(top, #f8f8f8, #f6f6f6);
}
table tr:last-child td {
border-bottom:0;
}
table tr:last-child td:first-child {
-moz-border-radius-bottomleft:3px;
-webkit-border-bottom-left-radius:3px;
border-bottom-left-radius:3px;
}
table tr:last-child td:last-child {
-moz-border-radius-bottomright:3px;
-webkit-border-bottom-right-radius:3px;
border-bottom-right-radius:3px;
}
table tr:hover td {
background: #f2f2f2;
background: -webkit-gradient(linear, left top, left bottom, from(#f2f2f2), to(#f0f0f0));
background: -moz-linear-gradient(top, #f2f2f2, #f0f0f0);
}
</style>
</head>
<body>";
echo print_benchmark_result($benchmarkResult, $showServerName);
echo "\n</body></html>";
exit;
// -----------------------------------------------------------------------------
// Benchmark functions
// -----------------------------------------------------------------------------
function test_benchmark(array $settings)
{
$result = [];
$result['version'] = '1.6';
$result['sysinfo']['time'] = date('Y-m-d H:i:s');
$result['sysinfo']['php_version'] = PHP_VERSION;
$result['sysinfo']['platform'] = PHP_OS;
$timeStart = microtime(true);
$count = 1000000;
test_casting_true($result, $count);
test_casting_false($result, $count);
test_boolval_true($result, $count);
test_boolval_false($result, $count);
$result['benchmark']['total'] = timer_diff($timeStart) . ' sec.';
return $result;
}
function test_casting_true(&$result, $count = 99999)
{
$timeStart = microtime(true);
for ($i = 0; $i < $count; $i++) {
(bool) 1;
(bool) [1];
(bool) "a";
(bool) true;
(bool) 1.0;
}
$result['benchmark']['casting_true'] = timer_diff($timeStart) . ' sec.';
}
function test_casting_false(&$result, $count = 99999)
{
$timeStart = microtime(true);
for ($i = 0; $i < $count; $i++) {
(bool) 0;
(bool) [];
(bool) "";
(bool) false;
(bool) 0.0;
}
$result['benchmark']['casting_false'] = timer_diff($timeStart) . ' sec.';
}
function test_boolval_true(&$result, $count = 99999)
{
$timeStart = microtime(true);
for ($i = 0; $i < $count; $i++) {
boolval(1);
boolval([1]);
boolval("a");
boolval(true);
boolval(1.0);
}
$result['benchmark']['boolval_true'] = timer_diff($timeStart) . ' sec.';
}
function test_boolval_false(&$result, $count = 99999)
{
$timeStart = microtime(true);
for ($i = 0; $i < $count; $i++) {
boolval(0);
boolval([]);
boolval("");
boolval(false);
boolval(0.0);
}
$result['benchmark']['boolval_false'] = timer_diff($timeStart) . ' sec.';
}
function timer_diff($timeStart)
{
return number_format(microtime(true) - $timeStart, 3);
}
function print_benchmark_result(array $data, bool $showServerName = true)
{
$result = '<table cellspacing="0">';
$result .= '<thead><tr><th>System Info</th><th></th></tr></thead>';
$result .= '<tbody>';
$result .= '<tr class="even"><td>Version</td><td>' . h($data['version']) . '</td></tr>';
$result .= '<tr class="even"><td>Time</td><td>' . h($data['sysinfo']['time']) . '</td></tr>';
$result .= '<tr class="even"><td>PHP Version</td><td>' . h($data['sysinfo']['php_version']) . '</td></tr>';
$result .= '<tr class="even"><td>Platform</td><td>' . h($data['sysinfo']['platform']) . '</td></tr>';
$result .= '</tbody>';
$result .= '<thead><tr><th>Benchmark</th><th></th></tr></thead>';
$result .= '<tbody>';
$result .= '<tr><td>casting true</td><td>' . h($data['benchmark']['casting_true']) . '</td></tr>';
$result .= '<tr><td>casting false</td><td>' . h($data['benchmark']['casting_false']) . '</td></tr>';
$result .= '<tr><td>boolval true</td><td>' . h($data['benchmark']['boolval_true']) . '</td></tr>';
$result .= '<tr><td>boolval false</td><td>' . h($data['benchmark']['boolval_false']) . '</td></tr>';
$result .= '</tbody>';
$result .= '<thead><tr><th>Total</th><th>' . h($data['benchmark']['total']) . '</th></tr></thead>';
$result .= '</table>';
return $result;
}
function h($v)
{
return htmlentities($v);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment