Class: Fractor::PerformanceMetricsCollector

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

Overview

Internal metrics collector for performance monitoring. Thread-safe collection of performance metrics.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePerformanceMetricsCollector

Returns a new instance of PerformanceMetricsCollector.



9
10
11
# File 'lib/fractor/performance_metrics_collector.rb', line 9

def initialize
  reset
end

Instance Attribute Details

#jobs_failedObject (readonly)

Returns the value of attribute jobs_failed.



7
8
9
# File 'lib/fractor/performance_metrics_collector.rb', line 7

def jobs_failed
  @jobs_failed
end

#jobs_processedObject (readonly)

Returns the value of attribute jobs_processed.



7
8
9
# File 'lib/fractor/performance_metrics_collector.rb', line 7

def jobs_processed
  @jobs_processed
end

#jobs_succeededObject (readonly)

Returns the value of attribute jobs_succeeded.



7
8
9
# File 'lib/fractor/performance_metrics_collector.rb', line 7

def jobs_succeeded
  @jobs_succeeded
end

#total_latencyObject (readonly)

Returns the value of attribute total_latency.



7
8
9
# File 'lib/fractor/performance_metrics_collector.rb', line 7

def total_latency
  @total_latency
end

Instance Method Details

#average_latencyFloat

Calculate average latency

Returns:

  • (Float)

    Average latency in seconds



74
75
76
77
78
# File 'lib/fractor/performance_metrics_collector.rb', line 74

def average_latency
  @mutex.synchronize do
    average_latency_unsynchronized
  end
end

#average_queue_depthFloat

Calculate average queue depth

Returns:

  • (Float)

    Average queue depth



97
98
99
100
101
102
103
# File 'lib/fractor/performance_metrics_collector.rb', line 97

def average_queue_depth
  @mutex.synchronize do
    return 0.0 if @queue_depths.empty?

    @queue_depths.sum / @queue_depths.size.to_f
  end
end

#average_wait_timeFloat

Calculate average wait time using Little's Law

Returns:

  • (Float)

    Average wait time in seconds



139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/fractor/performance_metrics_collector.rb', line 139

def average_wait_time
  # Wait time approximation based on queue depth and throughput
  @mutex.synchronize do
    return 0.0 if @queue_depths.empty? || @latencies.empty?

    avg_depth = @queue_depths.sum / @queue_depths.size.to_f
    avg_lat = @total_latency / @latencies.size
    return 0.0 if avg_lat.zero?

    # Little's Law: Wait Time ≈ Queue Length / Throughput
    avg_depth * avg_lat
  end
end

#dequeue_rate(duration) ⇒ Float

Calculate dequeue rate (jobs per second)

Parameters:

  • duration (Float)

    Time period in seconds

Returns:

  • (Float)

    Dequeue rate



130
131
132
133
134
# File 'lib/fractor/performance_metrics_collector.rb', line 130

def dequeue_rate(duration)
  return 0.0 if duration <= 0

  @jobs_processed / duration.to_f
end

#enqueue_rate(duration) ⇒ Float

Calculate enqueue rate (jobs per second)

Parameters:

  • duration (Float)

    Time period in seconds

Returns:

  • (Float)

    Enqueue rate



120
121
122
123
124
# File 'lib/fractor/performance_metrics_collector.rb', line 120

def enqueue_rate(duration)
  return 0.0 if duration <= 0

  @jobs_processed / duration.to_f
end

#max_queue_depthInteger

Get maximum queue depth observed

Returns:

  • (Integer)

    Maximum queue depth



108
109
110
111
112
113
114
# File 'lib/fractor/performance_metrics_collector.rb', line 108

def max_queue_depth
  @mutex.synchronize do
    return 0 if @queue_depths.empty?

    @queue_depths.max
  end
end

#percentile(p) ⇒ Float

Calculate latency percentile

Parameters:

  • p (Integer)

    Percentile (0-100)

Returns:

  • (Float)

    Latency at percentile in seconds



84
85
86
87
88
89
90
91
92
# File 'lib/fractor/performance_metrics_collector.rb', line 84

def percentile(p)
  @mutex.synchronize do
    return 0.0 if @latencies.empty?

    sorted = @latencies.sort
    index = ((p / 100.0) * sorted.size).ceil - 1
    sorted[[index, 0].max]
  end
end

#record_job(latency, success: true) ⇒ void

This method returns an undefined value.

Record a job completion with its latency

Parameters:

  • latency (Float)

    Job latency in seconds

  • success (Boolean) (defaults to: true)

    Whether job succeeded



31
32
33
34
35
36
37
38
39
# File 'lib/fractor/performance_metrics_collector.rb', line 31

def record_job(latency, success: true)
  @mutex.synchronize do
    @jobs_processed += 1
    @jobs_succeeded += 1 if success
    @jobs_failed += 1 unless success
    @latencies << latency
    @total_latency += latency
  end
end

#resetObject

Reset all metrics to initial state



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/fractor/performance_metrics_collector.rb', line 14

def reset
  @jobs_processed = 0
  @jobs_succeeded = 0
  @jobs_failed = 0
  @latencies = []
  @total_latency = 0.0
  @queue_depths = []
  @memory_samples = []
  @utilization_samples = []
  @mutex = Mutex.new
end

#sample_memory(mb) ⇒ void

This method returns an undefined value.

Sample current memory usage

Parameters:

  • mb (Float)

    Memory usage in MB



55
56
57
58
59
# File 'lib/fractor/performance_metrics_collector.rb', line 55

def sample_memory(mb)
  @mutex.synchronize do
    @memory_samples << mb
  end
end

#sample_queue_depth(depth) ⇒ void

This method returns an undefined value.

Sample the current queue depth

Parameters:

  • depth (Integer)

    Current queue depth



45
46
47
48
49
# File 'lib/fractor/performance_metrics_collector.rb', line 45

def sample_queue_depth(depth)
  @mutex.synchronize do
    @queue_depths << depth
  end
end

#sample_worker_utilization(ratio) ⇒ void

This method returns an undefined value.

Sample worker utilization ratio

Parameters:

  • ratio (Float)

    Worker utilization (0.0 to 1.0)



65
66
67
68
69
# File 'lib/fractor/performance_metrics_collector.rb', line 65

def sample_worker_utilization(ratio)
  @mutex.synchronize do
    @utilization_samples << ratio
  end
end

#wait_time_percentile(p) ⇒ Float

Calculate wait time at a given percentile

Parameters:

  • p (Integer)

    Percentile (0-100)

Returns:

  • (Float)

    Wait time at percentile in seconds



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/fractor/performance_metrics_collector.rb', line 157

def wait_time_percentile(p)
  # Simplified wait time percentile based on queue depth percentile
  @mutex.synchronize do
    return 0.0 if @queue_depths.empty?

    sorted = @queue_depths.sort
    index = ((p / 100.0) * sorted.size).ceil - 1
    depth_percentile = sorted[[index, 0].max]

    avg_lat = @total_latency / @latencies.size
    return 0.0 if avg_lat.zero?

    depth_percentile * avg_lat
  end
end