-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmark.php
233 lines (188 loc) · 6.99 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
<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
/**
this file is largely Copyright (c) 2014-2016 odan, and licensed under MIT license
*/
// -----------------------------------------------------------------------------
// Setup
// -----------------------------------------------------------------------------
set_time_limit(120); // 2 minutes
$options = array();
global $result;
$result = array();
global $wphc_timeStart ;
$wphc_timeStart = microtime(true);
// -----------------------------------------------------------------------------
// Main
// -----------------------------------------------------------------------------
// check performance
$benchmarkResult = test_benchmark($options);
//graph
global $grand;
echo "<p>score: " . round($grand) . " (lower is better)</p><br/>";
?>
<canvas id="cvs_bench" width="250" height="250">
[No canvas support]
</canvas>
<script>
new RGraph.Gauge({
id: 'cvs_bench',
min: 0,
max: 120,
value: <?php echo round($grand) ;?>,
options :{
scaleDecimals : 0,
labelsCount: 6,
greenColor: '#afa',
borderInner: '#1FA7E1',
borderOuter: '#1FA7E1',
greenEnd: 40,
redStart: 80,
}
}).draw()
</script>
<?php
// html output
echo "<br />Artifical benchmark<br /><br />";
echo array_to_html($benchmarkResult);
// -----------------------------------------------------------------------------
// Benchmark functions
// -----------------------------------------------------------------------------
function test_benchmark($settings)
{
$timeStart = microtime(true);
global $result, $grand;
// $result['version'] = '1.1';
// $result['sysinfo']['time'] = date("Y-m-d H:i:s");
// $result['sysinfo']['php_version'] = PHP_VERSION;
// $result['sysinfo']['platform'] = PHP_OS;
// $result['sysinfo']['server_name'] = $_SERVER['SERVER_NAME'];
// $result['sysinfo']['server_addr'] = $_SERVER['SERVER_ADDR'];
$grand=0.01;
test_math($result);
test_string($result);
test_loops($result);
test_ifelse($result);
// if (isset($settings['db.host'])) {
test_mysql($result, $settings);
// }
//$result['total'] = timer_diff($timeStart);
global $wphc_timeStart ;
$wphc_overalltime= timer_diff($wphc_timeStart);
$result['Duration']['start'] = round($wphc_timeStart,1);
$result['Duration']['end'] = round(microtime(true),1);
$result['Duration']['Elapsed'] = round($wphc_overalltime,1);
$result['Duration']['Score'] = round(pow($wphc_overalltime,2),1);
$grand +=pow($wphc_overalltime,2);
$grand=$grand*20/32*2/3;
$result['Overall Score'] = round($grand);
return $result;
}
function test_math(&$result, $count = 99999)
{
$timeStart = microtime(true);
$mathFunctions = array("abs", "acos", "asin", "atan", "bindec", "floor", "exp", "sin", "tan", "pi", "is_finite", "is_nan", "sqrt");
for ($i = 0; $i < $count; $i++) {
foreach ($mathFunctions as $function) {
call_user_func_array($function, array($i));
$a=(4+9*5/4-3+2)/7;
}
}
global $grand;
$grand+=timer_diff($timeStart)*10;
$result['benchmark']['php']['math'] = round(timer_diff($timeStart)*10,1);
}
function test_string(&$result, $count = 99999)
{
$timeStart = microtime(true);
$stringFunctions = array("addslashes", "chunk_split", "metaphone", "strip_tags", "md5", "sha1", "strtoupper", "strtolower", "strrev", "strlen", "soundex", "ord");
$string = 'the quick brown fox jumps over the lazy dog';
for ($i = 0; $i < $count; $i++) {
foreach ($stringFunctions as $function) {
call_user_func_array($function, array($string));
}
}
global $grand;
$grand+=timer_diff($timeStart)*10;
$result['benchmark']['php']['string'] = round(timer_diff($timeStart)*10,1);
}
function test_loops(&$result, $count = 999999)
{
$timeStart = microtime(true);
for ($i = 0; $i < $count; ++$i) {
}
$i = 0;
while ($i < $count) {
++$i;
}
global $grand;
$grand+=timer_diff($timeStart)*100;
$result['benchmark']['php']['loops'] = round(timer_diff($timeStart)*100,1);
}
function test_ifelse(&$result, $count = 999999)
{
$timeStart = microtime(true);
for ($i = 0; $i < $count; $i++) {
if ($i == -1) {
} elseif ($i == -2) {
} else if ($i == -3) {
}
}
global $grand;
$grand+=timer_diff($timeStart)*100;
$result['benchmark']['php']['ifelse'] = round(timer_diff($timeStart)*100,1);
}
function test_mysql(&$result, $settings)
{
global $grand, $wpdb;
$timeStart = microtime(true);
// $link = mysqli_connect($settings['db.host'], $settings['db.user'], $settings['db.pw']);
// $result['benchmark']['mysql']['connect'] = timer_diff($timeStart)*1000;
//$grand+=timer_diff($timeStart)*1000;
//$arr_return['sysinfo']['mysql_version'] = '';
//mysqli_select_db($link, $settings['db.name']);
//$result['benchmark']['mysql']['select_db'] = timer_diff($timeStart)*1000;
//$grand+=timer_diff($timeStart)*1000;
//$mylink =
// $arr_row = mysqli_fetch_array($mylink);
$wpdb->get_var('SELECT VERSION() as version;');
$result['benchmark']['mysql']['query_version'] = timer_diff($timeStart)*1000;
$grand+=timer_diff($timeStart)*1000;
// $query = "SELECT BENCHMARK(100000,ENCODE('hello',RAND()));";
// $query = "BENCHMARK(100000,ENCODE(select sum(ID)) from wp_users)";
//SELECT option_name, option_value FROM wp_options WHERE autoload = 'yes'
//$query = "BENCHMARK(100000,select stddev(option_id),sum(option_id) from wp_options limit 100)";
for ($i = 0; $i < 10; $i++) {
$mylink = $wpdb->get_var( "select BENCHMARK(1000000,(select sum(ID) from wp_users limit 5))");
// $dbResult = mysqli_query($link, $query);
$mylink = $wpdb->get_var("select BENCHMARK(1000000,(SELECT stddev(option_id) FROM wp_options WHERE autoload = 'yes' limit 100))");
// $dbResult = mysqli_query($link, $query);
}
$result['benchmark']['mysql']['query_benchmark'] = round(timer_diff($timeStart)*20,1);
$grand+=timer_diff($timeStart)*20;
//mysqli_close($link);
//$result['benchmark']['mysql']['total'] = timer_diff($timeStart);
return $result;
}
function timer_diff($timeStart)
{
return number_format(microtime(true) - $timeStart, 4);
}
function array_to_html($array)
{
$result = '';
if (is_array($array)) {
$result .= '<table>';
foreach ($array as $k => $v) {
$result .= "\n<tr><td>";
$result .= '<strong>' . htmlentities($k) . "</strong></td><td>";
$result .= array_to_html($v);
$result .= "</td></tr>";
}
$result .= "\n</table>";
} else {
$result = htmlentities($array);
}
return $result;
}
?>