Class: Benchmark::HTTP::Seconds

Inherits:
Object
  • Object
show all
Defined in:
lib/benchmark/http/seconds.rb

Constant Summary collapse

UNITS =
["s", "ms", "µs"]
SCALE =
UNITS.size - 1

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ Seconds

Returns a new instance of Seconds.



16
17
18
# File 'lib/benchmark/http/seconds.rb', line 16

def initialize(value)
  @value = value
end

Class Method Details

.[](value) ⇒ Object



12
13
14
# File 'lib/benchmark/http/seconds.rb', line 12

def self.[](value)
  self.new(value)
end

Instance Method Details

#scaleObject



20
21
22
# File 'lib/benchmark/http/seconds.rb', line 20

def scale
  Math.log(@value) / Math.log(1000)
end

#to_sObject



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/benchmark/http/seconds.rb', line 24

def to_s
  scaled_value = @value
  scale = 0
  
  while scaled_value < 1 && scale < SCALE
    scaled_value *= 1000
    scale += 1
  end
  
  return sprintf("%0.#{scale+1}f%s", scaled_value, UNITS.fetch(scale))
end