Class: Omnizip::ETA::RateCalculator
- Inherits:
-
Object
- Object
- Omnizip::ETA::RateCalculator
- Defined in:
- lib/omnizip/eta/rate_calculator.rb
Overview
Calculates processing rates from sample history.
This class computes various rates (bytes/sec, files/sec) with smoothing over a time window to reduce noise from fluctuations.
Instance Attribute Summary collapse
-
#sample_history ⇒ Object
readonly
Returns the value of attribute sample_history.
-
#window_seconds ⇒ Object
readonly
Returns the value of attribute window_seconds.
Instance Method Summary collapse
-
#bytes_per_second ⇒ Float
Calculate current bytes per second rate.
-
#files_per_second ⇒ Float
Calculate current files per second rate.
-
#format_rate(rate = bytes_per_second) ⇒ String
Format bytes per second as human-readable string.
-
#initialize(sample_history:, window_seconds: 10.0) ⇒ RateCalculator
constructor
Initialize a new rate calculator.
-
#instantaneous_rate ⇒ Float
Calculate instantaneous rate (last two samples).
-
#megabytes_per_second ⇒ Float
Calculate current megabytes per second rate.
-
#stable?(threshold: 0.2) ⇒ Boolean
Check if rate is stable (low variance).
Constructor Details
#initialize(sample_history:, window_seconds: 10.0) ⇒ RateCalculator
Initialize a new rate calculator
20 21 22 23 |
# File 'lib/omnizip/eta/rate_calculator.rb', line 20 def initialize(sample_history:, window_seconds: 10.0) @sample_history = sample_history @window_seconds = window_seconds end |
Instance Attribute Details
#sample_history ⇒ Object (readonly)
Returns the value of attribute sample_history.
14 15 16 |
# File 'lib/omnizip/eta/rate_calculator.rb', line 14 def sample_history @sample_history end |
#window_seconds ⇒ Object (readonly)
Returns the value of attribute window_seconds.
14 15 16 |
# File 'lib/omnizip/eta/rate_calculator.rb', line 14 def window_seconds @window_seconds end |
Instance Method Details
#bytes_per_second ⇒ Float
Calculate current bytes per second rate
28 29 30 |
# File 'lib/omnizip/eta/rate_calculator.rb', line 28 def bytes_per_second sample_history.recent_rate(window_seconds) end |
#files_per_second ⇒ Float
Calculate current files per second rate
42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/omnizip/eta/rate_calculator.rb', line 42 def files_per_second recent = sample_history.recent_samples(window_seconds) return 0.0 if recent.size < 2 first = recent.first last = recent.last time_diff = last. - first. return 0.0 if time_diff <= 0 files_diff = last.files_processed - first.files_processed files_diff / time_diff end |
#format_rate(rate = bytes_per_second) ⇒ String
Format bytes per second as human-readable string
73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/omnizip/eta/rate_calculator.rb', line 73 def format_rate(rate = bytes_per_second) return "0 B/s" if rate.zero? if rate < 1024 "#{rate.round(1)} B/s" elsif rate < 1024 * 1024 "#{(rate / 1024.0).round(1)} KB/s" elsif rate < 1024 * 1024 * 1024 "#{(rate / (1024.0 * 1024.0)).round(1)} MB/s" else "#{(rate / (1024.0 * 1024.0 * 1024.0)).round(1)} GB/s" end end |
#instantaneous_rate ⇒ Float
Calculate instantaneous rate (last two samples)
59 60 61 62 63 64 65 66 67 |
# File 'lib/omnizip/eta/rate_calculator.rb', line 59 def instantaneous_rate return 0.0 if sample_history.size < 2 samples = sample_history.samples last = samples[-1] previous = samples[-2] last.rate_since(previous) end |
#megabytes_per_second ⇒ Float
Calculate current megabytes per second rate
35 36 37 |
# File 'lib/omnizip/eta/rate_calculator.rb', line 35 def megabytes_per_second bytes_per_second / (1024.0 * 1024.0) end |
#stable?(threshold: 0.2) ⇒ Boolean
Check if rate is stable (low variance)
91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/omnizip/eta/rate_calculator.rb', line 91 def stable?(threshold: 0.2) return false if sample_history.size < 5 mean_rate = bytes_per_second return true if mean_rate.zero? # No data = stable std_dev = sample_history.rate_std_dev coefficient_of_variation = std_dev / mean_rate coefficient_of_variation < threshold end |