<?php
/*
Plugin Name: PHP Memery Stats
Plugin URI:
Description: This Plugin show how much ram PHP is Usiing
Version: 1.0
Author: My Name
Author URI: https://example.com/
License: GPLv2 or later
*/
add_action( 'wp_dashboard_setup', 'memory_usage_setup' );
function memory_usage_setup() {
wp_add_dashboard_widget(
'memory_usage-dashboard-widget',
'Memory Usage',
'memory_usage_dashboard_content',
$control_callback = null
);
}
function bytesToSize($bytes, $precision = 2)
{
$kilobyte = 1024;
$megabyte = $kilobyte * 1024;
$gigabyte = $megabyte * 1024;
$terabyte = $gigabyte * 1024;
if (($bytes >= 0) && ($bytes < $kilobyte)) {
return $bytes . ' B';
} elseif (($bytes >= $kilobyte) && ($bytes < $megabyte)) {
return round($bytes / $kilobyte, $precision) . ' KB';
} elseif (($bytes >= $megabyte) && ($bytes < $gigabyte)) {
return round($bytes / $megabyte, $precision) . ' MB';
} elseif (($bytes >= $gigabyte) && ($bytes < $terabyte)) {
return round($bytes / $gigabyte, $precision) . ' GB';
} elseif ($bytes >= $terabyte) {
return round($bytes / $terabyte, $precision) . ' TB';
} else {
return $bytes . ' B';
}
}
function get_dir_size($directory){
$size = 0;
$files= glob($directory.'/*');
foreach($files as $path){
is_file($path) && $size += filesize($path);
is_dir($path) && get_dir_size($path);
}
return $size;
}
function PluginUrl() {
//Try to use WP API if possible, introduced in WP 2.6
if (function_exists('plugins_url')) return trailingslashit(dirname(__FILE__));
//Try to find manually... can't work if wp-content was renamed or is redirected
$path = dirname(__FILE__);
$path = str_replace("\\","/",$path);
$path = trailingslashit(get_bloginfo('wpurl')) . trailingslashit(substr($path,strpos($path,"wp-content/")));
return $path;
}
function secondsToTime($seconds)
{
// extract hours
$hours = floor($seconds / (60 * 60));
// extract minutes
$divisor_for_minutes = $seconds % (60 * 60);
$minutes = floor($divisor_for_minutes / 60);
// extract the remaining seconds
$divisor_for_seconds = $divisor_for_minutes % 60;
$seconds = ceil($divisor_for_seconds);
// return the final array
$obj = array(
"h" => (int) $hours,
"m" => (int) $minutes,
"s" => (int) $seconds,
);
return implode(':', $obj);
sleep(1);
}
function get_dir_url($directory,$r="",$s="php-mem/") {
return str_replace($s,$r,$directory);
}
function memory_usage_dashboard_content() {
echo '<div id="activity-widget">';
echo '<div id="published-posts" class="activity-block">';
echo "<h4>System Info</h4>";
echo "<ul>";
echo "<li>Operating System <span>".php_uname('s')."</li>";
echo "<li>Release Name <span>".php_uname('r')."</li>";
echo "<li>Version <span>".php_uname('v')."</li>";
echo "<li>Machine Type <span>".php_uname('m')."</li>";
echo "</ul>";
echo "</div>";
echo '<div id="published-posts" class="activity-block">';
echo "<h4>Menory Usage</h4>";
echo "<ul>";
echo "<li>Total ram being used <span>".bytesToSize(memory_get_usage())."</span></li>";
echo "<li>Actule ram being used <span>".bytesToSize(memory_get_peak_usage())."</span></li>";
echo "</ul>";
echo "</div>";
echo '<div id="published-posts" class="activity-block">';
echo "<h4>Disk Space</h4>";
echo "<ul>";
echo "<li>Root directory size <span>".bytesToSize(get_dir_size("./"))."</span></li>";
echo "<li>Plugin directory size <span>".bytesToSize(get_dir_size(get_dir_url(PluginUrl())))."</span></li>";
echo "<li>Uploades directory size <span>".bytesToSize(get_dir_size(get_dir_url(PluginUrl(),"uploads/","plugins/php-mem/")))."</span></li>";
echo "<li>Themes directory size <span>".bytesToSize(get_dir_size(get_dir_url(PluginUrl(),"themes/","plugins/php-mem/")))."</span></li>";
echo "<li>Active Themes directory size <span>".bytesToSize(get_dir_size(get_dir_url(PluginUrl(),"themes/".trailingslashit(get_current_theme()),"plugins/php-mem/")))."</span></li>";
echo "</ul>";
echo "</div>";
echo '<div id="published-posts" class="activity-block">';
echo "<h4>Wordpress Speed</h4>";
echo "<ul>";
echo "<li>Query per second <span>".get_num_queries()." queries in "./*secondsToTime(*/timer_stop(1)/*)*/." seconds</span></li>";
echo "</ul>";
echo "</div>";
echo "</div>";
}
?>