Module: Benchmark

Defined in:
lib/benchmark/plot.rb,
lib/benchmark/plot/plotter.rb,
lib/benchmark/plot/version.rb,
lib/benchmark/plot/report_maker.rb

Defined Under Namespace

Modules: Plot

Class Method Summary collapse

Class Method Details

.plot(test_data, opts = {}) {|reporter| ... } ⇒ Object

Create a plot of the benchmarked code over a varied number of inputs.

Examples:

Benchmark map{}.flatten vs. flat_map

require 'benchmark/plot'
class TestArray
   attr_reader :arr

   def initialize arr
     @arr = arr
   end

   # The to_s method is called for populating the X axis labels.
   def to_s
     @arr.size.to_s
   end
 end

 test_data = [5, 25, 50, 75, 100, 125, 150, 175, 200,250,300]
 test_data.map! {|e| TestArray.new(Array.new(e) {|i| i}) }

 Benchmark.plot(test_data) do |x|
   x.report("map.flatten") do |data|
      # Here `data` will be of an object of type `TestArray`.
     data.arr.map { [nil]  }.flatten
   end

  x.report("flat_map") do |data|
    data.arr.flat_map { [nil] }
  end
end

Parameters:

  • test_data (Enumerable)

    An object containing the data that is to be used for the benchmarking. This object must be Enumerable, i.e. it must have the ‘#each` method defined. Each of the constituent objects of `test_data` should have a `#to_s` method defined, which will be used for populating the X axis labels.

  • opts (Hash) (defaults to: {})

    The options for configuring the graph.

Options Hash (opts):

  • :title (Symbol, String) — default: :Benchmarks

    Title of the graph.

  • :file_name (Symbol, String) — default: :benchmark_plot_graph

    Name of file to which plot is written.

  • :time (Symbol) — default: :real

    The kind of time that should be displayed on the graph. Can be ‘:real` for elapsed real time, `:stime` for System CPU time, `:utime` for User CPU time, `:cstime` for System CPU time of children, `:cutime` for User CPU time of children, and `:total` for the total time that is `:utime + :stime + :cutime + :cstime`.

  • :x_labels (TrueClass, FalseClass) — default: true

    Whether you want X-axis labels or not.

  • :x_axis_label (String, Symbol) — default: nil

    X-axis label string.

Yields:

  • (reporter)

56
57
58
59
60
61
62
63
# File 'lib/benchmark/plot.rb', line 56

def self.plot test_data, opts={}, &block
  include Benchmark::Plot

  reporter = ReportMaker.new(test_data)
  yield(reporter)
  plotter = Plotter.new reporter, test_data, opts
  plotter.plot
end