forked from kevinohashi/WPPerformanceTester
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWPPerformanceTester_Plugin.php
More file actions
387 lines (355 loc) · 18.5 KB
/
Copy pathWPPerformanceTester_Plugin.php
File metadata and controls
387 lines (355 loc) · 18.5 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
<?php
require_once( 'WPPerformanceTester_LifeCycle.php' );
require_once( 'benchmark.php' );
class WPPerformanceTester_Plugin extends WPPerformanceTester_LifeCycle {
/**
* Override settingsPage()
*/
public function settingsPage() {
if (!current_user_can('manage_options')) {
wp_die(__('You do not have sufficient permissions to access this page.'));
}
$performTest = false;
if ( !empty( $_POST['performTest'] ) && ( $_POST['performTest'] == true ) ) {
//verify nonce
if ( ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST[ '_wpnonce' ] ) ) ) ) {
wp_die( 'Invalid Request. Reload to try again.' );
}else{
$performTest=true;
}
}
?>
<div class="wrap">
<h2>WPPerformanceTester</h2>
<p>WPPerformanceTester performs a series of tests to see how well your server performs. The first set test the raw server performance. The second is WordPress specific. Your results will be displayed and you can see how your results stack up against others.</p>
<form method="post" action="<?php echo esc_url( admin_url('tools.php?page=WPPerformanceTester_PluginSettings') ); ?>">
<input type="hidden" name="performTest" value="true">
<?php wp_nonce_field(); ?>
<input type="submit" value="Begin Performance Test" onclick="this.value='This may take a minute...'">
</form>
<?php
if ( $performTest ) {
//do test
global $wpdb;
$arr_cfg = array();
// We need special handling for hyperdb
if ( is_a( $wpdb, 'hyperdb' ) && ! empty( $wpdb->hyper_servers ) ) {
// Grab a `write` server for the `global` dataset and fallback to `read`.
// We're not really paying attention to priority or have much in the way of error checking. Use at your own risk :)
$db_server = false;
if ( ! empty( $wpdb->hyper_servers['global']['write'] ) ) {
foreach ( $wpdb->hyper_servers['global']['write'] as $group => $dbs ) {
$db_server = current( $dbs );
break;
}
} elseif ( ! empty( $wpdb->hyper_servers['global']['read'] ) ) {
foreach ( $wpdb->hyper_servers['global']['read'] as $group => $dbs ) {
$db_server = current( $dbs );
break;
}
}
if ( $db_server ) {
$arr_cfg['db.host'] = $db_server['host'];
$arr_cfg['db.user'] = $db_server['user'];
$arr_cfg['db.pw'] = $db_server['password'];
$arr_cfg['db.name'] = $db_server['name'];
}
} else {
// Vanilla WordPress install with standard `wpdb`
$arr_cfg['db.host'] = DB_HOST;
$arr_cfg['db.user'] = DB_USER;
$arr_cfg['db.pw'] = DB_PASSWORD;
$arr_cfg['db.name'] = DB_NAME;
}
$arr_benchmark = apply_filters('wpperformancetester_benchmark', test_benchmark($arr_cfg));
$arr_wordpress = apply_filters('wpperformancetester_wordpress', test_wordpress());
//charting from results goes here
?>
<h2>Performance Test Results (in seconds)</h2>
<div id="chartDiv">
<div id="legendDiv"></div>
<canvas id="myChart" height="400" width="600"></canvas>
<p style="text-align: center; font-style: italic;">Test Type</p>
</div>
<p>* Lower (faster) time is better. Please submit your results to improve our industry average data :)</p>
<script>
jQuery(document).ready(function(){
jQuery.getJSON( "https://wphreviews.com/api/wpperformancetester.php?version=<?php echo $this->getVersion(); ?>", function( industryData ) {
//new code
const labels = ["Math (CPU)", "String (CPU)", "Loops (CPU)", "Conditionals (CPU)", "MySql (Database)", "Server Total", "WordPress Performance"];
const data = {
labels: labels,
datasets: [
{
label: "Your Results",
backgroundColor: "rgba(151,187,205,0.5)",
borderColor: "rgba(151,187,205,0.8)",
hoverBackgroundColor: "rgba(151,187,205,0.75)",
hoverBorderColor: "rgba(151,187,205,1)",
data: [<?php echo $arr_benchmark['benchmark']['math']; ?>, <?php echo $arr_benchmark['benchmark']['string']; ?>, <?php echo $arr_benchmark['benchmark']['loops']; ?>, <?php echo $arr_benchmark['benchmark']['ifelse']; ?>, <?php echo $arr_benchmark['benchmark']['mysql_query_benchmark']; ?>, <?php echo $arr_benchmark['total']; ?>, <?php echo $arr_wordpress['time']; ?>]
},
{
label: "Industry Average",
backgroundColor: "rgba(130,130,130,0.5)",
borderColor: "rgba(130,130,130,0.8)",
hoverBackgroundColor: "rgba(130,130,130,0.75)",
hoverBorderColor: "rgba(130,130,130,1)",
data: industryData
}
]
};
const config = {
type: 'bar',
data: data,
options: {
scales: {
y: {
beginAtZero: true
}
},
plugins: {
legend: {
display: true
}
}
},
};
var myChart = new Chart(
document.getElementById('myChart'),
config
);
/* var myChart = new Chart(ctx).Bar(data, {
barShowStroke: false,
multiTooltipTemplate: "<%= datasetLabel %> - <%= value %> Seconds",
});
var legendHolder = document.createElement('div');
legendHolder.innerHTML = myChart.generateLegend();
document.getElementById('legendDiv').appendChild(legendHolder.firstChild);*/
});
});
</script>
<div id="resultTable">
<table width="600">
<caption>Server Performance Benchmarks</caption>
<thead>
<tr>
<th width="300">Test</th>
<th>Execution Time (seconds)</th>
</tr>
</thead>
<tbody>
<tr>
<td><span class="simptip-position-right simptip-smooth" data-tooltip="Times 10,000 mathematical functions in PHP">Math</span></td>
<td><?php echo $arr_benchmark['benchmark']['math']; ?></td>
</tr>
<tr>
<td><span class="simptip-position-right simptip-smooth" data-tooltip="Times 10,000 string manipulation functions in PHP">String Manipulation</span></td>
<td><?php echo $arr_benchmark['benchmark']['string']; ?></td>
</tr>
<tr>
<td><span class="simptip-position-right simptip-smooth" data-tooltip="Times 10,000 increments in PHP while and for loops">Loops</span></td>
<td><?php echo $arr_benchmark['benchmark']['loops']; ?></td>
</tr>
<tr>
<td><span class="simptip-position-right simptip-smooth" data-tooltip="Times 10,000 conditional checks in PHP">Conditionals</span></td>
<td><?php echo $arr_benchmark['benchmark']['ifelse']; ?></td>
</tr>
<tr>
<td><span class="simptip-position-right simptip-smooth" data-tooltip="Time it takes to establish a Mysql Connection">Mysql Connect</span></td>
<td><?php echo $arr_benchmark['benchmark']['mysql_connect']; ?></td>
</tr>
<tr>
<td><span class="simptip-position-right simptip-smooth" data-tooltip="Time it takes to query Mysql version information">Mysql Query Version</span></td>
<td><?php echo $arr_benchmark['benchmark']['mysql_query_version']; ?></td>
</tr>
<tr>
<td><span class="simptip-position-right simptip-smooth" data-tooltip="Time it takes for 1,000,000 ENCODE()s with a random seed">Mysql Query Benchmark</span></td>
<td><?php echo $arr_benchmark['benchmark']['mysql_query_benchmark']; ?></td>
</tr>
</tbody>
<tfoot>
<tr>
<th>Total Time (seconds)</th>
<th><?php echo $arr_benchmark['total']; ?></th>
</tr>
</tfoot>
</table>
<br />
<table width="600">
<caption><span class="simptip-position-bottom simptip-multiline simptip-smooth" data-tooltip="Performs 250 Insert, Select, Update and Delete functions through $wpdb">WordPress Performance Benchmark</span></caption>
<thead>
<tr>
<th width="300">Execution Time (seconds)</th>
<th>Queries Per Second</th>
</tr>
</thead>
<tfoot>
<tr>
<td><?php echo $arr_wordpress['time']; ?></td>
<td><?php echo $arr_wordpress['queries']; ?></td>
</tr>
</tfoot>
</table>
<br />
<table width="600">
<caption>Your Server Information</caption>
<thead>
<tr>
<th width="300">Test</th>
<th>Result</th>
</tr>
</thead>
<tbody>
<tr>
<td>WPPerformanceTester Version</td>
<td><?php echo $this->getVersion(); ?></td>
</tr>
<tr>
<td>System Time</td>
<td><?php echo $arr_benchmark['sysinfo']['time']; ?></td>
</tr>
<tr>
<td>Platform</td>
<td><?php echo $arr_benchmark['sysinfo']['platform']; ?></td>
</tr>
<tr>
<td>Server Name</td>
<td><?php echo $arr_benchmark['sysinfo']['server_name'] ; ?></td>
</tr>
<tr>
<td>Server Address</td>
<td><?php echo $arr_benchmark['sysinfo']['server_addr'] ; ?></td>
</tr>
<tr>
<td>MySql Server</td>
<td><?php echo DB_HOST; ?></td>
</tr>
</tbody>
</table>
</div>
<br />
<br />
<form target="_blank" method="post" action="https://wphreviews.com/wpperformancetester/" class="basic-grey">
<h2>Share Your Results & Write a Review</h2>
<p>All submitted data may be published. Do not include any personal information you do not want publicly listed.
Your data helps us maintain industry performance averages and provide users with real web hosting reviews.</p>
<input type="submit" value="Share Results Only (Do not fill out form)">
<input type="hidden" name="benchresult" value="<?php echo urlencode(json_encode($arr_benchmark)); ?>">
<input type="hidden" name="wordpressresult" value="<?php echo urlencode(json_encode($arr_wordpress)); ?>">
<input type="hidden" name="wpperformancetesterversion" value="<?php echo $this->getVersion(); ?>">
<h3>What Web Host do you use?</h3>
<input type="text" name="host" length="50" placeholder="Company Name">
<h3>Please tell us about your experience</h3>
<br />The Good
<br /><textarea name="reviewpros" rows="4" cols="50" placeholder="Please list the pros"></textarea>
<br />The Bad
<br /><textarea name="reviewcons" rows="4" cols="50" placeholder="Please list the cons"></textarea>
<br />Anything Else?
<br /><textarea name="reviewother" rows="4" cols="50" placeholder="Anything else you would like to add?"></textarea>
<h3>How would you rate your web host overall?</h3>
<input type="radio" name="rating" value="1"><label>1 - The Worst</label>
<input type="radio" name="rating" value="2"><label>2</label>
<input type="radio" name="rating" value="3"><label>3</label>
<input type="radio" name="rating" value="4"><label>4</label>
<input type="radio" name="rating" value="5"><label>5 - The Best</label>
<br /><br />
<h3>Your Information</h3>
Name
<input type="text" name="name" length="50" placeholder="Your Name">
<br />Website
<input type="text" name="website" length="50" placeholder="Your Website">
<br />Twitter
<input type="text" name="twitter" length="50" placeholder="@username">
<br /><br />
<input type="submit" value="Submit Review + Results">
</form>
<?php
}
?>
</div>
<div style="margin-top:500px;">
<a href="https://github.com/kevinohashi/WPPerformanceTester" target="_blank">WPPerformanceTester</a> - Version <?php echo $this->getVersion(); ?>
</div>
<?php
}
/**
* See: http://plugin.michael-simpson.com/?page_id=31
* @return array of option meta data.
*/
public function getOptionMetaData() {
// http://plugin.michael-simpson.com/?page_id=31
return array(
//'_version' => array('Installed Version'), // Leave this one commented-out. Uncomment to test upgrades.
'ATextInput' => array(__('Enter in some text', 'my-awesome-plugin')),
'AmAwesome' => array(__('I like this awesome plugin', 'my-awesome-plugin'), 'false', 'true'),
'CanDoSomething' => array(__('Which user role can do something', 'my-awesome-plugin'),
'Administrator', 'Editor', 'Author', 'Contributor', 'Subscriber', 'Anyone')
);
}
// protected function getOptionValueI18nString($optionValue) {
// $i18nValue = parent::getOptionValueI18nString($optionValue);
// return $i18nValue;
// }
protected function initOptions() {
$options = $this->getOptionMetaData();
if (!empty($options)) {
foreach ($options as $key => $arr) {
if (is_array($arr) && count($arr) > 1) {
$this->addOption($key, $arr[1]);
}
}
}
}
public function getPluginDisplayName() {
return 'WP Performance Tester';
}
protected function getMainPluginFileName() {
return 'wp-performance-tester.php';
}
/**
* See: http://plugin.michael-simpson.com/?page_id=101
* Called by install() to create any database tables if needed.
* Best Practice:
* (1) Prefix all table names with $wpdb->prefix
* (2) make table names lower case only
* @return void
*/
protected function installDatabaseTables() {
// global $wpdb;
// $tableName = $this->prefixTableName('mytable');
// $wpdb->query("CREATE TABLE IF NOT EXISTS `$tableName` (
// `id` INTEGER NOT NULL");
}
/**
* See: http://plugin.michael-simpson.com/?page_id=101
* Drop plugin-created tables on uninstall.
* @return void
*/
protected function unInstallDatabaseTables() {
// global $wpdb;
// $tableName = $this->prefixTableName('mytable');
// $wpdb->query("DROP TABLE IF EXISTS `$tableName`");
}
/**
* Perform actions when upgrading from version X to version Y
* See: http://plugin.michael-simpson.com/?page_id=35
* @return void
*/
public function upgrade() {
}
public function enqueue_scripts_and_style( $hook ) {
if ( $hook != 'tools_page_WPPerformanceTester_PluginSettings' ) {
return;
}
wp_enqueue_script( 'chart-js-3-7', plugins_url('/js/Chart.js', __FILE__) );
wp_enqueue_script( 'jquery');
wp_enqueue_style( 'wppt-style', plugins_url('/css/wppt.css', __FILE__) );
wp_enqueue_style( 'simptip-style', plugins_url('/css/simptip.css', __FILE__) );
}
public function addActionsAndFilters() {
// Add options administration page
// http://plugin.michael-simpson.com/?page_id=47
add_action('admin_menu', array( $this, 'addSettingsSubMenuPage'));
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts_and_style' ) );
}
}