Passed
Push — master ( 782dea...729c40 )
by Gerben
06:59 queued 02:14
created

MySQL::configure()   A

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 9.4285
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 $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;
0 ignored issues
show
Bug introduced by
It seems like mysqli_query($this->conn...n, $this->versionQuery) can also be of type boolean; however, parameter $result of mysqli_fetch_object() does only seem to accept mysqli_result, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

96
        $this->version = \mysqli_fetch_object(/** @scrutinizer ignore-type */ \mysqli_query($this->connection, $this->versionQuery))->version;
Loading history...
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
}