-
Notifications
You must be signed in to change notification settings - Fork 10
/
benchmark.php
242 lines (197 loc) · 7.35 KB
/
benchmark.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
<?php
/**
* PHP Script to benchmark PHP and MySQL-Server
*
* inspired by / thanks to:
* - www.php-benchmark-script.com (Alessandro Torrisi)
* - www.webdesign-informatik.de
*
* @author odan
* @version 2014.02.23
* @license MIT
*
*/
// -----------------------------------------------------------------------------
// Setup
// -----------------------------------------------------------------------------
set_time_limit(120); // 2 minutes
$arr_cfg = array();
// optional: mysql performance test
//$arr_cfg['db.host'] = DB_HOST;
//$arr_cfg['db.user'] = DB_USER;
//$arr_cfg['db.pw'] = DB_PASSWORD;
//$arr_cfg['db.name'] = DB_NAME;
// -----------------------------------------------------------------------------
// Benchmark functions
// -----------------------------------------------------------------------------
function test_benchmark($arr_cfg)
{
$arr_return = array();
$arr_return['version'] = '1.6';
$arr_return['sysinfo']['time'] = date("Y-m-d H:i:s");
$arr_return['sysinfo']['php_version'] = PHP_VERSION;
$arr_return['sysinfo']['platform'] = PHP_OS;
$arr_return['sysinfo']['server_name'] = $_SERVER['SERVER_NAME'];
$arr_return['sysinfo']['server_addr'] = $_SERVER['SERVER_ADDR'];
$time_start = microtime(true);
test_math($arr_return);
test_string($arr_return);
test_loops($arr_return);
test_ifelse($arr_return);
if (isset($arr_cfg['db.host'])) {
test_mysql($arr_return, $arr_cfg);
}
$arr_return['total'] = timer_diff($time_start);
return $arr_return;
}
function test_math(&$arr_return, $count = 99999)
{
$time_start = microtime(true);
for ($i = 0; $i < $count; $i++) {
sin($i);
asin($i);
cos($i);
acos($i);
tan($i);
atan($i);
abs($i);
floor($i);
exp($i);
is_finite($i);
is_nan($i);
sqrt($i);
log10($i);
}
$arr_return['benchmark']['math'] = timer_diff($time_start);
}
function test_string(&$arr_return, $count = 99999)
{
$time_start = microtime(true);
$string = 'the quick brown fox jumps over the lazy dog';
for ($i = 0; $i < $count; $i++) {
addslashes($string);
chunk_split($string);
metaphone($string);
strip_tags($string);
md5($string);
sha1($string);
strtoupper($string);
strtolower($string);
strrev($string);
strlen($string);
soundex($string);
ord($string);
}
$arr_return['benchmark']['string'] = timer_diff($time_start);
}
function test_loops(&$arr_return, $count = 999999)
{
$time_start = microtime(true);
for ($i = 0; $i < $count; ++$i)
;
$i = 0;
while ($i < $count) {
++$i;
}
$arr_return['benchmark']['loops'] = timer_diff($time_start);
}
function test_ifelse(&$arr_return, $count = 999999)
{
$time_start = microtime(true);
for ($i = 0; $i < $count; $i++) {
if ($i == -1) {
} elseif ($i == -2) {
} else if ($i == -3) {
}
}
$arr_return['benchmark']['ifelse'] = timer_diff($time_start);
}
function test_mysql(&$arr_return, $arr_cfg)
{
$time_start = microtime(true);
//detect socket connection
if(stripos($arr_cfg['db.host'], '.sock')!==false){
//parse socket location
//set a default guess
$socket = "/var/lib/mysql.sock";
$serverhost = explode(':', $arr_cfg['db.host']);
if(count($serverhost) == 2 && $serverhost[0] == 'localhost'){
$socket = $serverhost[1];
}
$link = mysqli_connect('localhost', $arr_cfg['db.user'], $arr_cfg['db.pw'], $arr_cfg['db.name'], null, $socket);
}else{
//parse out port number if exists
$port = 3306;//default
if(stripos($arr_cfg['db.host'],':')){
$port = substr($arr_cfg['db.host'], stripos($arr_cfg['db.host'],':')+1);
$arr_cfg['db.host'] = substr($arr_cfg['db.host'], 0, stripos($arr_cfg['db.host'],':'));
}
$link = mysqli_connect($arr_cfg['db.host'], $arr_cfg['db.user'], $arr_cfg['db.pw'], $arr_cfg['db.name'], $port);
}
$arr_return['benchmark']['mysql_connect'] = timer_diff($time_start);
// //$arr_return['sysinfo']['mysql_version'] = '';
//$arr_return['benchmark']['mysql_select_db'] = timer_diff($time_start);
$result = mysqli_query($link, 'SELECT VERSION() as version;');
$arr_row = mysqli_fetch_assoc($result);
$arr_return['sysinfo']['mysql_version'] = $arr_row['version'];
$arr_return['benchmark']['mysql_query_version'] = timer_diff($time_start);
$query = "SELECT BENCHMARK(5000000, AES_ENCRYPT(CONCAT('WPHostingBenchmarks.com',RAND()), UNHEX(SHA2('is part of Review Signal.com',512))))";
$result = mysqli_query($link, $query);
$arr_return['benchmark']['mysql_query_benchmark'] = timer_diff($time_start);
mysqli_close($link);
$arr_return['benchmark']['mysql_total'] = timer_diff($time_start);
return $arr_return;
}
function test_wordpress(){
//create dummy text to insert into database
$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.";
$dummytext = "";
for($x=0; $x<100; $x++){
$dummytext .= str_shuffle($dummytextseed);
}
//start timing wordpress mysql functions
$time_start = microtime(true);
global $wpdb;
$table = $wpdb->prefix . 'options';
$optionname = 'wpperformancetesterbenchmark_';
$count = 250;
for($x=0; $x<$count;$x++){
//insert
$data = array('option_name' => $optionname . $x, 'option_value' => $dummytext);
$wpdb->insert($table, $data);
//select
$select = "SELECT option_value FROM $table WHERE option_name='$optionname" . $x . "'";
$wpdb->get_var($select);
//update
$data = array('option_value' => $dummytextseed);
$where = array('option_name' => $optionname . $x);
$wpdb->update($table, $data, $where);
//delete
$where = array('option_name'=>$optionname.$x);
$wpdb->delete($table,$where);
}
$time = timer_diff($time_start);
$queries = ($count * 4) / $time;
return array('time'=>$time,'queries'=>$queries);
}
function timer_diff($time_start)
{
return number_format(microtime(true) - $time_start, 3);
}
function array_to_html($my_array)
{
$strReturn = '';
if (is_array($my_array)) {
$strReturn .= '<table>';
foreach ($my_array as $k => $v) {
$strReturn .= "\n<tr><td style=\"vertical-align:top;\">";
$strReturn .= '<strong>' . htmlentities($k) . "</strong></td><td>";
$strReturn .= array_to_html($v);
$strReturn .= "</td></tr>";
}
$strReturn .= "\n</table>";
} else {
$strReturn = htmlentities($my_array);
}
return $strReturn;
}