class Benchmark::Memory::Job::Task

Hold a labelled job for later measurement.

Attributes

action[R]

@return [#call] The code to be measured.

label[R]

@return [#to_s] The label for the benchmark.

Public Class Methods

new(label, action) click to toggle source

Instantiate a job task for later measurement.

@param label [#to_s] The label for the benchmark. @param action [#call] The code to be measured.

@raise [ArgumentError] if the action does not respond to `#call`.

# File lib/benchmark/memory/job/task.rb, line 15
def initialize(label, action)
  unless action.respond_to?(:call)
    fail(
      ArgumentError,
      "Invalid action (#{@action.inspect} does not respond to call)"
    )
  end

  @label = label
  @action = action
end

Public Instance Methods

call() click to toggle source

Call the action and report on its memory usage.

@return [Measurement] the memory usage measurement of the code.

# File lib/benchmark/memory/job/task.rb, line 36
def call
  result = while_measuring_memory_usage { action.call }

  Measurement.from_result(result)
end

Private Instance Methods

while_measuring_memory_usage(&block) click to toggle source
# File lib/benchmark/memory/job/task.rb, line 44
def while_measuring_memory_usage(&block)
  MemoryProfiler.report({}, &block)
end