|
1
|
|
|
<?php /** @noinspection PhpStatementHasEmptyBodyInspection */ |
|
2
|
|
|
|
|
3
|
|
|
namespace Hyperized\Benchmark\Modules; |
|
4
|
|
|
|
|
5
|
|
|
use Hyperized\Benchmark\Config\Config; |
|
6
|
|
|
use Hyperized\Benchmark\Generic\Table; |
|
7
|
|
|
use Hyperized\Benchmark\Generic\Visual; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class CPU |
|
11
|
|
|
* @package Hyperized\Benchmark\Modules |
|
12
|
|
|
* |
|
13
|
|
|
* Totally borrowed from: https://github.com/odan/benchmark-php/blob/master/benchmark.php |
|
14
|
|
|
*/ |
|
15
|
|
|
class CPU |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var int |
|
19
|
|
|
*/ |
|
20
|
|
|
private static $defaultCount = 99999; |
|
21
|
|
|
/** |
|
22
|
|
|
* @var array |
|
23
|
|
|
*/ |
|
24
|
|
|
private static $mathFunctions = [ |
|
25
|
|
|
'abs', |
|
26
|
|
|
'acos', |
|
27
|
|
|
'asin', |
|
28
|
|
|
'atan', |
|
29
|
|
|
'bindec', |
|
30
|
|
|
'floor', |
|
31
|
|
|
'exp', |
|
32
|
|
|
'sin', |
|
33
|
|
|
'tan', |
|
34
|
|
|
'pi', |
|
35
|
|
|
'is_finite', |
|
36
|
|
|
'is_nan', |
|
37
|
|
|
'sqrt' |
|
38
|
|
|
]; |
|
39
|
|
|
/** |
|
40
|
|
|
* @var array |
|
41
|
|
|
*/ |
|
42
|
|
|
private static $stringFunctions = [ |
|
43
|
|
|
'addslashes', |
|
44
|
|
|
'chunk_split', |
|
45
|
|
|
'metaphone', |
|
46
|
|
|
'strip_tags', |
|
47
|
|
|
'md5', |
|
48
|
|
|
'sha1', |
|
49
|
|
|
'strtoupper', |
|
50
|
|
|
'strtolower', |
|
51
|
|
|
'strrev', |
|
52
|
|
|
'strlen', |
|
53
|
|
|
'soundex', |
|
54
|
|
|
'ord' |
|
55
|
|
|
]; |
|
56
|
|
|
/** |
|
57
|
|
|
* @var string |
|
58
|
|
|
*/ |
|
59
|
|
|
private static $string = 'the quick brown fox jumps over the lazy dog'; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @var \Hyperized\Benchmark\Config\Config |
|
63
|
|
|
*/ |
|
64
|
|
|
private $config; |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @var array |
|
68
|
|
|
*/ |
|
69
|
|
|
private $mathResults = []; |
|
70
|
|
|
/** |
|
71
|
|
|
* @var array |
|
72
|
|
|
*/ |
|
73
|
|
|
private $stringsResults = []; |
|
74
|
|
|
/** |
|
75
|
|
|
* @var integer |
|
76
|
|
|
*/ |
|
77
|
|
|
private $loopsResults; |
|
78
|
|
|
/** |
|
79
|
|
|
* @var integer |
|
80
|
|
|
*/ |
|
81
|
|
|
private $ifElseResults; |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @var |
|
85
|
|
|
*/ |
|
86
|
|
|
private $mathCount; |
|
87
|
|
|
/** |
|
88
|
|
|
* @var |
|
89
|
|
|
*/ |
|
90
|
|
|
private $stringsCount; |
|
91
|
|
|
/** |
|
92
|
|
|
* @var |
|
93
|
|
|
*/ |
|
94
|
|
|
private $loopsCount; |
|
95
|
|
|
/** |
|
96
|
|
|
* @var |
|
97
|
|
|
*/ |
|
98
|
|
|
private $ifElseCount; |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* CPU constructor. |
|
102
|
|
|
* |
|
103
|
|
|
* @param \Hyperized\Benchmark\Config\Config $config |
|
104
|
|
|
*/ |
|
105
|
|
|
public function __construct(Config $config) |
|
106
|
|
|
{ |
|
107
|
|
|
$this->config = $config; |
|
108
|
|
|
$this->configure(); |
|
109
|
|
|
|
|
110
|
|
|
if ($config->get('benchmark.cpu.enabled')) { |
|
111
|
|
|
$this->run(); |
|
112
|
|
|
$this->render(); |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Configure |
|
118
|
|
|
*/ |
|
119
|
|
|
private function configure(): void |
|
120
|
|
|
{ |
|
121
|
|
|
$this->mathCount = $this->config->get('benchmark.cpu.math.count') ?? self::$defaultCount; |
|
122
|
|
|
$this->stringsCount = $this->config->get('benchmark.cpu.strings.count') ?? self::$defaultCount; |
|
123
|
|
|
$this->loopsCount = $this->config->get('benchmark.cpu.loops.count') ?? self::$defaultCount; |
|
124
|
|
|
$this->ifElseCount = $this->config->get('benchmark.cpu.ifElse.count') ?? self::$defaultCount; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Run! |
|
129
|
|
|
*/ |
|
130
|
|
|
private function run(): void |
|
131
|
|
|
{ |
|
132
|
|
|
$this->math(); |
|
133
|
|
|
$this->strings(); |
|
134
|
|
|
$this->loops(); |
|
135
|
|
|
$this->ifElse(); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Do Maths! |
|
140
|
|
|
*/ |
|
141
|
|
|
private function math(): void |
|
142
|
|
|
{ |
|
143
|
|
|
foreach (self::$mathFunctions as $function) { |
|
144
|
|
|
$this->mathResults['x'][$function] = 0; |
|
145
|
|
|
$start = \microtime(true); |
|
146
|
|
|
for ($i = 0; $i < $this->mathCount; $i++) { |
|
147
|
|
|
\call_user_func_array($function, array($i)); |
|
148
|
|
|
} |
|
149
|
|
|
$this->mathResults['x'][$function] += (\microtime(true) - $start); |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Do string operations |
|
155
|
|
|
*/ |
|
156
|
|
|
private function strings(): void |
|
157
|
|
|
{ |
|
158
|
|
|
foreach (self::$stringFunctions as $function) { |
|
159
|
|
|
$this->stringsResults['x'][$function] = 0; |
|
160
|
|
|
$start = \microtime(true); |
|
161
|
|
|
for ($i = 0; $i < $this->stringsCount; $i++) { |
|
162
|
|
|
\call_user_func_array($function, array(self::$string)); |
|
163
|
|
|
} |
|
164
|
|
|
$this->stringsResults['x'][$function] += (\microtime(true) - $start); |
|
165
|
|
|
} |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* Loopy loop |
|
170
|
|
|
*/ |
|
171
|
|
|
private function loops(): void |
|
172
|
|
|
{ |
|
173
|
|
|
$start = \microtime(true); |
|
174
|
|
|
|
|
175
|
|
|
for ($i = 0; $i < $this->loopsCount; ++$i) { |
|
176
|
|
|
; |
|
177
|
|
|
} |
|
178
|
|
|
$i = 0; |
|
179
|
|
|
while ($i < $this->loopsCount) { |
|
180
|
|
|
++$i; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
$this->loopsResults = (\microtime(true) - $start); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* ifElseIf really .. |
|
188
|
|
|
*/ |
|
189
|
|
|
private function ifElse(): void |
|
190
|
|
|
{ |
|
191
|
|
|
$start = \microtime(true); |
|
192
|
|
|
|
|
193
|
|
|
for ($i = 0; $i < $this->ifElseCount; $i++) { |
|
194
|
|
|
if ($i === -1) { |
|
195
|
|
|
; |
|
196
|
|
|
} elseif ($i === -2) { |
|
197
|
|
|
; |
|
198
|
|
|
} else if ($i === -3) { |
|
199
|
|
|
; |
|
200
|
|
|
} |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
$this->ifElseResults = (\microtime(true) - $start); |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* Render |
|
208
|
|
|
*/ |
|
209
|
|
|
private function render(): void |
|
210
|
|
|
{ |
|
211
|
|
|
Visual::print('== CPU performance information', "\n"); |
|
212
|
|
|
Visual::print('Math operation results by function in milliseconds (less is better), for a total of ' . $this->mathCount . ' cycles:'); |
|
213
|
|
|
new Table($this->mathResults); |
|
214
|
|
|
Visual::print(' ', "\n"); |
|
215
|
|
|
Visual::print('String operation results by function in milliseconds (less is better), for a total of ' . $this->stringsCount . ' cycles:'); |
|
216
|
|
|
new Table($this->stringsResults); |
|
217
|
|
|
Visual::print(' ', "\n"); |
|
218
|
|
|
Visual::print('Loop operation results in milliseconds (less is better), for a total of ' . $this->loopsCount . ' cycles: ' . $this->loopsResults); |
|
219
|
|
|
Visual::print('If/Else operation results in milliseconds (less is better), for a total of ' . $this->ifElseCount . ' cycles: ' . $this->ifElseResults); |
|
220
|
|
|
Visual::print(' ', "\n"); |
|
221
|
|
|
} |
|
222
|
|
|
} |