Class: Benchmark::Memory::Report::Comparator

Inherits:
Object
  • Object
show all
Defined in:
lib/benchmark/memory/report/comparator.rb

Overview

Compares two Entry for the purposes of sorting and outputting a Comparison.

Constant Summary collapse

METRICS =
i[memory objects strings].freeze
VALUES =
i[allocated retained].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(metric: :memory, value: :allocated) ⇒ Comparator

Instantiate a new comparator

Parameters:

  • metric (Symbol) (defaults to: :memory)

    (see #metric)

  • value (Symbol) (defaults to: :allocated)

    (see #value)

Raises:

  • (ArgumentError)


32
33
34
35
36
37
38
# File 'lib/benchmark/memory/report/comparator.rb', line 32

def initialize(metric: :memory, value: :allocated)
  raise ArgumentError, "Invalid metric: #{metric.inspect}" unless METRICS.include? metric
  raise ArgumentError, "Invalid value: #{value.inspect}" unless VALUES.include? value

  @metric = metric
  @value = value
end

Instance Attribute Details

#metricSymbol (readonly)

Returns The metric to compare, one of :memory, :objects, or :strings.

Returns:

  • (Symbol)

    The metric to compare, one of :memory, :objects, or :strings



41
42
43
# File 'lib/benchmark/memory/report/comparator.rb', line 41

def metric
  @metric
end

#valueSymbol (readonly)

Returns The value to compare, one of :allocated or :retained.

Returns:

  • (Symbol)

    The value to compare, one of :allocated or :retained



44
45
46
# File 'lib/benchmark/memory/report/comparator.rb', line 44

def value
  @value
end

Class Method Details

.from_spec(spec) ⇒ Comparator

Instantiates a Benchmark::Memory::Report::Comparator from a spec given by Job#compare!

Parameters:

Returns:

Raises:

  • (ArgumentError)


18
19
20
21
22
23
24
25
26
# File 'lib/benchmark/memory/report/comparator.rb', line 18

def self.from_spec(spec)
  raise ArgumentError, 'Only send a single metric and value, in the form memory: :allocated' if spec.length > 1

  metric, value = *spec.first
  metric ||= :memory
  value  ||= :allocated

  new(metric: metric, value: value)
end

Instance Method Details

#==(other) ⇒ Boolean

Checks whether a Benchmark::Memory::Report::Comparator equals another

Parameters:

  • other (Benchmark::Memory::Comparator)

    The comparator to check against

Returns:

  • (Boolean)


51
52
53
# File 'lib/benchmark/memory/report/comparator.rb', line 51

def ==(other)
  metric == other.metric && value == other.value
end

#to_procProc

Converts the Benchmark::Memory::Report::Comparator to a Proc for passing to a block

Returns:

  • (Proc)


58
59
60
# File 'lib/benchmark/memory/report/comparator.rb', line 58

def to_proc
  proc { |entry| entry.measurement.public_send(metric).public_send(value) }
end