class Benchmark::BigO::Job

Attributes

min_size[RW]
step_size[RW]
steps[RW]

Public Class Methods

new(opts={}) click to toggle source
Calls superclass method
# File lib/benchmark/bigo/job.rb, line 42
def initialize opts={}
  super

  @full_report = Report.new
  @generator = nil

  # defaults
  @min_size = 100
  @step_size = 100
  @steps = 10

  # whether to generate a chart of the results
  # if nil, do not generate chart
  # else string is name of file to write chart out to
  @chart_file = nil

  # whether to generate json output of the results
  # if nil, do not generate data
  # else string is name of file to write data out to
  @json_file = nil

  # whether to generate csv output of the results
  # if nil, do not generate data
  # else string is name of file to write data out to
  @csv_file = nil

  # whether to generate a plot in the terminal
  # using gnuplot
  @term_plot = false
end

Public Instance Methods

chart!(filename='chart.html') click to toggle source
# File lib/benchmark/bigo/job.rb, line 85
def chart! filename='chart.html'
  @chart_file = filename
end
compare?() click to toggle source
# File lib/benchmark/bigo/job.rb, line 89
def compare?
  @compare
end
config(opts) click to toggle source
Calls superclass method
# File lib/benchmark/bigo/job.rb, line 78
def config opts
  super
  @min_size = opts[:min_size] if opts[:min_size]
  @steps = opts[:steps] if opts[:steps]
  @step_size = opts[:step_size] if opts[:step_size]
end
csv!(filename='data.csv') click to toggle source
# File lib/benchmark/bigo/job.rb, line 101
def csv! filename='data.csv'
  @csv_file = filename
end
generate(sym) click to toggle source

use a generator that creates a randomized object represented by the symbol passed to the method

# File lib/benchmark/bigo/job.rb, line 114
def generate sym

  @generator =
    case sym

      # generates an Array containing shuffled integer values from 0 to size
    when :array
      Proc.new {|size| (0...size).to_a.shuffle }

      # generates a random hex string of length size
    when :string
      Proc.new {|size| SecureRandom.hex(size) }

      # simply returns the size
      # for performance benefits when handling the
      # size completely in the report block
    when :size
      Proc.new {|size| size }

      # when :hash
      # TODO: hash generator

    else
      raise "#{sym} is not a supported object type"
    end
end
generate_chart() click to toggle source
# File lib/benchmark/bigo/job.rb, line 206
def generate_chart
  return if @chart_file.nil?

  @chart = Chart.new @full_report.data, sizes

  charts = @chart.generate(compare: compare?)

  template_file = File.join File.dirname(__FILE__), 'templates/chart.erb'
  template = ERB.new(File.read(template_file))

  File.open @chart_file, 'w' do |f|
    f.write template.result(binding)
  end

end
generate_csv() click to toggle source
# File lib/benchmark/bigo/job.rb, line 191
def generate_csv
  return if @csv_file.nil?

  all_data = @full_report.data
  data_points = all_data.map{|report| report[:data].keys }.flatten.uniq

  CSV.open @csv_file, 'w' do |csv|
    header = [''] + data_points
    csv << header
    all_data.each do |row|
      csv << [row[:name]] + row[:data].values
    end
  end
end
generate_json() click to toggle source
# File lib/benchmark/bigo/job.rb, line 181
def generate_json
  return if @json_file.nil?

  all_data = @full_report.data

  File.open @json_file, 'w' do |f|
    f.write JSON.pretty_generate(all_data)
  end
end
generate_output() click to toggle source
# File lib/benchmark/bigo/job.rb, line 174
def generate_output
  generate_json
  generate_csv
  generate_chart
  generate_termplot
end
generate_termplot() click to toggle source
# File lib/benchmark/bigo/job.rb, line 222
def generate_termplot
  return unless @term_plot

  @plot = TermPlot.new @full_report.data, sizes
  @plot.generate
end
generator(&blk) click to toggle source
# File lib/benchmark/bigo/job.rb, line 105
def generator &blk
  @generator = blk

  raise ArgumentError, "no block" unless @generator
end
item(label="", str=nil) { || ... } click to toggle source
# File lib/benchmark/bigo/job.rb, line 155
def item label="", str=nil, &blk # :yield:
  if blk and str
    raise ArgumentError, "specify a block and a str, but not both"
  end

  action = str || blk
  raise ArgumentError, "no block or string" unless action

  for size in sizes
    generated = @generator.call(size)

    label_size = "#{label} #{size}"
    @list.push Entry.new(label_size, action, generated, size)
  end

  self
end
Also aliased as: report
json!(filename='data.json') click to toggle source
# File lib/benchmark/bigo/job.rb, line 97
def json! filename='data.json'
  @json_file = filename
end
max_size() click to toggle source
# File lib/benchmark/bigo/job.rb, line 73
def max_size
  @min_size + (@step_size * (@steps-1))
  # should also equal step(@steps-1)
end
report(label="", str=nil, &blk)
Alias for: item
sizes() click to toggle source
# File lib/benchmark/bigo/job.rb, line 147
def sizes
  @sizes ||=
  (0...@steps).collect do |n|
    step n
  end
  @sizes
end
step(n) click to toggle source

return the size for the nth step n = 0 returns @min_size

# File lib/benchmark/bigo/job.rb, line 143
def step n
  @min_size + (n * @step_size)
end
termplot!() click to toggle source
# File lib/benchmark/bigo/job.rb, line 93
def termplot!
  @term_plot = true
end