class Benchmark::Memory::HeldResults

Collate results that should be held until the next run.

Attributes

path[RW]

@return [String, IO] The path to write held results to.

results[R]

@return [Hash{String => Measurement}] Held results from previous runs.

Public Class Methods

new(path = nil) click to toggle source

Instantiate a new set of held results on a path.

@param path [String, IO] The path to write held results to.

# File lib/benchmark/memory/held_results.rb, line 13
def initialize(path = nil)
  @path = path
  @results = {}
end

Public Instance Methods

add_result(entry) click to toggle source

Add a result to the held results.

@param entry [Report::Entry] The entry to hold.

@return [void]

# 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?() click to toggle source

Check whether any results have been stored.

@return [Boolean]

# 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
cleanup() click to toggle source

Clean up the results after all results have been collated.

@return [void]

# 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?() click to toggle source

Check whether to hold results.

@return [Boolean]

# File lib/benchmark/memory/held_results.rb, line 62
def holding?
  !!@path
end
include?(entry) click to toggle source

Check whether an entry has been added to the results.

@param entry [#label] The entry to check.

@return [Boolean]

# File lib/benchmark/memory/held_results.rb, line 71
def include?(entry)
  holding? && any? && results.key?(entry.label)
end
load() click to toggle source

Load results from the serialized output.

@return [void]

# 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

Private Instance Methods

with_hold_file(access_mode = "r") { |f| ... } click to toggle source

Execute a block on the hold file.

@param access_mode [String] The mode to use when opening the file. @param _block [Proc] The block to execute on each line of the file.

@return [void]

# File lib/benchmark/memory/held_results.rb, line 97
def with_hold_file(access_mode = "r", &_block)
  return unless @path

  if @path.is_a?(String)
    File.open(@path, access_mode) do |f|
      yield f
    end
  else
    yield @path
  end
end