Phoenix.SocketClient.PerformanceMonitor (phoenix_socket_client v0.8.0)

View Source

Comprehensive performance monitoring for Phoenix Socket Client.

This module provides centralized performance monitoring and metrics collection from all optimization components. It aggregates data from binary pools, route caches, hibernation manager, and transport performance to provide a complete view of system performance.

Features

  • Real-time performance metrics
  • Memory usage monitoring
  • Connection performance tracking
  • Cache hit rate analysis
  • Hibernation effectiveness
  • Network throughput metrics
  • Alerting for performance issues

Usage

The monitor is automatically started by the socket supervisor and provides performance data through various interfaces.

Metrics Collected

  • Binary pool efficiency and memory savings
  • Route cache hit rates and size
  • Process hibernation statistics
  • Network throughput and latency
  • Message processing rates
  • Memory usage patterns
  • Error rates and types

Summary

Types

Performance metrics structure

Monitor configuration options

t()

Monitor state

Functions

Returns a specification to start this module under a supervisor.

Triggers immediate metrics collection.

Generates performance report.

Gets current performance metrics.

Gets performance summary statistics.

Starts the performance monitor.

Types

metrics()

@type metrics() :: %{
  timestamp: integer(),
  binary_pool: map(),
  route_cache: map(),
  hibernation: map(),
  transport: map(),
  system: map(),
  alerts: [map()]
}

Performance metrics structure

opts()

@type opts() :: [
  collection_interval: non_neg_integer(),
  retention_period: non_neg_integer(),
  registry_name: atom(),
  alert_thresholds: map()
]

Monitor configuration options

t()

@type t() :: %Phoenix.SocketClient.PerformanceMonitor{
  alert_thresholds: map(),
  collection_interval: non_neg_integer(),
  collection_timer: reference() | nil,
  current_metrics: metrics(),
  metrics_history: :ets.tid(),
  registry_name: atom() | nil,
  retention_period: non_neg_integer(),
  stats: %{
    collections: non_neg_integer(),
    alerts_triggered: non_neg_integer(),
    uptime_start: integer()
  }
}

Monitor state

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

collect_metrics(monitor_pid)

@spec collect_metrics(pid()) :: :ok

Triggers immediate metrics collection.

Useful for on-demand performance checks.

generate_report(monitor_pid, format \\ :text)

@spec generate_report(pid(), atom()) :: {:ok, String.t()} | {:error, term()}

Generates performance report.

Creates a comprehensive performance report in various formats.

get_metrics(monitor_pid)

@spec get_metrics(pid()) :: {:ok, metrics()} | {:error, term()}

Gets current performance metrics.

Returns the most recent performance data collected.

get_metrics_history(monitor_pid, from_time, to_time \\ System.monotonic_time(:millisecond))

@spec get_metrics_history(pid(), integer(), integer()) ::
  {:ok, [metrics()]} | {:error, term()}

Gets historical performance metrics.

Returns metrics for a specified time range.

get_summary(monitor_pid)

@spec get_summary(pid()) :: {:ok, map()} | {:error, term()}

Gets performance summary statistics.

Returns aggregated statistics for the monitoring period.

start_link(opts \\ [])

@spec start_link(opts()) :: GenServer.on_start()

Starts the performance monitor.

Options

  • :collection_interval - Metrics collection interval (default: 30000ms)
  • :retention_period - How long to retain metrics (default: 1800000ms)
  • :registry_name - Registry name for component discovery
  • :alert_thresholds - Alerting thresholds map

update_alert_thresholds(monitor_pid, thresholds)

@spec update_alert_thresholds(pid(), map()) :: :ok

Updates alert thresholds.

Modifies the thresholds used for performance alerting.