dotibrb048760 2016-08-11 08:29
浏览 29
已采纳

测量课堂上的功能之间的时间?

I am trying to understand OOP and made my own class to benchmark a MySQL DB

class Benchmark
{

    protected $sql, $db;
    public $result, $time;

    public function __construct($host, $user, $pass, $db, $result = array(), $time = null)
    {

        /* Vars */
        $this->db = $db;
        $this->result = $result;
        $this->time = $time;

        /*Start Timer */
        $this->time = microtime(true);

        /* Connect to DB */
        $this->sql = new \mysqli($host, $user, $pass);

        /* Measure Time and put result to array */
        $this->result['benchmark']['connect'] = $this->elapsedTime($this->time);


    }

    public function testMySQL()
    {

        /* Connect to DB */
        $this->sql->select_db($this->db);
        $this->result['benchmark']['selectDb'] = $this->elapsedTime($this->time);

        /* Fetch Version */
        $version = $this->sql->server_version;
        $this->result['benchmark']['version'] = $this->elapsedTime($this->time);
        $this->result['info']['version'] = $version;

        /* Benchmark */
        $this->sql->query('SELECT BENCHMARK(1000000,ENCODE("hello",RAND()));');
        $this->result['benchmark']['result'] = $this->elapsedTime($this->time);

        /* Close Connection */
        $this->sql->close();

        /* Total Time */
        $this->result['info']['total'] = $this->elapsedTime($this->time);

        $this->dump($this->result);

    }
}

However, the result is this:

Array
(
[benchmark] => Array
    (
        [connect] => 0.001
        [selectDb] => 0.001
        [version] => 0.001
        [result] => 10.181
    )

[info] => Array
    (
        [version] => 50713
        [total] => 10.181
    )

)

Why doesnt the time add up? Shouldn't [total] be 10.184 in this case?

The original idea is from here :https://github.com/odan/benchmark-php and that works. If I use the same data on that script, it adds up, where am I wrong? I guess it has something to do with my lack of OOP though...

EDIT

 public function elapsedTime($time)
 {
     return number_format(microtime(true) - $time, 3);
 }
  • 写回答

1条回答 默认 最新

  • drip5880 2016-08-11 08:42
    关注

    If you are measuring the elapsed time since the timer is started, then each output is simply a point in that time period. The time it takes from initializing the timer until the result is obtained is not in addition to the time it takes to connect, select the db, and get the version, those are merely checkpoints along the way (which appear to be occurring virtually simultaneously, at least at the resolution of this timer). So, the total time is 10.181, .001 of which it took to perform the first three operations, not .003.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图