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.



15
16
17
18
# File 'lib/benchmark/memory/held_results.rb', line 15

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

Instance Attribute Details

#pathString, IO



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

def path
  @path
end

#resultsHash{String => Measurement} (readonly)



24
25
26
# File 'lib/benchmark/memory/held_results.rb', line 24

def results
  @results
end

Instance Method Details

#add_result(entry) ⇒ void

This method returns an undefined value.

Add a result to the held results.



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

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.



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

def any?
  if @path.is_a?(String)
    File.exist?(@path)
  else
    @path.size.positive?
  end
end

#cleanupvoid

This method returns an undefined value.

Clean up the results after all results have been collated.



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

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

#holding?Boolean

Check whether to hold results.



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.



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
# 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 = results.map { |result| [result.label, result.measurement] }.to_h
end