Context manager for timing operations with enhanced metrics tracking.
Copy
from transfer_learning.monitoring.metrics import Timerwith Timer(metrics_tracker, "operation_name", operation_type="processing"): # Your operation code here
from transfer_learning.monitoring.metrics import MetricsTracker, Timer# Initialize metrics trackermetrics = MetricsTracker()# Start tracking a video processing runmetrics.start_processing("video_123")try: # Use timer to track operation duration with Timer(metrics, "frame_processing"): process_frames() # Record an API call with Timer(metrics, "openai_api", operation_type="api"): response = openai.ChatCompletion.create(...) # Add custom metrics metrics.add_custom_metric("quality_score", 0.95)finally: # End tracking and save metrics metrics.end_processing()
metrics.start_processing("video_id")try: # Your processing codefinally: metrics.end_processing()
Use the Timer context manager for accurate timing:
Copy
with Timer(metrics, "operation_name"): # Your operation code
Add custom metrics for application-specific monitoring:
Copy
metrics.add_custom_metric("key", value)
Monitor system resources for performance optimization:
Copy
if metrics.current_metrics.system.cpu_percent > 80: # Adjust processing parameters reduce_batch_size()
Use appropriate operation types for timing:
Copy
# For processing operationswith Timer(metrics, "process_frames", operation_type="processing"): process_frames()# For API callswith Timer(metrics, "openai_api", operation_type="api"): call_api()