Class: Benchmark::Memory::Job

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/benchmark/memory/job.rb,
lib/benchmark/memory/job/task.rb,
lib/benchmark/memory/job/io_output.rb,
lib/benchmark/memory/job/null_output.rb,
lib/benchmark/memory/job/io_output/entry_formatter.rb,
lib/benchmark/memory/job/io_output/metric_formatter.rb,
lib/benchmark/memory/job/io_output/comparison_formatter.rb

Overview

Encapsulate the memory measurements of reports.

Defined Under Namespace

Classes: IOOutput, NullOutput, Task

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(output: $stdout, quiet: false) ⇒ Job

Instantiate a job for containing memory performance reports.

Parameters:

  • output (#puts) (defaults to: $stdout)

    The output to use for showing the job results.

  • quiet (Boolean) (defaults to: false)

    A flag for stopping output.



23
24
25
26
27
28
29
# File 'lib/benchmark/memory/job.rb', line 23

def initialize(output: $stdout, quiet: false)
  @full_report = Report.new
  @held_results = HeldResults.new
  @quiet = quiet
  @output = quiet? ? NullOutput.new : IOOutput.new(output)
  @tasks = []
end

Instance Attribute Details

#full_reportReport (readonly)

Returns the full report of all measurements in the job.

Returns:

  • (Report)

    the full report of all measurements in the job.



32
33
34
# File 'lib/benchmark/memory/job.rb', line 32

def full_report
  @full_report
end

#tasksArray<Task> (readonly)

Returns the measurement tasks to run.

Returns:

  • (Array<Task>)

    the measurement tasks to run.



35
36
37
# File 'lib/benchmark/memory/job.rb', line 35

def tasks
  @tasks
end

Instance Method Details

#compare!(**spec) ⇒ void

This method returns an undefined value.

Enable output of a comparison of the different tasks.



49
50
51
# File 'lib/benchmark/memory/job.rb', line 49

def compare!(**spec)
  @comparator = full_report.comparator = Report::Comparator.from_spec(spec.to_h)
end

#compare?Boolean

Check whether the job should do a comparison.

Returns:

  • (Boolean)


42
43
44
# File 'lib/benchmark/memory/job.rb', line 42

def compare?
  !!@comparator
end

#hold!(held_path) ⇒ void

This method returns an undefined value.

Enable holding results to compare between separate runs.

Parameters:

  • held_path (String, IO)

    The location to save the held results.



58
59
60
# File 'lib/benchmark/memory/job.rb', line 58

def hold!(held_path)
  @held_results.path = held_path
end

#quiet?Boolean

Check whether the job is set to quiet.

Returns:

  • (Boolean)


118
119
120
# File 'lib/benchmark/memory/job.rb', line 118

def quiet?
  @quiet
end

#report(label = '', &block) ⇒ Object

Add a measurement entry to the job to measure the specified block.

Parameters:

  • label (String) (defaults to: '')

    The label for the measured code.

  • block (Proc)

    Code the measure.

Raises:

  • (ArgumentError)

    if no code block is specified.



68
69
70
71
72
# File 'lib/benchmark/memory/job.rb', line 68

def report(label = '', &block)
  raise ArgumentError, 'You did not specify a block for the item' unless block_given?

  tasks.push Task.new(label, block)
end

#runReport

Run the job and outputs its full report.

Returns:



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/benchmark/memory/job.rb', line 77

def run
  @output.put_header
  @held_results.load

  tasks.each do |task|
    held = run_task(task)

    if held
      @output.put_hold_notice
      break
    end
  end

  full_report
end

#run_comparisonvoid

This method returns an undefined value.

Run a comparison of the entries and puts it on the output.



109
110
111
112
113
# File 'lib/benchmark/memory/job.rb', line 109

def run_comparison
  return unless compare? && full_report.comparable?

  @output.put_comparison(full_report.comparison)
end

#run_task(task) ⇒ Boolean

Run a task.

Parameters:

Returns:

  • (Boolean)

    A flag indicating whether to hold or not.



98
99
100
101
102
103
104
# File 'lib/benchmark/memory/job.rb', line 98

def run_task(task)
  if @held_results.include?(task)
    run_with_held_results(task)
  else
    run_without_held_results(task)
  end
end