# sec_interp.core.performance_metrics module

Performance metrics module for SecInterp plugin.

This module provides tools for tracking performance and resource usage across the plugin’s operations.

class sec_interp.core.performance_metrics.MetricsCollector

  Bases: `object`

  Collects and aggregates performance metrics.

  timings

    Dictionary mapping operation names to durations.

  counts

    Dictionary mapping metric names to integer counts.

  metadata

    Dictionary of additional context and metadata.

  Initialize empty metrics collection.

  __init__()

    Initialize empty metrics collection.

  record_timing(operation: str, duration: float) → None

    Record duration of an operation.

    Parameters:

      - operation – Name of the operation

      - duration – Duration in seconds

  record_count(metric: str, count: int) → None

    Record a count metric (e.g. number of points).

    Parameters:

      - metric – Name of the metric

      - count – Count value

  add_metadata(key: str, value: Any) → None

    Add metadata to the metrics collection.

    Parameters:

      - key – Metadata key

      - value – Metadata value

  get_summary() → dict[str, Any]

    Get summary of collected metrics.

    Returns:

      A dictionary with all collected metrics including total duration.

  clear() → None

    Clear all collected metrics.

class sec_interp.core.performance_metrics.PerformanceTimer(operation_name: str, collector: MetricsCollector | None = None, logger_func: Any | None = None)

  Bases: `object`

  Context manager for timing specific operations.

  Initialize timer.

  Parameters:

    - operation_name – Name of operation to measure

    - collector – Optional metrics collector to record into

    - logger_func – Optional logger function for immediate logging

  __init__(operation_name: str, collector: MetricsCollector | None = None, logger_func: Any | None = None)

    Initialize timer.

    Parameters:

      - operation_name – Name of operation to measure

      - collector – Optional metrics collector to record into

      - logger_func – Optional logger function for immediate logging

  __enter__()

    Start the timer.

    Returns:

      The timer instance

    Return type:

      self

  __exit__(exc_type, exc_val, exc_tb)

    Stop the timer and record/log validity.

    Parameters:

      - exc_type – Exception type if raised

      - exc_val – Exception value if raised

      - exc_tb – Exception traceback if raised

sec_interp.core.performance_metrics.format_duration(seconds: float) → str

  Format duration in human readable format.

  Parameters:

    seconds – Duration in seconds.

  Returns:

    Formatted string (e.g. “1.2s”, “150ms”, “100µs”).

class sec_interp.core.performance_metrics.PerformanceMonitor(log_file='performance.log')

  Bases: `object`

  Performance monitoring using only Python standard library.

  Tracks duration and memory usage of specific operations.

  Initialize monitor and setup logging.

  Parameters:

    log_file – Path to the performance log file.

  __init__(log_file='performance.log')

    Initialize monitor and setup logging.

    Parameters:

      log_file – Path to the performance log file.

  measure_operation(operation_name, **metadata)

    Context manager to measure operation performance (time and memory).

    Parameters:

      - operation_name – Human-readable name of the operation.

      - **metadata – Additional context for logging.

  get_operation_stats(operation_name)

    Calculate statistics for multiple runs of an operation.

    Parameters:

      operation_name – Name of the operation to analyze.

    Returns:

      Dictionary with mean/min/max duration and memory usage.

sec_interp.core.performance_metrics.performance_monitor(func)

  Automatically monitor function performance.

  Wraps the function call with a PerformanceMonitor measurement.