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.



20
21
22
23
24
25
26
27
# File 'lib/benchmark/memory/job.rb', line 20

def initialize(output: $stdout, quiet: false)
  @compare = 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.



30
31
32
# File 'lib/benchmark/memory/job.rb', line 30

def full_report
  @full_report
end

#tasksArray<Task> (readonly)

Returns the measurement tasks to run.

Returns:

  • (Array<Task>)

    the measurement tasks to run.



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

def tasks
  @tasks
end

Instance Method Details

#compare!void

This method returns an undefined value.

Enable output of a comparison of the different tasks.



47
48
49
# File 'lib/benchmark/memory/job.rb', line 47

def compare!
  @compare = true
end

#compare?Boolean

Check whether the job should do a comparison.

Returns:

  • (Boolean)


40
41
42
# File 'lib/benchmark/memory/job.rb', line 40

def compare?
  @compare
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.



56
57
58
# File 'lib/benchmark/memory/job.rb', line 56

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

#quiet?Boolean

Check whether the job is set to quiet.

Returns:

  • (Boolean)


130
131
132
# File 'lib/benchmark/memory/job.rb', line 130

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.



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

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

  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.



121
122
123
124
125
# File 'lib/benchmark/memory/job.rb', line 121

def run_comparison
  if compare? && full_report.comparable?
    @output.put_comparison(full_report.comparison)
  end
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
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/benchmark/memory/job.rb', line 98

def run_task(task)
  if @held_results.include?(task)
    measurement = @held_results[task.label]
    full_report.add_entry(task, measurement)
    return false
  else
    measurement = task.call
    entry = full_report.add_entry(task, measurement)
    @output.put_entry(entry)

    if task == tasks.last
      @held_results.cleanup
      false
    else
      @held_results.add_result(entry)
      @held_results.holding?
    end
  end
end