从 php 应用程序服务器到 mysql 数据库服务器的确切网络时间

Exact Network time from php application Server to mysql database server

我有两台服务器 一个是 应用程序服务器,PHP 应用程序 是 运行,另一个是 Mysql 数据库 是 运行。我想测量从应用程序服务器发送请求到数据库服务器上接收到执行任何查询的请求所花费的时间。

注意:请求刚刚发送到数据库服务器时的时间戳,以及在执行任何查询以响应请求之前在数据库服务器上接收到的请求。对在两个 servers.I 之间传输数据所需的时间感兴趣,知道这取决于数据的大小。我只是想为不同大小的数据获取时间。

如果有这种测量的任何工具或任何方法仅针对此特定请求进行测量,请提出建议。(已经知道请求发送时间戳只需要知道如何捕获请求接收时间戳)

任何人都可以建议从一台机器上的 php 服务器到另一台机器上的 mysql 数据库服务器的方法。 并将该时间记录在数据库或文件或任何可能有用的工具上的任何位置。 到目前为止,我正在做的是从 php 服务器上传任何文件并在执行 mysql 查询之前记录时间,并在收到执行查询的请求时尝试在数据库级别获取时间戳,但问题是时间是由 now() 拍摄,我不认为这是 datsbase 第一次被击中的时间。

代码是这样写的

<?php
include('db_config.php');
if (isset($_FILES['data'])) {

    if (!file_exists('files/')) {
        mkdir('files/', 0777, true);
    }
    if (!file_exists('img/')) {
        mkdir('img/', 0777, true);
    }
    $errors = array();
    $file_name = $_FILES['data']['name'];
    $file_size = $_FILES['data']['size'];
    $file_tmp = $_FILES['data']['tmp_name'];
    $file_type = $_FILES['data']['type'];
    if (move_uploaded_file($file_tmp, "img/" . $file_name)) {
        $l = 1;

        $t = microtime(true);
        $micro = sprintf("%03d", ($t - floor($t)) * 1000);
        $d = new DateTime(date('Y-m-d H:i:s.' . $micro, $t));
        $dd = 'Receiving Time : ' . $d->format("Y-m-d H:i:s.u");
        $fileSize = 'File Size : ' . $file_size;
        $res = 'Sending Time : ' . $_REQUEST['hidd'] . ' ' . $dd . ' ' . $fileSize;


        $directory = 'files/';

        if (glob($directory . "*.txt") != false) {
            $filecount = count(glob($directory . "*.txt"));
            $filecount ++;
            $file = "files/record_" . $filecount . ".txt";
            $fd = fopen($file, "a");
            if (!$fd) {
                die("Could not open $file to write in");
            }
            fwrite($fd, $res);
        } else {
            $file = "files/record_" . $l . ".txt";
            $fd = fopen($file, "a");
            if (!$fd) {
                die("Could not open $file to write in");
            }
            fwrite($fd, $res);
        }

        fclose($fd);
    }

    $db = new Database();
    $db->connect();

    echo $send_time = date("Y-m-d H:i:s");
    $db->insert('request_record', array("$file_name", "$file_size", $send_time, "$res"), "file_name,
        file_size,
        request_generated_time,       
        file_uploaded
        ");
    $res = $db->getResult();
    print_r($res);
    echo $end_time = date("Y-m-d H:i:s"); } ?>
<html>
    <body>

        <form action="" method="POST" enctype="multipart/form-data" id="frm1">
            <input type="file" name="data" />
            <input type="hidden" name="hidd" id="hd" />
            <input type="button" value="Submit" onclick="pdfSubmit()"/>
        </form>

    </body>

    <script type="text/javascript" src="./jquery-1.9.1.min.js"></script>
    <script>
                function pdfSubmit() {
                    var currentTime = new Date();
                    var hours = currentTime.getHours();
                    var minutes = currentTime.getMinutes();
                    var seconds = currentTime.getSeconds();
                    var miliseconds = currentTime.getMilliseconds();

                    if (minutes < 10) {
                        minutes = "0" + minutes;
                    }
                    if (seconds < 10) {
                        seconds = "0" + seconds;
                    }
                    var year = pad(currentTime.getFullYear());
                    var month = pad(currentTime.getMonth() + 1);
                    var day = pad(currentTime.getDate());

                    var n = year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds + '.' + miliseconds;
                    $('#hd').val(n);
                    $('#frm1').submit();
                }
                function pad(numb) {
                    return (numb < 10 ? '0' : '') + numb;
                }
    </script>
</html>

PHP 是同步的,所以这很容易。使用一些非常简单的东西,应该 return 马上。

使用 microtime(true) 到 return 时间戳浮点数(之前和之后)。

那要看你用什么 SQL API 了,但是 运行 像 SELECT 1.

然后比较时间戳。

我会在发送查询之前和之后在应用程序服务器上使用 time() 函数。将差值除以二,您应该得到整轮平均时间。