Module: Benchmark::Memory::Helpers

Included in:
Job::IOOutput::ComparisonFormatter, Job::IOOutput::EntryFormatter, Job::IOOutput::MetricFormatter
Defined in:
lib/benchmark/memory/helpers.rb

Overview

Helper methods for formatting output.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.scale(value) ⇒ String

Scale a value into human-understandable terms.

Parameters:

  • value (Integer, Float)

    The value to scale.

Returns:

  • (String)

    The scaled value.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# 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

Instance Method Details

#rjust(label) ⇒ String

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

Parameters:

  • label (#to_s)

    The label to justify.

Returns:

  • (String)

    The justified label.



10
11
12
13
14
15
16
17
18
# 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