PluginProbe
WPWaterMark 轻水印插件 / trunk
WPWaterMark 轻水印插件 vtrunk
5.2.4 5.2.2 5.2.1 5.1.7 5.1.6 trunk 4.3 5.0.0 5.0.1 5.1.2 5.1.3 5.1.4 5.1.5
wpwatermark / WaterMarkPerformance.php

WaterMarkPerformance.php in WPWaterMark 轻水印插件 trunk, at WaterMarkPerformance.php

188 lines 5.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * WaterMark Performance Monitor Class
4 *
5 * Monitors and logs watermark processing performance
6 */
7 class WaterMarkPerformance {
8 private $start_time;
9 private $start_memory;
10 private $log_file;
11
12 /**
13 * 是否启用性能日志(默认�
14 �闭,避�
15 �长期占用磁盘)
16 */
17 public static function isEnabled(): bool {
18 return (bool) apply_filters('wpwatermark_performance_logging_enabled', false);
19 }
20
21 /**
22 * Constructor
23 */
24 public function __construct() {
25 $this->log_file = plugin_dir_path(__FILE__) . 'logs/watermark_performance.log';
26
27 if (!self::isEnabled()) {
28 return;
29 }
30
31 $log_dir = dirname($this->log_file);
32 if (!file_exists($log_dir)) {
33 wp_mkdir_p($log_dir);
34 }
35 }
36
37 /**
38 * Start monitoring
39 */
40 public function startMonitoring() {
41 if (!self::isEnabled()) {
42 return;
43 }
44
45 $this->start_time = microtime(true);
46 $this->start_memory = memory_get_usage();
47 }
48
49 /**
50 * End monitoring and log results
51 *
52 * @param string $operation Operation being monitored
53 * @param array $metadata Additional metadata to log
54 */
55 public function endMonitoring($operation, $metadata = []) {
56 if (!self::isEnabled()) {
57 return;
58 }
59
60 $end_time = microtime(true);
61 $end_memory = memory_get_usage();
62
63 $duration = round(($end_time - $this->start_time) * 1000, 2); // Convert to milliseconds
64 $memory_used = round(($end_memory - $this->start_memory) / 1024 / 1024, 2); // Convert to MB
65
66 $log_data = array_merge([
67 'timestamp' => date('Y-m-d H:i:s'),
68 'operation' => $operation,
69 'duration_ms' => $duration,
70 'memory_mb' => $memory_used
71 ], $metadata);
72
73 $this->logPerformance($log_data);
74 }
75
76 /**
77 * Log performance data
78 */
79 private function logPerformance($data) {
80 if (!self::isEnabled()) {
81 return;
82 }
83
84 $log_entry = json_encode($data) . "\n";
85
86 if (file_exists($this->log_file) && filesize($this->log_file) > 5 * 1024 * 1024) { // 5MB limit
87 $this->rotateLogFile();
88 }
89
90 file_put_contents($this->log_file, $log_entry, FILE_APPEND);
91 }
92
93 /**
94 * Rotate log file when it gets too large
95 */
96 private function rotateLogFile() {
97 $backup_file = $this->log_file . '.' . date('Y-m-d-H-i-s') . '.bak';
98 rename($this->log_file, $backup_file);
99
100 // Keep only last 5 backup files
101 $backup_files = glob($this->log_file . '.*.bak');
102 if (count($backup_files) > 5) {
103 usort($backup_files, function($a, $b) {
104 return filemtime($b) - filemtime($a);
105 });
106
107 $files_to_delete = array_slice($backup_files, 5);
108 foreach ($files_to_delete as $file) {
109 unlink($file);
110 }
111 }
112 }
113
114 /**
115 * Get performance statistics
116 */
117 public function getStatistics($period = '24h') {
118 $stats = [
119 'total_operations' => 0,
120 'avg_duration' => 0,
121 'avg_memory' => 0,
122 'max_duration' => 0,
123 'max_memory' => 0
124 ];
125
126 if (!file_exists($this->log_file)) {
127 return $stats;
128 }
129
130 $cutoff_time = strtotime('-' . $period);
131 $total_duration = 0;
132 $total_memory = 0;
133
134 $handle = fopen($this->log_file, 'r');
135 while (($line = fgets($handle)) !== false) {
136 $data = json_decode($line, true);
137 if (!$data) continue;
138
139 $log_time = strtotime($data['timestamp']);
140 if ($log_time < $cutoff_time) continue;
141
142 $stats['total_operations']++;
143 $total_duration += $data['duration_ms'];
144 $total_memory += $data['memory_mb'];
145
146 $stats['max_duration'] = max($stats['max_duration'], $data['duration_ms']);
147 $stats['max_memory'] = max($stats['max_memory'], $data['memory_mb']);
148 }
149 fclose($handle);
150
151 if ($stats['total_operations'] > 0) {
152 $stats['avg_duration'] = round($total_duration / $stats['total_operations'], 2);
153 $stats['avg_memory'] = round($total_memory / $stats['total_operations'], 2);
154 }
155
156 return $stats;
157 }
158
159 /**
160 * Clean old log entries
161 */
162 public function cleanOldLogs($days = 30) {
163 if (!self::isEnabled() || !file_exists($this->log_file)) {
164 return;
165 }
166
167 $cutoff_time = strtotime('-' . $days . ' days');
168 $temp_file = $this->log_file . '.temp';
169
170 $handle = fopen($this->log_file, 'r');
171 $temp_handle = fopen($temp_file, 'w');
172
173 while (($line = fgets($handle)) !== false) {
174 $data = json_decode($line, true);
175 if (!$data) continue;
176
177 $log_time = strtotime($data['timestamp']);
178 if ($log_time >= $cutoff_time) {
179 fwrite($temp_handle, $line);
180 }
181 }
182
183 fclose($handle);
184 fclose($temp_handle);
185
186 rename($temp_file, $this->log_file);
187 }
188 }