class Benchmark::Memory::HeldResults
Collate results that should be held until the next run.
Attributes
@return [String, IO] The path to write held results to.
@return [Hash{String => Measurement}] Held results from previous runs.
Public Class Methods
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 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
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
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
Check whether to hold results.
@return [Boolean]
# File lib/benchmark/memory/held_results.rb, line 62 def holding? !!@path end
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 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
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