1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Hyperized\Benchmark\Modules; |
4
|
|
|
|
5
|
|
|
use Hyperized\Benchmark\Config\Config; |
6
|
|
|
use Hyperized\Benchmark\Generic\Utility; |
7
|
|
|
use Hyperized\Benchmark\Generic\Visual; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class MySQL |
11
|
|
|
* @package Hyperized\Benchmark\Modules |
12
|
|
|
*/ |
13
|
|
|
class MySQL |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var Config |
17
|
|
|
*/ |
18
|
|
|
private $config; |
19
|
|
|
/** |
20
|
|
|
* @var |
21
|
|
|
*/ |
22
|
|
|
private $connection; |
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
private $versionQuery = 'SELECT VERSION() as version;'; |
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
private $benchmarkQuery = 'SELECT BENCHMARK({},ENCODE(\'{}\',RAND()));'; |
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
private $benchmarkText = 'hello'; |
35
|
|
|
/** |
36
|
|
|
* @var |
37
|
|
|
*/ |
38
|
|
|
private $version; |
39
|
|
|
/** |
40
|
|
|
* @var |
41
|
|
|
*/ |
42
|
|
|
private $queryResults; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* MySQL constructor. |
46
|
|
|
* @param Config $config |
47
|
|
|
*/ |
48
|
|
|
public function __construct(Config $config) |
49
|
|
|
{ |
50
|
|
|
$this->config = $config; |
51
|
|
|
|
52
|
|
|
if ($config->get('benchmark.mysql.enabled')) { |
53
|
|
|
$this->configure(); |
54
|
|
|
$this->run(); |
55
|
|
|
$this->render(); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Configure |
61
|
|
|
*/ |
62
|
|
|
private function configure() |
63
|
|
|
{ |
64
|
|
|
$this->connection = \mysqli_connect( |
65
|
|
|
$this->config->get('benchmark.mysql.hostname'), |
66
|
|
|
$this->config->get('benchmark.mysql.username'), |
67
|
|
|
$this->config->get('benchmark.mysql.password'), |
68
|
|
|
$this->config->get('benchmark.mysql.database') |
69
|
|
|
); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Run |
74
|
|
|
*/ |
75
|
|
|
private function run() |
76
|
|
|
{ |
77
|
|
|
$this->getVersion(); |
78
|
|
|
$this->encodeRand(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Render |
83
|
|
|
*/ |
84
|
|
|
private function render() |
85
|
|
|
{ |
86
|
|
|
Visual::print('== MySQL performance information', "\n"); |
87
|
|
|
Visual::print('MySQL version: ' . $this->version, "\n"); |
88
|
|
|
Visual::print('Query (Encode + random) operation results in milliseconds (less is better), for a total of ' . $this->config->get('benchmark.mysql.count') . ' cycles: ' . $this->queryResults); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Obtain MySQL version |
93
|
|
|
*/ |
94
|
|
|
private function getVersion() |
95
|
|
|
{ |
96
|
|
|
$this->version = \mysqli_fetch_object(\mysqli_query($this->connection, $this->versionQuery))->version; |
|
|
|
|
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Run encode with Random query |
101
|
|
|
*/ |
102
|
|
|
private function encodeRand() { |
103
|
|
|
$query = Utility::format($this->benchmarkQuery, [ |
104
|
|
|
$this->config->get('benchmark.mysql.count'), |
105
|
|
|
$this->benchmarkText |
106
|
|
|
]); |
107
|
|
|
|
108
|
|
|
$start = \microtime(true); |
109
|
|
|
|
110
|
|
|
\mysqli_query($this->connection, $query); |
111
|
|
|
|
112
|
|
|
$this->queryResults = (\microtime(true) - $start); |
113
|
|
|
|
114
|
|
|
} |
115
|
|
|
} |