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 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的