Last active
September 15, 2025 09:31
-
-
Save mehrshaddarzi/ff7e2a8ea02f8cbd6a8ecd4b547c6736 to your computer and use it in GitHub Desktop.
WordPress Ram Usage Log
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // افزودن هدر برای مانیتورینگ مصرف رم | |
| function add_memory_usage_header() { | |
| if (!is_admin()) { | |
| $memory_usage = round(memory_get_peak_usage() / 1024 / 1024, 2); | |
| header('X-Memory-Usage: ' . $memory_usage . ' MB'); | |
| } | |
| } | |
| add_action('send_headers', 'add_memory_usage_header'); | |
| // لاگ کردن مصرف رم برای صفحات سنگین | |
| function log_high_memory_usage() { | |
| $memory_usage = memory_get_peak_usage(true); | |
| if ($memory_usage > 4 * 1024 * 1024) { // بیش از ۴MB | |
| error_log('High memory usage on ' . $_SERVER['REQUEST_URI'] . ': ' . round($memory_usage / 1024 / 1024, 2) . 'MB'); | |
| } | |
| } | |
| add_action('shutdown', 'log_high_memory_usage'); | |
| // در فایل functions.php یا یک مکان پلاگین | |
| add_action('shutdown', 'log_memory_usage_per_page'); | |
| function log_memory_usage_per_page() { | |
| // دریافت اطلاعات مصرف رم | |
| $memory_used = memory_get_usage(); | |
| $memory_peak = memory_get_peak_usage(); | |
| $memory_limit = ini_get('memory_limit'); | |
| // اطلاعات صفحه | |
| $current_url = home_url($_SERVER['REQUEST_URI']); | |
| $is_admin = is_admin(); | |
| $is_ajax = defined('DOING_AJAX') && DOING_AJAX; | |
| $is_cron = defined('DOING_CRON') && DOING_CRON; | |
| $is_rest = defined('REST_REQUEST') && REST_REQUEST; | |
| // نوع درخواست | |
| $request_type = 'Frontend'; | |
| if ($is_admin) $request_type = 'Admin'; | |
| if ($is_ajax) $request_type = 'AJAX'; | |
| if ($is_cron) $request_type = 'Cron'; | |
| if ($is_rest) $request_type = 'REST'; | |
| // اطلاعات پست (اگر وجود دارد) | |
| $post_info = ''; | |
| if (is_singular()) { | |
| $post_id = get_the_ID(); | |
| $post_type = get_post_type($post_id); | |
| $post_info = " | Post: {$post_id} ({$post_type})"; | |
| } | |
| // ایجاد پیام لاگ | |
| $log_message = sprintf( | |
| "[%s] %s - Memory: %s MB | Peak: %s MB | Limit: %s | URL: %s%s", | |
| date('Y-m-d H:i:s'), | |
| $request_type, | |
| round($memory_used / 1024 / 1024, 2), | |
| round($memory_peak / 1024 / 1024, 2), | |
| $memory_limit, | |
| $current_url, | |
| $post_info | |
| ); | |
| // نوشتن در فایل لاگ | |
| $log_file = WP_CONTENT_DIR . '/memory-usage.log'; | |
| file_put_contents($log_file, $log_message . PHP_EOL, FILE_APPEND); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment