MySQL::configure()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
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 $benchmarkQuery = 'SELECT BENCHMARK({},ENCODE(\'{}\',RAND()));';
27
    /**
28
     * @var string
29
     */
30
    private $benchmarkText = 'hello';
31
    /**
32
     * @var
33
     */
34
    private $version;
35
    /**
36
     * @var
37
     */
38
    private $queryResults;
39
40
    /**
41
     * MySQL constructor.
42
     *
43
     * @param Config $config
44
     */
45
    public function __construct(Config $config)
46
    {
47
        $this->config = $config;
48
49
        if ($config->get('benchmark.mysql.enabled')) {
50
            $this->configure();
51
            $this->run();
52
            $this->render();
53
        }
54
    }
55
56
    /**
57
     * Configure
58
     */
59
    private function configure(): void
60
    {
61
        $this->connection = \mysqli_connect(
62
            $this->config->get('benchmark.mysql.hostname'),
63
            $this->config->get('benchmark.mysql.username'),
64
            $this->config->get('benchmark.mysql.password'),
65
            $this->config->get('benchmark.mysql.database')
66
        );
67
    }
68
69
    /**
70
     * Run
71
     */
72
    private function run(): void
73
    {
74
        $this->getVersion();
75
        $this->encodeRand();
76
    }
77
78
    /**
79
     * Obtain MySQL version
80
     */
81
    private function getVersion(): void
82
    {
83
        $this->version = \mysqli_get_server_version($this->connection);
84
    }
85
86
    /**
87
     * Run encode with Random query
88
     */
89
    private function encodeRand(): void
90
    {
91
        $query = Utility::format($this->benchmarkQuery, [
92
            $this->config->get('benchmark.mysql.count'),
93
            $this->benchmarkText
94
        ]);
95
96
        $start = \microtime(true);
97
98
        \mysqli_query($this->connection, $query);
99
100
        $this->queryResults = (\microtime(true) - $start);
101
102
    }
103
104
    /**
105
     * Render
106
     */
107
    private function render(): void
108
    {
109
        Visual::print('== MySQL performance information', "\n");
110
        Visual::print('MySQL version: ' . $this->version, "\n");
111
        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);
112
    }
113
}