Class: Fractor::PerformanceMetricsCollector
- Inherits:
-
Object
- Object
- Fractor::PerformanceMetricsCollector
- 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
-
#jobs_failed ⇒ Object
readonly
Returns the value of attribute jobs_failed.
-
#jobs_processed ⇒ Object
readonly
Returns the value of attribute jobs_processed.
-
#jobs_succeeded ⇒ Object
readonly
Returns the value of attribute jobs_succeeded.
-
#total_latency ⇒ Object
readonly
Returns the value of attribute total_latency.
Instance Method Summary collapse
-
#average_latency ⇒ Float
Calculate average latency.
-
#average_queue_depth ⇒ Float
Calculate average queue depth.
-
#average_wait_time ⇒ Float
Calculate average wait time using Little's Law.
-
#dequeue_rate(duration) ⇒ Float
Calculate dequeue rate (jobs per second).
-
#enqueue_rate(duration) ⇒ Float
Calculate enqueue rate (jobs per second).
-
#initialize ⇒ PerformanceMetricsCollector
constructor
A new instance of PerformanceMetricsCollector.
-
#max_queue_depth ⇒ Integer
Get maximum queue depth observed.
-
#percentile(p) ⇒ Float
Calculate latency percentile.
-
#record_job(latency, success: true) ⇒ void
Record a job completion with its latency.
-
#reset ⇒ Object
Reset all metrics to initial state.
-
#sample_memory(mb) ⇒ void
Sample current memory usage.
-
#sample_queue_depth(depth) ⇒ void
Sample the current queue depth.
-
#sample_worker_utilization(ratio) ⇒ void
Sample worker utilization ratio.
-
#wait_time_percentile(p) ⇒ Float
Calculate wait time at a given percentile.
Constructor Details
#initialize ⇒ PerformanceMetricsCollector
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_failed ⇒ Object (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_processed ⇒ Object (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_succeeded ⇒ Object (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_latency ⇒ Object (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_latency ⇒ Float
Calculate average latency
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_depth ⇒ Float
Calculate 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_time ⇒ Float
Calculate average wait time using Little's Law
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)
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)
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_depth ⇒ Integer
Get maximum queue depth observed
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
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
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 |
#reset ⇒ Object
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
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
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
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
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 |