forked from vanilla-php/benchmark-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark.php
More file actions
407 lines (360 loc) · 13.7 KB
/
benchmark.php
File metadata and controls
407 lines (360 loc) · 13.7 KB
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
<?php
// Start the buffering //
if($_POST['collect']) { ob_start(); }
?>
<!DOCTYPE html><html><head>
<style>
table {
color: #333; /* Lighten up font color */
font-family: Helvetica, Arial, sans-serif; /* Nicer font */
width: 640px;
border-collapse:
collapse; border-spacing: 0;
}
td, th {
border: 1px solid #CCC; height: 30px;
} /* Make cells a bit taller */
th {
background: #F3F3F3; /* Light grey background */
font-weight: bold; /* Make sure they're bold */
}
</style>
<script type="text/javascript">
function showMe (box) {
var chboxs = document.getElementsByName(box);
var vis = "none";
for(var i=0;i<chboxs.length;i++) {
if(chboxs[i].checked){
vis = "block";
break;
}
}
document.getElementById(box).style.display = vis;
}
function CompareTables(table1,table2)
{
var instHasChange = false;
for(var i=0; i < table1.rows.length; i++)
{
var changes =RowExists(table2,table1.rows[i].cells[0].innerHTML,parseFloat(table1.rows[i].cells[1].innerHTML));
if(!changes[0])
{
table1.rows[i].style.backgroundColor = "orange";
instHasChange = true;
}
else if(changes[1])
{
table1.rows[i].style.backgroundColor = "green";
instHasChange = true;
}
}
for(var i=0; i < table2.rows.length; i++)
{
var changes = RowExists(table1,table2.rows[i].cells[0].innerHTML,parseFloat(table2.rows[i].cells[1].innerHTML));
if(!changes[0])
{
table2.rows[i].style.backgroundColor = "green";
instHasChange = true;
}
else if(changes[1])
{
table2.rows[i].style.backgroundColor = "orange";
instHasChange = true;
}
}
return instHasChange;
}
function RowExists(table,columnName,columnValue)
{
var hasColumnOrChange = new Array(2);
hasColumnOrChange[0] = false;
hasColumnOrChange[1] = false;
for(var i=0; i < table.rows.length; i++)
{
if(table.rows[i].cells[0].innerHTML == columnName)
{
hasColumnOrChange[0] = true;
if(table.rows[i].cells[1].innerHTML > columnValue)
hasColumnOrChange[1] = true;
}
}
return hasColumnOrChange;
}
</script>
</head>
<body>
<?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
* @license MIT
*/
if($_POST['collect']) {
// -----------------------------------------------------------------------------
// Setup
// -----------------------------------------------------------------------------
set_time_limit(320); // 6 minutes
$options = array();
if($_POST['mybench']) {
$mysql = True;
// Optional: mysql performance test
$options['db.host'] = $_POST['mysql_host'];
$options['db.user'] = $_POST['mysql_user'];
$options['db.pw'] = $_POST['mysql_password'];
$options['db.name'] = $_POST['mysql_db'];
}
else {
$mysql = False;
echo "MySQL credentials were not provided, so MySQL bench was skipped. <br><br>";
}
// -----------------------------------------------------------------------------
// Main
// -----------------------------------------------------------------------------
// check performance
// Running benchmark
$benchmarkResult = test_benchmark($options);
// Reading from file if comparison is enabled
if($_POST['compare']) { $fileReadResult = file_get_contents($_POST['compare-file']); }
// saving to file, if selected
if($_POST['saveFile']) {
$file_path = explode('/', $_POST['save-file']);
if(count($file_path) > 1) {
mkdir(implode("/", array_slice($file_path, 0, -1)));
}
if (file_exists($_POST['save-file'])) {
$file_array = explode(".", $_POST['save-file']);
array_splice($file_array, -1, 0, date('dmY_his', time()));
$file = implode('.', $file_array);
}
else {
$file = $_POST['save-file'];
}
file_put_contents($file, json_encode($benchmarkResult));
}
echo "You may download file with results via the links: <a href='".$file."'>JSON</a>, <a href='".$file.".html'>HTML</a><br>";
echo "<table style='border: 1px solid black;'><tr><td><center>Current</center></td>";
if($_POST['compare']) { echo "<td><center>Comparison</center></td>"; }
echo "</tr><tr><td>";
echo array_to_html($benchmarkResult, "current");
if($_POST['compare']) { echo "</td><td>", array_to_html(json_decode($fileReadResult, true), "file"); }
echo "</td></tr></table>";
if($_POST['compare']) {
if ($mysql == True) {
echo "<script>
window.onload = CompareTables(document.getElementById('currentmysql'), document.getElementById('filemysql'));
</script>";
}
echo "<script>
window.onload = CompareTables(document.getElementById('currentphp'), document.getElementById('filephp'));
window.onload = CompareTables(document.getElementById('currentdisk'), document.getElementById('filedisk'));
window.onload = function() {
var currenttotal = document.getElementById('currenttotal');
var filetotal = document.getElementById('filetotal');
var currenttotal_int = parseFloat(currenttotal.innerText);
var filetotal_int = parseFloat(filetotal.innerText);
if(currenttotal_int < filetotal_int)
{
currenttotal.style.backgroundColor = 'green';
var better_pct = parseFloat((filetotal_int - currenttotal_int) / (currenttotal_int/100)).toFixed(2);
currenttotal.innerText = currenttotal_int + ' (' + better_pct + '% better)'
}
else if(currenttotal_int > filetotal_int)
{
filetotal.style.backgroundColor = 'orange';
var better_pct = parseFloat((currenttotal_int - filetotal_int) / (filetotal_int/100)).toFixed(2);
filetotal.innerText = filetotal_int + ' (' + better_pct + '% worse)'
}
};
</script>";
}
echo "</body></html>";
file_put_contents($file.'.html', ob_get_contents());
ob_end_flush();
exit;
}
else {
?>
<form method="POST" action="">
Save results to file: <input type="checkbox" onclick="showMe('saveFile')" name="saveFile" checked>
<div id="saveFile">
Filename: <input type="text" name="save-file" value="benchmark/benchmark_results.txt">
</div>
<br>
Compare with existing file: <input type="checkbox" onclick="showMe('compare')" name="compare">
<div id="compare" style="display:none">
Filename: <input type="text" name="compare-file" value="benchmark/benchmark_results.txt">
</div>
<br>
MySQL benchmark: <input type="checkbox" onclick="showMe('mybench')" name="mybench">
<div id="mybench" style="display:none">
MySQL username:
<input type="text" name="mysql_user"><br>
MySQL database:
<input type="text" name="mysql_db"><br>
MySQL password:
<input type="password" name="mysql_password"><br>
MySQL host:
<input type="text" name="mysql_host" value="127.0.0.1"><br>
</div>
<br><br><input type="submit" value="Submit" name="collect">
</form>
<?php
}
// -----------------------------------------------------------------------------
// Benchmark functions
// -----------------------------------------------------------------------------
function test_benchmark($settings)
{
global $mysql;
$timeStart = microtime(true);
$result = array();
// $result['version'] = '1.2';
$result['sysinfo']['time'] = date("Y-m-d H:i:s");
$result['sysinfo']['php_version'] = PHP_VERSION;
$result['sysinfo']['platform'] = PHP_OS;
$result['sysinfo']['post_max_size'] = ini_get('post_max_size');
$result['sysinfo']['memory_limit'] = ini_get('memory_limit');
$result['sysinfo']['platform'] = PHP_OS;
$result['sysinfo']['server_name'] = $_SERVER['SERVER_NAME'];
$result['sysinfo']['server_addr'] = $_SERVER['SERVER_ADDR'];
test_math($result['php']);
test_string($result['php']);
test_loops($result['php']);
test_ifelse($result['php']);
test_disk($result['disk']);
if ($mysql == True) {
test_mysql($result['mysql'], $settings);
}
$result['total'] = timer_diff($timeStart);
return $result;
}
function test_math(&$result, $count = 999999)
{
$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));
}
}
$result['math'] = timer_diff($timeStart);
}
function test_string(&$result, $count = 999999)
{
$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));
}
}
$result['string'] = timer_diff($timeStart);
}
function test_loops(&$result, $count = 99999999)
{
$timeStart = microtime(true);
for ($i = 0; $i < $count; ++$i) {
}
$i = 0;
while ($i < $count) {
++$i;
}
$result['loops'] = timer_diff($timeStart);
}
function test_ifelse(&$result, $count = 99999999)
{
$timeStart = microtime(true);
for ($i = 0; $i < $count; $i++) {
if ($i == -1) {
} elseif ($i == -2) {
} else if ($i == -3) {
}
}
$result['ifelse'] = timer_diff($timeStart);
}
function test_disk(&$result, $filesize = 104857600) {
$filename = "randfile";
$src_write = fopen('/dev/urandom', 'r');
$dest_write = fopen($filename, 'w');
$writeStart = microtime(true);
$write = stream_copy_to_stream($src_write, $dest_write, $filesize);
/*
if ($h = fopen($filename, 'w')) {
if ($filesize > 1024) {
for ($i = 0; $i < floor($filesize / 1024); $i++) {
fwrite($h, bin2hex(openssl_random_pseudo_bytes(511)) . PHP_EOL);
}
$filesize = $filesize - (1024 * $i);
}
$mod = $filesize % 2;
fwrite($h, bin2hex(openssl_random_pseudo_bytes(($filesize - $mod) / 2)));
if ($mod) {
fwrite($h, substr(uniqid(), 0, 1));
}
fclose($h);
umask(0000);
chmod($filename, 0644);
}
*/
$result['write'] = timer_diff($writeStart);
fclose($src_write);
fclose($dest_write);
$src_read = fopen($filename, 'r');
$dest_read = fopen('/dev/null', 'w');
$readStart = microtime(true);
$read = stream_copy_to_stream($src_read, $dest_read);
$result['read'] = timer_diff($readStart);
fclose($src_read);
fclose($dest_read);
unlink($filename);
}
function test_mysql(&$result, $settings)
{
$timeStart = microtime(true);
$link = mysqli_connect($settings['db.host'], $settings['db.user'], $settings['db.pw']);
$result['mysql_connect'] = timer_diff($timeStart);
//$arr_return['sysinfo']['mysql_version'] = '';
mysqli_select_db($link, $settings['db.name']);
$result['select_db'] = timer_diff($timeStart);
$dbResult = mysqli_query($link, 'SELECT VERSION() as version;');
$arr_row = mysqli_fetch_array($dbResult);
//$result['sysinfo']['mysql_version'] = $arr_row['version'];
$result['query_version'] = timer_diff($timeStart);
$query = "SELECT BENCHMARK(1000000,ENCODE('hello',RAND()));";
$dbResult = mysqli_query($link, $query);
$result['query_benchmark'] = timer_diff($timeStart);
mysqli_close($link);
$result['total_mysql'] = timer_diff($timeStart);
return $result;
}
function timer_diff($timeStart)
{
return number_format(microtime(true) - $timeStart, 3);
}
function array_to_html($array, $step, $type='')
{
$result = '';
if (is_array($array)) {
if ($type) { $result .= '<table id='.$step.$type.'>'; }
else { $result .= '<table>'; }
foreach ($array as $k => $v) {
$result .= "<tr><td>";
$result .= '<strong>' . htmlentities($k) . "</strong></td>";
if ($k == "total") { $result .= "<td id=".$step.$k.">"; }
else { $result .= "<td>"; }
if ($k == "php" || $k == "disk" || $k =="mysql") { $result .= array_to_html($v, $step, $k); }
else { $result .= array_to_html($v, $step); }
$result .= "</td></tr>";
}
$result .= "</table>";
} else {
$result = htmlentities($array);
}
return $result;
}