0

i am trying to test database speed with php script.

  1. database connect time
  2. database query time/execution time
  3. database basic information like version, port etc

I am using : https://stackoverflow.com/a/39559173

I am trying :

<?php
/**
 * Created by PhpStorm.
 * User: NEO
 * Date: 9/18/2016
 * Time: 10:57 AM
 */

/**
 * PHP Script to benchmark PHP and MySQL-Server
 *
 * inspired by / thanks to:
 * - www.php-benchmark-script.com  (Alessandro Torrisi)
 * - www.webdesign-informatik.de
 *
 * @author odan
 * @license MIT
 */
// -----------------------------------------------------------------------------
// Setup
// -----------------------------------------------------------------------------
set_time_limit(120); // 2 minutes
$options = array();
// Optional: mysql performance test
$options['db.host'] = '127.0.0.1';
$options['db.user'] = 'root';
$options['db.pw'] = '';
$options['db.name'] = 'bache3';
// -----------------------------------------------------------------------------
// Main
// -----------------------------------------------------------------------------
// check performance
$benchmarkResult = test_benchmark($options);
// html output
echo "<!DOCTYPE html>\n<html><head>\n";
echo "<style>
    table {
        color: #333; /* Lighten up font color */
        font-family: Helvetica, Arial, sans-serif; /* Nicer font */
        width: 640px;
        border-collapse:
        collapse; border-spacing: 0;
    }
    td, th {
        border: 1px solid #CCC; height: 30px;
    } /* Make cells a bit taller */
    th {
        background: #F3F3F3; /* Light grey background */
        font-weight: bold; /* Make sure they're bold */
    }
    td {
        background: #FAFAFA; /* Lighter grey background */
    }
    </style>
    </head>
    <body>";
echo array_to_html($benchmarkResult);
echo "\n</body></html>";
exit;
// -----------------------------------------------------------------------------
// Benchmark functions
// -----------------------------------------------------------------------------
function test_benchmark($settings)
{
    $timeStart = microtime(true);
    $result = array();
    $result['version'] = '1.1';
    $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'];
    test_math($result);
    test_string($result);
    test_loops($result);
    test_ifelse($result);
    if (isset($settings['db.host'])) {
        test_mysql($result, $settings);
    }
    $result['total'] = timer_diff($timeStart);
    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);
}
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);
}
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);
}
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);
}
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);
    //$arr_return['sysinfo']['mysql_version'] = '';
    mysqli_select_db($link, $settings['db.name']);
    $result['benchmark']['mysql']['select_db'] = timer_diff($timeStart);
    $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);
    $query = "SELECT BENCHMARK(1000000,ENCODE('hello',RAND()));";
    $dbResult = mysqli_query($link, $query);
    $result['benchmark']['mysql']['query_benchmark'] = timer_diff($timeStart);
    mysqli_close($link);
    $result['benchmark']['mysql']['total'] = timer_diff($timeStart);
    return $result;
}
function timer_diff($timeStart)
{
    return number_format(microtime(true) - $timeStart, 3);
}
function array_to_html($array)
{
    $result = '';
    if (is_array($array)) {
        $result .= '<table>';
        foreach ($array as $k => $v) {
            $result .= "\n<tr><td>";
            $result .= '<strong>' . htmlentities($k) . "</strong></td><td>";
            $result .= array_to_html($v);
            $result .= "</td></tr>";
        }
        $result .= "\n</table>";
    } else {
        $result = htmlentities($array);
    }
    return $result;
}

The above script is working fine but it does not have a option to add custom port for testing and it do not show information like execution time etc

MT0
  • 143,790
  • 11
  • 59
  • 117
  • Did you try anything to implement those features? For the port, you should be able to just add that to the host with a colon. – Chris Haas Jan 29 '22 at 14:21
  • Also, the code you are referencing was added without any additional details, has zero internal comments, and received only one vote in 5+ years. I’m not (necessarily) questioning the code, but it wouldn’t be my first choice. – Chris Haas Jan 29 '22 at 14:27
  • @ChrisHaas can you provide me different code for checking – user18059799 Jan 29 '22 at 14:45
  • Sorry, no. I’ve never had a need for such a thing. I would generally recommend to get performance metrics on your application itself. If you have a slow query, that is almost definitely a database problem, not a PHP one, and you’d want to either tune your query or your tables. – Chris Haas Jan 29 '22 at 15:00
  • @ChrisHaas it dam slow. website taking around 30 seconds to load :(. – user18059799 Jan 29 '22 at 15:04
  • There’s a million reasons that it could be slow, and the code you posted wouldn’t really help you find any of them. Start with a static HTML page, no additional assets. Is it slow? If so, your problem is with your server. Next, try boring PHP page. Is that slow? Problem is with PHP. Next, try boring PHP with boring SQL, nothing complicated. If slow, problem is MySQL. My guess is that all of these will be fast. Next, bring in _one_ of your queries? If slow, fix it. If not, bring in another and repeat. You could also use something like Blackfire to perf your requests. – Chris Haas Jan 29 '22 at 15:09
  • More than likely you have a single slow query, and that can be solved often by an index. Dump all of your queries out and manually run them through a SQL tool by hand, look for the slow ones. – Chris Haas Jan 29 '22 at 15:10
  • @ChrisHaas it a fresh install. You can check here : https://stackoverflow.com/questions/70906824/web-page-loading-time-is-very-slow-after-connecting-to-db – user18059799 Jan 29 '22 at 15:16
  • Your database is half way around the globe? – Chris Haas Jan 29 '22 at 15:23
  • @ChrisHaas is this is only the reason behind slow connection?. approx 30 sec? – user18059799 Jan 29 '22 at 15:24
  • 1
    I’m going to partially retract my previous bits. It is very rare for the connection from the application to the database to be a problem, but it might be in your case. Make sure you are connecting to an IP, not a host name first, just to test DNS resolution. And then I’d time an [individual script](https://stackoverflow.com/a/9288945/231316) that connects to the database. – Chris Haas Jan 29 '22 at 15:27
  • What is `ENCODE()`? And why test it? – Rick James Jan 29 '22 at 19:46
  • Have the timings helped you pin down which statement is causing "taking around 30 seconds to load"? – Rick James Jan 29 '22 at 19:48
  • Halfway around the globe is only 200-300ms. So, 30 seconds might imply 100 queries?? – Rick James Jan 29 '22 at 19:49

0 Answers0