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.

__init__() None

Initialize empty metrics collection.

add_metadata(key: str, value: Any) None

Add metadata to the metrics collection.

Parameters:
  • key – Metadata key

  • value – Metadata value

clear() None

Clear all collected metrics.

get_summary() dict[str, Any]

Get summary of collected metrics.

Returns:

A dictionary with all collected metrics including total duration.

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

record_timing(operation: str, duration: float) None

Record duration of an operation.

Parameters:
  • operation – Name of the operation

  • duration – Duration in seconds

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

Bases: object

Performance monitoring using only Python standard library.

Tracks duration and memory usage of specific operations.

__init__(log_file: str = 'performance.log') None

Initialize monitor and setup logging.

Parameters:

log_file – Path to the performance log file.

get_operation_stats(operation_name: str) dict[str, Any] | None

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.

measure_operation(operation_name: str, **metadata: Any) Generator[None, None, None]

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

Parameters:
  • operation_name – Human-readable name of the operation.

  • **metadata – Additional context for logging.

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.

__enter__() PerformanceTimer

Start the timer.

Returns:

The timer instance

Return type:

self

__exit__(exc_type: type[BaseException] | None, exc_val: BaseException | None, exc_tb: Any | None) None

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

__init__(operation_name: str, collector: MetricsCollector | None = None, logger_func: Any | None = 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

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”).

sec_interp.core.performance_metrics.performance_monitor(func: Callable) Callable

Automatically monitor function performance.

Wraps the function call with a PerformanceMonitor measurement.