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);
}