PluginProbe ʕ •ᴥ•ʔ
10Web Booster – Website speed optimization, Cache & Page Speed optimizer / trunk
10Web Booster – Website speed optimization, Cache & Page Speed optimizer vtrunk
2.33.0 2.30.5 2.30.7 2.30.9 2.31.10 2.31.8 2.32.11 2.32.21 2.32.3 2.32.4 2.32.7 2.6.31 2.6.40 2.6.42 2.6.7 2.7.37 2.7.44 2.7.47 2.8.18 2.8.19 2.8.32 2.8.34 2.8.35 2.9.23 2.9.24 2.9.25 2.9.27 v2.27.4 trunk 2.0.10 2.0.11 2.0.12 2.0.13 2.0.14 2.0.15 2.0.17 2.0.18 2.0.21 2.0.22 2.0.25 2.0.26 2.0.27 2.0.3 2.0.7 2.0.9 2.10.46 2.10.65 2.10.66 2.10.68 2.11.41 2.11.42 2.11.43 2.12.15 2.12.21 2.12.22 2.12.23 2.12.26 2.13.37 2.13.40 2.13.41 2.13.42 2.13.44 2.13.45 2.13.47 2.14.49 2.14.50 2.15.18 2.17.21 2.17.23 2.18.17 2.19.44 2.19.45 2.19.46 2.19.49 2.2.12 2.2.15 2.2.16 2.2.18 2.2.8 2.20.31 2.20.32 2.20.33 2.21.11 2.21.12 2.21.16 2.21.25 2.22.32 2.23.13 2.23.15 2.23.16 2.23.18 2.24.12 2.24.14 2.24.18 2.25.14 2.26.6 2.28.10 2.28.13 2.28.14 2.28.7 2.29.1 2.29.2 2.29.3 2.3.0 2.3.1 2.3.2 2.3.3 2.30.18
tenweb-speed-optimizer / vendor / 10web-utils / 10web-wordpress-benchmark / src / TenWebWpBenchmark / OptimizerBenchmark.php
tenweb-speed-optimizer / vendor / 10web-utils / 10web-wordpress-benchmark / src / TenWebWpBenchmark Last commit date
Init.php 3 years ago OptimizerBenchmark.php 3 years ago Rest.php 3 years ago
OptimizerBenchmark.php
300 lines
1 <?php
2 namespace TenWebWpBenchmark;
3
4 class OptimizerBenchmark
5 {
6 private $configuration;
7 protected static $instance = null;
8 public const TWO_BENCHMARK_DATA_KEY = 'two_benchmark_data';
9
10 public function __construct()
11 {
12 }
13
14 private function initConfiguration()
15 {
16 //do test
17 global $wpdb;
18 $arr_cfg = array();
19
20 // We need special handling for hyperdb
21 if ( is_a( $wpdb, 'hyperdb' ) && ! empty( $wpdb->hyper_servers ) ) {
22 // Grab a `write` server for the `global` dataset and fallback to `read`.
23 // We're not really paying attention to priority or have much in the way of error checking. Use at your own risk :)
24 $db_server = false;
25 if ( ! empty( $wpdb->hyper_servers['global']['write'] ) ) {
26 foreach ( $wpdb->hyper_servers['global']['write'] as $group => $dbs ) {
27 $db_server = current( $dbs );
28 break;
29 }
30 } elseif ( ! empty( $wpdb->hyper_servers['global']['read'] ) ) {
31 foreach ( $wpdb->hyper_servers['global']['read'] as $group => $dbs ) {
32 $db_server = current( $dbs );
33 break;
34 }
35 }
36
37 if ( $db_server ) {
38 $arr_cfg['db.host'] = $db_server['host'];
39 $arr_cfg['db.user'] = $db_server['user'];
40 $arr_cfg['db.pw'] = $db_server['password'];
41 $arr_cfg['db.name'] = $db_server['name'];
42 }
43 } else {
44 // Vanilla WordPress install with standard `wpdb`
45 $arr_cfg['db.host'] = DB_HOST;
46 $arr_cfg['db.user'] = DB_USER;
47 $arr_cfg['db.pw'] = DB_PASSWORD;
48 $arr_cfg['db.name'] = DB_NAME;
49 }
50
51 $this->configuration = $arr_cfg;
52 }
53
54 /**
55 * Runs a benchmark and returns array with data. Also saves data in wp_options under the "two_benchmark_data".
56 * @return array|false
57 */
58 public function test()
59 {
60 $arr_return = [];
61 $this->initConfiguration();
62 try {
63 $arr_return['system'] = $this->test_benchmark();
64 $arr_return['wordpress'] = $this->test_wordpress();
65 $arr_return['check_timestamp'] = current_time('timestamp');
66 } catch (\Exception $exception) {
67 return false;
68 }
69
70 update_option(self::TWO_BENCHMARK_DATA_KEY, $arr_return, false);
71 return $arr_return;
72 }
73
74 /**
75 * Returns benchmark data stored in wp_options under the "two_benchmark_data"
76 * @return mixed
77 */
78 public function getData()
79 {
80 return get_option(self::TWO_BENCHMARK_DATA_KEY);
81 }
82
83 private function test_benchmark()
84 {
85
86 $arr_return = array();
87 $arr_return['version'] = '1.6';
88 $arr_return['sysinfo']['time'] = date("Y-m-d H:i:s"); // phpcs:ignore
89 $arr_return['sysinfo']['php_version'] = PHP_VERSION;
90 $arr_return['sysinfo']['platform'] = PHP_OS;
91 $arr_return['sysinfo']['server_name'] = isset( $_SERVER['SERVER_NAME'] ) ? sanitize_text_field($_SERVER['SERVER_NAME']) : '';
92 $arr_return['sysinfo']['server_addr'] = isset( $_SERVER['SERVER_ADDR'] ) ? sanitize_text_field($_SERVER['SERVER_ADDR']) : '';
93
94 $time_start = microtime(true);
95
96 $this->test_math($arr_return);
97
98 $this->test_string($arr_return);
99
100 $this->test_loops($arr_return);
101
102 $this->test_ifelse($arr_return);
103
104 if (isset($this->configuration['db.host'])) {
105 $this->test_mysql($arr_return, $this->configuration);
106 }
107
108 $arr_return['total'] = self::timer_diff($time_start);
109
110 return $arr_return;
111 }
112
113 private function test_math(&$arr_return, $count = 99999)
114 {
115 $time_start = microtime(true);
116
117 for ($i = 0; $i < $count; $i++) {
118 sin($i);
119 asin($i);
120 cos($i);
121 acos($i);
122 tan($i);
123 atan($i);
124 abs($i);
125 floor($i);
126 exp($i);
127 is_finite($i);
128 is_nan($i);
129 sqrt($i);
130 log10($i);
131 }
132
133 $arr_return['benchmark']['math'] = self::timer_diff($time_start);
134 }
135
136 private function test_string(&$arr_return, $count = 99999)
137 {
138 $time_start = microtime(true);
139 $string = 'the quick brown fox jumps over the lazy dog';
140 for ($i = 0; $i < $count; $i++) {
141 addslashes($string);
142 chunk_split($string);
143 metaphone($string);
144 strip_tags($string); // phpcs:ignore
145 md5($string);
146 sha1($string);
147 strtoupper($string);
148 strtolower($string);
149 strrev($string);
150 strlen($string);
151 soundex($string);
152 ord($string);
153 }
154 $arr_return['benchmark']['string'] = self::timer_diff($time_start);
155 }
156
157 private function test_loops(&$arr_return, $count = 999999)
158 {
159 $time_start = microtime(true);
160 for ($i = 0; $i < $count; ++$i) {
161
162 }
163 $i = 0;
164 while ($i < $count) {
165 ++$i;
166 }
167
168 $arr_return['benchmark']['loops'] = self::timer_diff($time_start);
169 }
170
171 private function test_ifelse(&$arr_return, $count = 999999)
172 {
173 $time_start = microtime(true);
174 for ($i = 0; $i < $count; $i++) {
175 if ($i == -1) {
176
177 } elseif ($i == -2) {
178
179 } else if ($i == -3) {
180
181 }
182 }
183 $arr_return['benchmark']['ifelse'] = self::timer_diff($time_start);
184 }
185
186 private function test_mysql(&$arr_return)
187 {
188
189 $time_start = microtime(true);
190
191
192 //detect socket connection
193 if (stripos($this->configuration['db.host'], '.sock') !== false) {
194 //parse socket location
195 //set a default guess
196 $socket = "/var/lib/mysql.sock";
197 $serverhost = explode(':', $this->configuration['db.host']);
198 if (count($serverhost) == 2 && $serverhost[0] == 'localhost') {
199 $socket = $serverhost[1];
200 }
201 $link = mysqli_connect('localhost', $this->configuration['db.user'], $this->configuration['db.pw'], $this->configuration['db.name'], null, $socket);
202 } else {
203 //parse out port number if exists
204 $port = 3306;//default
205 if (stripos($this->configuration['db.host'], ':')) {
206 $port = substr($this->configuration['db.host'], stripos($this->configuration['db.host'], ':') + 1);
207 $this->configuration['db.host'] = substr($this->configuration['db.host'], 0, stripos($this->configuration['db.host'], ':'));
208 }
209 $link = mysqli_connect($this->configuration['db.host'], $this->configuration['db.user'], $this->configuration['db.pw'], $this->configuration['db.name'], $port);
210 }
211 $arr_return['benchmark']['mysql_connect'] = self::timer_diff($time_start);
212
213 // phpcs:ignore Squiz.PHP.CommentedOutCode.Found
214 //$arr_return['sysinfo']['mysql_version'] = '';
215 //$arr_return['benchmark']['mysql_select_db'] = self::timer_diff($time_start);
216
217 $result = mysqli_query($link, 'SELECT VERSION() as version;');
218 $arr_row = mysqli_fetch_assoc($result);
219 $arr_return['sysinfo']['mysql_version'] = $arr_row['version'];
220 $arr_return['benchmark']['mysql_query_version'] = self::timer_diff($time_start);
221
222 $query = "SELECT BENCHMARK(5000000, AES_ENCRYPT(CONCAT('WPHostingBenchmarks.com',RAND()), UNHEX(SHA2('is part of Review Signal.com',512))))";
223 $result = mysqli_query($link, $query);
224 $arr_return['benchmark']['mysql_query_benchmark'] = self::timer_diff($time_start);
225
226 mysqli_close($link);
227
228 $arr_return['benchmark']['mysql_total'] = self::timer_diff($time_start);
229
230 return $arr_return;
231 }
232
233 private function test_wordpress()
234 {
235 //create dummy text to insert into database
236 $dummytextseed = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque sollicitudin iaculis libero id pellentesque. Donec sodales nunc id lorem rutrum molestie. Duis ac ornare diam. In hac habitasse platea dictumst. Donec nec mi ipsum. Aenean dictum imperdiet erat, at lacinia mi ultrices ut. Phasellus quis nibh ornare, pulvinar dui sit amet, venenatis arcu. Suspendisse eget vehicula ligula, et placerat sapien. Cras enim erat, scelerisque sit amet tellus vel, tempor venenatis risus. In ultricies tristique ante, eu lobortis leo. Cras ullamcorper eleifend libero, quis sollicitudin massa venenatis a. Vestibulum sed pellentesque urna, nec consectetur nulla. Vestibulum sodales purus metus, non scelerisque.";
237 $dummytext = "";
238 for ($x = 0; $x < 100; $x++) {
239 $dummytext .= str_shuffle($dummytextseed);
240 }
241
242 //start timing wordpress mysql functions
243 $time_start = microtime(true);
244 global $wpdb;
245 $table = $wpdb->prefix . 'options';
246 $optionname = 'wpperformancetesterbenchmark_';
247 $count = 250;
248 for ($x = 0; $x < $count; $x++) {
249 //insert
250 $data = array('option_name' => $optionname . $x, 'option_value' => $dummytext);
251 $wpdb->insert($table, $data); // phpcs:ignore
252 //select
253 $select = $wpdb->prepare( "SELECT option_value FROM $table WHERE option_name='%s'", $data['option_name'] ); // phpcs:ignore
254 $wpdb->get_var($select); // phpcs:ignore
255 //update
256 $data = array('option_value' => $dummytextseed);
257 $where = array('option_name' => $optionname . $x);
258 $wpdb->update($table, $data, $where); // phpcs:ignore
259 //delete
260 $where = array('option_name' => $optionname . $x);
261 $wpdb->delete($table, $where); // phpcs:ignore
262 }
263
264 $time = self::timer_diff($time_start);
265 $queries = ($count * 4) / $time;
266 return array('time' => $time, 'queries' => $queries);
267 }
268
269
270 private static function timer_diff($time_start)
271 {
272 return number_format(microtime(true) - $time_start, 3);
273 }
274
275 public static function array_to_html($my_array)
276 {
277 $strReturn = '';
278 if (is_array($my_array)) {
279 $strReturn .= '<table>';
280 foreach ($my_array as $k => $v) {
281 $strReturn .= "\n<tr><td style=\"vertical-align:top;\">";
282 $strReturn .= '<strong>' . htmlentities($k) . "</strong></td><td>";
283 $strReturn .= self::array_to_html($v);
284 $strReturn .= "</td></tr>";
285 }
286 $strReturn .= "\n</table>";
287 } else {
288 $strReturn = htmlentities($my_array);
289 }
290 return $strReturn;
291 }
292
293 public static function get_instance(){
294 if(null === self::$instance) {
295 self::$instance = new self;
296 }
297
298 return self::$instance;
299 }
300 }