class Benchpress::Chart

Public Class Methods

new(opts = {}) click to toggle source

format - format of the file max - maximum # of profiles to run min - minimum # of profiles to run name - name of the file step - increment rate theme - theme of the chart entities - data methods to run against

# File lib/benchpress/chart.rb, line 11
def initialize(opts = {})
  @cycles   = opts[:cycles] || 1
  @format   = opts[:format] || 'png'
  @max      = opts[:max]    || 1000
  @min      = opts[:min]    || 0
  @name     = opts[:name]   || "#{Time.now.strftime '%Y-%m-%d-%H:%M:%S'}"
  @step     = opts[:step]   || 250
  @theme    = opts[:theme]  || Gruff::Themes::THIRTYSEVEN_SIGNALS
  @entities = parse_entities opts[:entities]
end

Public Instance Methods

image_name() click to toggle source

file_name.ext

# File lib/benchpress/chart.rb, line 33
def image_name
  @name + '.' + @format
end
render(type = :line) click to toggle source

Render dispatcher. Note to refactor a tinge later.

# File lib/benchpress/chart.rb, line 23
def render(type = :line)
  create_chart_data_for(
    case type
    when :line then Gruff::Line.new
    when :bar  then Gruff::Bar.new
    end
  ).write image_name
end
step_points() click to toggle source

Calculates Step Points

(min / step) will render 0 unless min is greater than the step rate, giving the floor of the range (max / step) will render the maximum amount of times step will fit in the max wholly

Caveat - If the max % step != 0, it will not render to the max. This is intended behavior.

# File lib/benchpress/chart.rb, line 43
def step_points
  raise 'Cannot have larger step than max' if @step > @max

  @step_points ||= ((@min / @step)..(@max / @step)).reduce([]) { |steps, i| steps << @step * i }
end

Private Instance Methods

all_labels() click to toggle source

Get labels for every point

# File lib/benchpress/chart.rb, line 52
def all_labels
  step_points.each_with_index.reduce({}) { |hash, (val, index)| hash.merge!({ index => val.to_s }) }
end
create_chart_data_for(chart) click to toggle source
# File lib/benchpress/chart.rb, line 72
def create_chart_data_for(chart)
  chart.tap { |g|
    g.labels = all_labels
    g.theme = @theme
    g.x_axis_label = 'Times Run (n)'
    g.y_axis_label = 'Length (seconds)'

    @entities.each { |entity| g.data *entity.data }
  }
end
parse_entities(entities) click to toggle source

Lets you parse in entities by passing a hash in.

Benchpress::Chart.new(
  entities: {
    string: -> { 'string' }
    symbol: -> { :symbol  }
  }
)
# File lib/benchpress/chart.rb, line 64
def parse_entities(entities)
  raise 'Must have entities' unless entities

  @entities = entities.reduce([]) { |ary, (name, method)| 
    ary << Entity.new(step_points, @cycles, name => method) 
  }
end