File size: 10,026 Bytes
27762e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
"""
Performance Monitoring Module

This module provides performance monitoring and timing instrumentation for
StingrayExplorer operations.

Features:
- Operation timing with context managers
- Performance metrics collection
- Operation history tracking
- Statistical analysis of operation performance
"""

import time
import logging
from typing import Dict, List, Optional, Any
from dataclasses import dataclass, field
from datetime import datetime
from contextlib import contextmanager
import statistics

logger = logging.getLogger(__name__)


@dataclass
class OperationMetric:
    """
    Represents a single operation metric.

    Attributes:
        operation_name: Name of the operation
        start_time: When the operation started
        end_time: When the operation ended
        duration_ms: Duration in milliseconds
        success: Whether the operation succeeded
        metadata: Additional metadata about the operation
    """
    operation_name: str
    start_time: datetime
    end_time: datetime
    duration_ms: float
    success: bool
    metadata: Dict[str, Any] = field(default_factory=dict)


class PerformanceMonitor:
    """
    Performance monitoring system for tracking operation timings and metrics.

    This class provides tools for measuring, recording, and analyzing the
    performance of operations throughout the application.

    Example:
        >>> monitor = PerformanceMonitor()
        >>>
        >>> # Using context manager
        >>> with monitor.track_operation("load_event_list"):
        ...     event_list = EventList.read("file.evt")
        >>>
        >>> # Get statistics
        >>> stats = monitor.get_operation_stats("load_event_list")
        >>> print(f"Average: {stats['avg_ms']:.2f}ms")
    """

    def __init__(self, max_history: int = 1000):
        """
        Initialize the performance monitor.

        Args:
            max_history: Maximum number of metrics to keep in history
        """
        self.max_history = max_history
        self._metrics: List[OperationMetric] = []
        self._operation_counts: Dict[str, int] = {}
        logger.info(f"PerformanceMonitor initialized (max_history={max_history})")

    @contextmanager
    def track_operation(self, operation_name: str, **metadata):
        """
        Context manager for tracking operation performance.

        Args:
            operation_name: Name of the operation being tracked
            **metadata: Additional metadata to store with the metric

        Yields:
            None

        Example:
            >>> with monitor.track_operation("compute_power_spectrum", dt=0.1):
            ...     ps = AveragedPowerspectrum.from_lightcurve(lc, 100)
        """
        start_time = datetime.now()
        start_perf = time.perf_counter()
        success = True
        error = None

        try:
            yield
        except Exception as e:
            success = False
            error = str(e)
            raise
        finally:
            end_time = datetime.now()
            end_perf = time.perf_counter()
            duration_ms = (end_perf - start_perf) * 1000

            # Store error in metadata if operation failed
            if not success and error:
                metadata['error'] = error

            # Create and record metric
            metric = OperationMetric(
                operation_name=operation_name,
                start_time=start_time,
                end_time=end_time,
                duration_ms=duration_ms,
                success=success,
                metadata=metadata
            )

            self._record_metric(metric)

            # Log performance
            status = "SUCCESS" if success else "FAILED"
            logger.info(
                f"Operation '{operation_name}' {status} "
                f"(duration: {duration_ms:.2f}ms)"
            )

    def _record_metric(self, metric: OperationMetric) -> None:
        """Record a metric and maintain history limits."""
        self._metrics.append(metric)

        # Update operation count
        self._operation_counts[metric.operation_name] = (
            self._operation_counts.get(metric.operation_name, 0) + 1
        )

        # Enforce history limit (FIFO)
        if len(self._metrics) > self.max_history:
            oldest = self._metrics.pop(0)
            self._operation_counts[oldest.operation_name] -= 1

    def get_operation_stats(self, operation_name: str) -> Dict[str, Any]:
        """
        Get statistical analysis for a specific operation.

        Args:
            operation_name: Name of the operation

        Returns:
            Dict containing statistics:
            - count: Number of times operation was executed
            - avg_ms: Average duration in milliseconds
            - min_ms: Minimum duration
            - max_ms: Maximum duration
            - median_ms: Median duration
            - std_dev_ms: Standard deviation
            - success_rate: Percentage of successful operations

        Example:
            >>> stats = monitor.get_operation_stats("load_event_list")
            >>> print(f"Executed {stats['count']} times, avg {stats['avg_ms']:.2f}ms")
        """
        # Filter metrics for this operation
        op_metrics = [m for m in self._metrics if m.operation_name == operation_name]

        if not op_metrics:
            return {
                'count': 0,
                'avg_ms': 0.0,
                'min_ms': 0.0,
                'max_ms': 0.0,
                'median_ms': 0.0,
                'std_dev_ms': 0.0,
                'success_rate': 0.0
            }

        durations = [m.duration_ms for m in op_metrics]
        successes = sum(1 for m in op_metrics if m.success)

        return {
            'count': len(op_metrics),
            'avg_ms': statistics.mean(durations),
            'min_ms': min(durations),
            'max_ms': max(durations),
            'median_ms': statistics.median(durations),
            'std_dev_ms': statistics.stdev(durations) if len(durations) > 1 else 0.0,
            'success_rate': (successes / len(op_metrics)) * 100 if op_metrics else 0.0
        }

    def get_all_operation_names(self) -> List[str]:
        """
        Get list of all tracked operation names.

        Returns:
            List of operation names
        """
        return list(self._operation_counts.keys())

    def get_recent_operations(self, limit: int = 10) -> List[OperationMetric]:
        """
        Get most recent operations.

        Args:
            limit: Maximum number of operations to return

        Returns:
            List of recent OperationMetric objects
        """
        return self._metrics[-limit:] if self._metrics else []

    def get_slow_operations(self, threshold_ms: float = 1000.0, limit: int = 10) -> List[OperationMetric]:
        """
        Get operations that exceeded a duration threshold.

        Args:
            threshold_ms: Duration threshold in milliseconds
            limit: Maximum number of operations to return

        Returns:
            List of slow OperationMetric objects, sorted by duration (slowest first)
        """
        slow_ops = [m for m in self._metrics if m.duration_ms > threshold_ms]
        slow_ops.sort(key=lambda x: x.duration_ms, reverse=True)
        return slow_ops[:limit]

    def get_failed_operations(self, limit: int = 10) -> List[OperationMetric]:
        """
        Get operations that failed.

        Args:
            limit: Maximum number of operations to return

        Returns:
            List of failed OperationMetric objects (most recent first)
        """
        failed = [m for m in self._metrics if not m.success]
        return failed[-limit:] if failed else []

    def get_summary(self) -> Dict[str, Any]:
        """
        Get overall performance summary.

        Returns:
            Dict with summary statistics:
            - total_operations: Total number of tracked operations
            - unique_operations: Number of unique operation types
            - total_duration_ms: Total time spent in all operations
            - avg_duration_ms: Average operation duration
            - success_rate: Overall success rate
            - most_frequent: Most frequently called operation
            - slowest: Slowest operation type (by average)
        """
        if not self._metrics:
            return {
                'total_operations': 0,
                'unique_operations': 0,
                'total_duration_ms': 0.0,
                'avg_duration_ms': 0.0,
                'success_rate': 0.0,
                'most_frequent': None,
                'slowest': None
            }

        total_duration = sum(m.duration_ms for m in self._metrics)
        total_success = sum(1 for m in self._metrics if m.success)

        # Find most frequent operation
        most_frequent = max(self._operation_counts.items(), key=lambda x: x[1])[0] if self._operation_counts else None

        # Find slowest operation (by average)
        slowest = None
        slowest_avg = 0.0
        for op_name in self.get_all_operation_names():
            stats = self.get_operation_stats(op_name)
            if stats['avg_ms'] > slowest_avg:
                slowest_avg = stats['avg_ms']
                slowest = op_name

        return {
            'total_operations': len(self._metrics),
            'unique_operations': len(self._operation_counts),
            'total_duration_ms': total_duration,
            'avg_duration_ms': total_duration / len(self._metrics),
            'success_rate': (total_success / len(self._metrics)) * 100,
            'most_frequent': most_frequent,
            'slowest': slowest
        }

    def clear_history(self) -> None:
        """Clear all recorded metrics."""
        count = len(self._metrics)
        self._metrics.clear()
        self._operation_counts.clear()
        logger.info(f"Cleared performance history ({count} metrics)")


# Global performance monitor instance
performance_monitor = PerformanceMonitor()