Skip to content

Commit

Permalink
More documentation for gem and some more features like added options …
Browse files Browse the repository at this point in the history
…for plotting.
  • Loading branch information
v0dro committed Jun 28, 2016
1 parent 8743b0d commit 2cba8dc
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 5 deletions.
5 changes: 2 additions & 3 deletions benchmark-plot.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ require 'benchmark/plot/version.rb'
Benchmark::Plot::DESCRIPTION = <<MSG
benchmark-plot is an extension to the Ruby standard benchmarking library.
It let's you easily create plots in the form of PNG images of any code that you
want to benchmark over a varied number of inputs. It also supports comparative
benchmarking.
It let's you easily create plots of any code that you want to benchmark over
a varied number of inputs. It also supports comparative benchmarking.
MSG

Gem::Specification.new do |spec|
Expand Down
47 changes: 47 additions & 0 deletions lib/benchmark/plot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,53 @@
require 'benchmark/plot/version'

module Benchmark
# Create a plot of the benchmarked code over a varied number of inputs.
#
# @param 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.
# @param [Hash] opts The options for configuring the graph.
# @option opts [Symbol, String] :title (:Benchmarks) Title of the graph.
# @option opts [Symbol, String] :file_name (:benchmark_plot_graph) Name of
# file to which plot is written.
# @option opts [Symbol] :time (: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`.
# @option opts [TrueClass, FalseClass] :x_labels (true) Whether you want X-axis
# labels or not.
# @option opts [String, Symbol] :x_axis_label (nil) X-axis label string.
# @example 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
def self.plot test_data, opts={}, &block
include Benchmark::Plot

Expand Down
4 changes: 3 additions & 1 deletion lib/benchmark/plot/plotter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ def initialize reporter, test_data, opts
@title = opts[:title] || :Benchmarks
@time = opts[:time] || :real
@x_labels = opts[:x_labels] || true
@x_axis_label = opts[:x_axis_label]
@test_data = test_data
end

Expand All @@ -22,8 +23,9 @@ def plot
plot.data label, time_data
end
positions = Array.new(@test_data.size) { |i| i }
puts positions.zip(@test_data.map(&:to_s)).to_h
plot.labels = positions.zip(@test_data.map(&:to_s)).to_h if @x_labels
plot.x_axis_label = @x_axis_label if @x_axis_label
plot.y_axis_label = 'Seconds'
plot.write("#{@file_name}.png")
end

Expand Down
2 changes: 1 addition & 1 deletion lib/benchmark/plot/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Benchmark
module Plot
VERSION = "0.1"
VERSION = "0.1.1"
end
end

0 comments on commit 2cba8dc

Please sign in to comment.