Class: Benchmark::Memory::HeldResults

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/benchmark/memory/held_results.rb,
lib/benchmark/memory/held_results/serializer.rb,
lib/benchmark/memory/held_results/entry_serializer.rb,
lib/benchmark/memory/held_results/metric_serializer.rb,
lib/benchmark/memory/held_results/measurement_serializer.rb

Overview

Collate results that should be held until the next run.

Defined Under Namespace

Classes: EntrySerializer, MeasurementSerializer, MetricSerializer, Serializer

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = nil) ⇒ HeldResults

Instantiate a new set of held results on a path.

Parameters:

  • path (String, IO) (defaults to: nil)

    The path to write held results to.



13
14
15
16
# File 'lib/benchmark/memory/held_results.rb', line 13

def initialize(path = nil)
  @path = path
  @results = {}
end

Instance Attribute Details

#pathString, IO

Returns The path to write held results to.

Returns:

  • (String, IO)

    The path to write held results to.



19
20
21
# File 'lib/benchmark/memory/held_results.rb', line 19

def path
  @path
end

#resultsHash{String => Measurement} (readonly)

Returns Held results from previous runs.

Returns:

  • (Hash{String => Measurement})

    Held results from previous runs.



22
23
24
# File 'lib/benchmark/memory/held_results.rb', line 22

def results
  @results
end

Instance Method Details

#add_result(entry) ⇒ void

This method returns an undefined value.

Add a result to the held results.

Parameters:



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

def add_result(entry)
  with_hold_file("a") do |file|
    file.write EntrySerializer.new(entry)
    file.write "\n"
  end
end

#any?Boolean

Check whether any results have been stored.

Returns:

  • (Boolean)


42
43
44
45
46
47
48
# File 'lib/benchmark/memory/held_results.rb', line 42

def any?
  if @path.is_a?(String)
    File.exist?(@path)
  else
    @path.size > 0 # rubocop:disable Style/ZeroLengthPredicate
  end
end

#cleanupvoid

This method returns an undefined value.

Clean up the results after all results have been collated.



53
54
55
56
57
# File 'lib/benchmark/memory/held_results.rb', line 53

def cleanup
  if @path.is_a?(String) && File.exist?(@path)
    File.delete(@path)
  end
end

#holding?Boolean

Check whether to hold results.

Returns:

  • (Boolean)


62
63
64
# File 'lib/benchmark/memory/held_results.rb', line 62

def holding?
  !!@path
end

#include?(entry) ⇒ Boolean

Check whether an entry has been added to the results.

Parameters:

  • entry (#label)

    The entry to check.

Returns:

  • (Boolean)


71
72
73
# File 'lib/benchmark/memory/held_results.rb', line 71

def include?(entry)
  holding? && any? && results.key?(entry.label)
end

#loadvoid

This method returns an undefined value.

Load results from the serialized output.



78
79
80
81
82
83
84
85
86
87
# File 'lib/benchmark/memory/held_results.rb', line 78

def load
  return unless holding? && any?

  results = with_hold_file do |file|
    file.map { |line| EntrySerializer.load(line) }
  end
  @results = Hash[results.map do |result|
    [result.label, result.measurement]
  end]
end