module Benchmark::Memory::Helpers

Helper methods for formatting output.

Public Class Methods

scale(value) click to toggle source

Scale a value into human-understandable terms.

@param value [Integer, Float] The value to scale.

@return [String] The scaled value.

# File lib/benchmark/memory/helpers.rb, line 25
def scale(value)
  scale = Math.log10(value)
  scale = 0 if scale.infinite?
  scale = (scale / 3).to_i
  suffix =
    case scale
    when 1 then "k"
    when 2 then "M"
    when 3 then "B"
    when 4 then "T"
    when 5 then "Q"
    else
      scale = 0
      " "
    end

  format("%10.3f#{suffix}", value.to_f / (1000**scale))
end

Public Instance Methods

rjust(label) click to toggle source

Right-justifies to a length of 20 or adds a line of padding when longer.

@param label [#to_s] The label to justify.

@return [String] The justified label.

# File lib/benchmark/memory/helpers.rb, line 10
def rjust(label)
  label = label.to_s

  if label.size > 20
    "#{label}\n#{' ' * 20}"
  else
    label.rjust(20)
  end
end

Private Instance Methods

scale(value) click to toggle source

Scale a value into human-understandable terms.

@param value [Integer, Float] The value to scale.

@return [String] The scaled value.

# File lib/benchmark/memory/helpers.rb, line 25
def scale(value)
  scale = Math.log10(value)
  scale = 0 if scale.infinite?
  scale = (scale / 3).to_i
  suffix =
    case scale
    when 1 then "k"
    when 2 then "M"
    when 3 then "B"
    when 4 then "T"
    when 5 then "Q"
    else
      scale = 0
      " "
    end

  format("%10.3f#{suffix}", value.to_f / (1000**scale))
end