Skip to content

Instantly share code, notes, and snippets.

@noameppel
Created December 6, 2016 13:02
Show Gist options
  • Save noameppel/64a71fd729cd0f3108c8c4a406c590fc to your computer and use it in GitHub Desktop.
Save noameppel/64a71fd729cd0f3108c8c4a406c590fc to your computer and use it in GitHub Desktop.
Charts the number of options in the wp_options table using autoload.
<?php
// Version 1.0
// Author Noam Eppel noam@cleanforest.co
// Related: https://pressjitsu.com/blog/optimizing-wp-options-for-speed/
//
// Requires jquery.canvasjs.min.js from http://canvasjs.com/download-html5-charting-graphing-library/
// Hello WP. Update the path to the wp-load.php file.
require_once '../wp-load.php';
// Administrators Only
if (!is_user_logged_in() || !current_user_can('manage_options')) {
die('Please log in to use this feature.');
}
global $wpdb;
$count = 1;
$output = '';
$starttime = microtime(true); // Start timer
// SQL Transaction
$options = $wpdb->get_results(
"SELECT option_name, option_value FROM wp_options WHERE autoload = 'yes'"
);
$endtime = microtime(true); // End timer
$duration = $endtime - $starttime; //calculates total time taken
$duration = round($duration, 3); // Only keep 3 decimals
foreach ($options as $option) {
// $output .= $option->option_name . '<br />'; // Enable if you want to see the options_name
$count++;
}
$wp_options_count = get_user_meta(1, 'wp_options_count', true); // Must be true. Hardcode this to Admin User 1
//$wp_options_count[] = $count; // Add $count value to $wp_options_count array
$wp_options_count[] = array("count" => $count, "duration" => $duration);
update_user_meta( 1, 'wp_options_count', $wp_options_count );
$wp_options_count = get_user_meta(1, 'wp_options_count', true); // Get updated $wp_options_count value
?>
<!DOCTYPE HTML>
<html>
<head>
<title>CanvasJS Chart jQuery Plugin</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript" src="jquery.canvasjs.min.js"></script>
<script type="text/javascript">
$(function () {
//Better to construct options first and then pass it as a parameter
var options = {
exportEnabled: true,
animationEnabled: true,
title: {
text: "Number of wp_options using autoload"
},
data: [
{
type: "splineArea", //change it to line, area, bar, pie, etc
dataPoints: [
<?php
foreach ($wp_options_count as $value) {
echo "{ y: " . $value['count'] . ", label: " . $value['duration'] . " },\n";
}
?>
]
}
]
};
$("#chartContainer").CanvasJSChart(options);
});
</script>
</head>
<body>
<?php //echo "<h2>Time for Latest SQL Query: " . $duration . "</h2><br /><br />"; ?>
<!-- This is where the canvas.js magic happens -->
<div id="chartContainer" style="height: 300px; width: 100%;"></div>
<p>
<strong>
Y Axis: Number of wp_options using autoload<br />
X Axis: Time to Execution SQL Query
</strong>
</p>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment