Class: Fractor::PerformanceMonitor

Inherits:
Object
  • Object
show all
Defined in:
lib/fractor/performance_monitor.rb

Overview

Monitors and tracks performance metrics for Fractor supervisors and workers.

Collects metrics including:

  • Jobs processed count
  • Latency statistics (average, p50, p95, p99)
  • Throughput (jobs/second)
  • Worker utilization
  • Queue depth over time
  • Memory usage

Examples:

Basic usage

supervisor = Fractor::Supervisor.new(...)
monitor = Fractor::PerformanceMonitor.new(supervisor)
monitor.start

# ... run workload ...

monitor.stop
puts monitor.report

With custom sampling interval

monitor = Fractor::PerformanceMonitor.new(
  supervisor,
  sample_interval: 0.5  # Sample every 500ms
)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(supervisor, sample_interval: 1.0) ⇒ PerformanceMonitor

Create a new performance monitor

Parameters:

  • supervisor (Supervisor)

    The supervisor to monitor

  • sample_interval (Float) (defaults to: 1.0)

    How often to sample metrics (seconds)



39
40
41
42
43
44
45
46
47
# File 'lib/fractor/performance_monitor.rb', line 39

def initialize(supervisor, sample_interval: 1.0)
  @supervisor = supervisor
  @sample_interval = sample_interval
  @metrics = PerformanceMetricsCollector.new
  @start_time = nil
  @end_time = nil
  @monitoring = false
  @monitor_thread = nil
end

Instance Attribute Details

#end_timeObject (readonly)

Returns the value of attribute end_time.



33
34
35
# File 'lib/fractor/performance_monitor.rb', line 33

def end_time
  @end_time
end

#metricsObject (readonly)

Returns the value of attribute metrics.



33
34
35
# File 'lib/fractor/performance_monitor.rb', line 33

def metrics
  @metrics
end

#start_timeObject (readonly)

Returns the value of attribute start_time.



33
34
35
# File 'lib/fractor/performance_monitor.rb', line 33

def start_time
  @start_time
end

#supervisorObject (readonly)

Returns the value of attribute supervisor.



33
34
35
# File 'lib/fractor/performance_monitor.rb', line 33

def supervisor
  @supervisor
end

Instance Method Details

#monitoring?Boolean

Check if currently monitoring

Returns:

  • (Boolean)


78
79
80
# File 'lib/fractor/performance_monitor.rb', line 78

def monitoring?
  @monitoring
end

#record_job(latency, success: true) ⇒ void

This method returns an undefined value.

Record a job completion

Parameters:

  • latency (Float)

    Job latency in seconds

  • success (Boolean) (defaults to: true)

    Whether job succeeded



139
140
141
# File 'lib/fractor/performance_monitor.rb', line 139

def record_job(latency, success: true)
  @metrics.record_job(latency, success: success)
end

#reportString

Generate a human-readable report

Returns:

  • (String)

    Formatted report



115
116
117
# File 'lib/fractor/performance_monitor.rb', line 115

def report
  PerformanceReportGenerator.generate_report(snapshot)
end

#snapshotHash

Get current metrics snapshot

Returns:

  • (Hash)

    Current metrics



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
# File 'lib/fractor/performance_monitor.rb', line 85

def snapshot
  {
    jobs_processed: @metrics.jobs_processed,
    jobs_succeeded: @metrics.jobs_succeeded,
    jobs_failed: @metrics.jobs_failed,
    average_latency: @metrics.average_latency,
    p50_latency: @metrics.percentile(50),
    p95_latency: @metrics.percentile(95),
    p99_latency: @metrics.percentile(99),
    throughput: calculate_throughput,
    queue_depth: current_queue_depth,
    queue_depth_avg: @metrics.average_queue_depth,
    queue_depth_max: @metrics.max_queue_depth,
    enqueue_rate: @metrics.enqueue_rate(uptime),
    dequeue_rate: @metrics.dequeue_rate(uptime),
    average_wait_time: @metrics.average_wait_time,
    p50_wait_time: @metrics.wait_time_percentile(50),
    p95_wait_time: @metrics.wait_time_percentile(95),
    p99_wait_time: @metrics.wait_time_percentile(99),
    worker_count: worker_count,
    active_workers: active_worker_count,
    worker_utilization: worker_utilization,
    memory_mb: current_memory_mb,
    uptime: uptime,
  }
end

#startvoid

This method returns an undefined value.

Start monitoring



52
53
54
55
56
57
58
59
60
61
# File 'lib/fractor/performance_monitor.rb', line 52

def start
  return if @monitoring

  @monitoring = true
  @start_time = Time.now
  @metrics.reset

  # Start background monitoring thread
  @monitor_thread = Thread.new { monitor_loop }
end

#stopvoid

This method returns an undefined value.

Stop monitoring



66
67
68
69
70
71
72
73
# File 'lib/fractor/performance_monitor.rb', line 66

def stop
  return unless @monitoring

  @monitoring = false
  @end_time = Time.now
  @monitor_thread&.join
  @monitor_thread = nil
end

#to_json(*_args) ⇒ String

Export metrics in JSON format

Returns:

  • (String)

    JSON representation



122
123
124
# File 'lib/fractor/performance_monitor.rb', line 122

def to_json(*_args)
  PerformanceReportGenerator.to_json(snapshot)
end

#to_prometheusString

Export metrics in Prometheus format

Returns:

  • (String)

    Prometheus metrics



129
130
131
132
# File 'lib/fractor/performance_monitor.rb', line 129

def to_prometheus
  stats = snapshot
  PerformanceReportGenerator.to_prometheus(stats, @metrics.total_latency)
end