| 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 |
* Constructor |
| 14 |
*/ |
| 15 |
public function __construct() { |
| 16 |
$this->log_file = plugin_dir_path(__FILE__) . 'logs/watermark_performance.log'; |
| 17 |
|
| 18 |
// Ensure log directory exists |
| 19 |
$log_dir = dirname($this->log_file); |
| 20 |
if (!file_exists($log_dir)) { |
| 21 |
wp_mkdir_p($log_dir); |
| 22 |
} |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* Start monitoring |
| 27 |
*/ |
| 28 |
public function startMonitoring() { |
| 29 |
$this->start_time = microtime(true); |
| 30 |
$this->start_memory = memory_get_usage(); |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* End monitoring and log results |
| 35 |
* |
| 36 |
* @param string $operation Operation being monitored |
| 37 |
* @param array $metadata Additional metadata to log |
| 38 |
*/ |
| 39 |
public function endMonitoring($operation, $metadata = []) { |
| 40 |
$end_time = microtime(true); |
| 41 |
$end_memory = memory_get_usage(); |
| 42 |
|
| 43 |
$duration = round(($end_time - $this->start_time) * 1000, 2); // Convert to milliseconds |
| 44 |
$memory_used = round(($end_memory - $this->start_memory) / 1024 / 1024, 2); // Convert to MB |
| 45 |
|
| 46 |
$log_data = array_merge([ |
| 47 |
'timestamp' => date('Y-m-d H:i:s'), |
| 48 |
'operation' => $operation, |
| 49 |
'duration_ms' => $duration, |
| 50 |
'memory_mb' => $memory_used |
| 51 |
], $metadata); |
| 52 |
|
| 53 |
$this->logPerformance($log_data); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Log performance data |
| 58 |
*/ |
| 59 |
private function logPerformance($data) { |
| 60 |
$log_entry = json_encode($data) . "\n"; |
| 61 |
|
| 62 |
if (file_exists($this->log_file) && filesize($this->log_file) > 5 * 1024 * 1024) { // 5MB limit |
| 63 |
$this->rotateLogFile(); |
| 64 |
} |
| 65 |
|
| 66 |
file_put_contents($this->log_file, $log_entry, FILE_APPEND); |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Rotate log file when it gets too large |
| 71 |
*/ |
| 72 |
private function rotateLogFile() { |
| 73 |
$backup_file = $this->log_file . '.' . date('Y-m-d-H-i-s') . '.bak'; |
| 74 |
rename($this->log_file, $backup_file); |
| 75 |
|
| 76 |
// Keep only last 5 backup files |
| 77 |
$backup_files = glob($this->log_file . '.*.bak'); |
| 78 |
if (count($backup_files) > 5) { |
| 79 |
usort($backup_files, function($a, $b) { |
| 80 |
return filemtime($b) - filemtime($a); |
| 81 |
}); |
| 82 |
|
| 83 |
$files_to_delete = array_slice($backup_files, 5); |
| 84 |
foreach ($files_to_delete as $file) { |
| 85 |
unlink($file); |
| 86 |
} |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Get performance statistics |
| 92 |
*/ |
| 93 |
public function getStatistics($period = '24h') { |
| 94 |
$stats = [ |
| 95 |
'total_operations' => 0, |
| 96 |
'avg_duration' => 0, |
| 97 |
'avg_memory' => 0, |
| 98 |
'max_duration' => 0, |
| 99 |
'max_memory' => 0 |
| 100 |
]; |
| 101 |
|
| 102 |
if (!file_exists($this->log_file)) { |
| 103 |
return $stats; |
| 104 |
} |
| 105 |
|
| 106 |
$cutoff_time = strtotime('-' . $period); |
| 107 |
$total_duration = 0; |
| 108 |
$total_memory = 0; |
| 109 |
|
| 110 |
$handle = fopen($this->log_file, 'r'); |
| 111 |
while (($line = fgets($handle)) !== false) { |
| 112 |
$data = json_decode($line, true); |
| 113 |
if (!$data) continue; |
| 114 |
|
| 115 |
$log_time = strtotime($data['timestamp']); |
| 116 |
if ($log_time < $cutoff_time) continue; |
| 117 |
|
| 118 |
$stats['total_operations']++; |
| 119 |
$total_duration += $data['duration_ms']; |
| 120 |
$total_memory += $data['memory_mb']; |
| 121 |
|
| 122 |
$stats['max_duration'] = max($stats['max_duration'], $data['duration_ms']); |
| 123 |
$stats['max_memory'] = max($stats['max_memory'], $data['memory_mb']); |
| 124 |
} |
| 125 |
fclose($handle); |
| 126 |
|
| 127 |
if ($stats['total_operations'] > 0) { |
| 128 |
$stats['avg_duration'] = round($total_duration / $stats['total_operations'], 2); |
| 129 |
$stats['avg_memory'] = round($total_memory / $stats['total_operations'], 2); |
| 130 |
} |
| 131 |
|
| 132 |
return $stats; |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Clean old log entries |
| 137 |
*/ |
| 138 |
public function cleanOldLogs($days = 30) { |
| 139 |
if (!file_exists($this->log_file)) { |
| 140 |
return; |
| 141 |
} |
| 142 |
|
| 143 |
$cutoff_time = strtotime('-' . $days . ' days'); |
| 144 |
$temp_file = $this->log_file . '.temp'; |
| 145 |
|
| 146 |
$handle = fopen($this->log_file, 'r'); |
| 147 |
$temp_handle = fopen($temp_file, 'w'); |
| 148 |
|
| 149 |
while (($line = fgets($handle)) !== false) { |
| 150 |
$data = json_decode($line, true); |
| 151 |
if (!$data) continue; |
| 152 |
|
| 153 |
$log_time = strtotime($data['timestamp']); |
| 154 |
if ($log_time >= $cutoff_time) { |
| 155 |
fwrite($temp_handle, $line); |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
fclose($handle); |
| 160 |
fclose($temp_handle); |
| 161 |
|
| 162 |
rename($temp_file, $this->log_file); |
| 163 |
} |
| 164 |
} |