【发布时间】:2016-08-11 08:29:10
【问题描述】:
我正在尝试了解 OOP 并创建了自己的课程来对 MySQL 数据库进行基准测试
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);
}
}
但是,结果是这样的:
Array
(
[benchmark] => Array
(
[connect] => 0.001
[selectDb] => 0.001
[version] => 0.001
[result] => 10.181
)
[info] => Array
(
[version] => 50713
[total] => 10.181
)
)
为什么时间没有加起来?在这种情况下 [total] 不应该是 10.184 吗?
最初的想法来自这里 :https://github.com/odan/benchmark-php 并且有效。如果我在该脚本上使用相同的数据,它会加起来,我错在哪里?我想这与我缺乏 OOP 有关...
编辑
public function elapsedTime($time)
{
return number_format(microtime(true) - $time, 3);
}
【问题讨论】:
-
elapsedTime定义在哪里? -
为清晰起见已更新