Skip to content

Commit 2cba8dc

Browse files
committed
More documentation for gem and some more features like added options for plotting.
1 parent 8743b0d commit 2cba8dc

File tree

4 files changed

+53
-5
lines changed

4 files changed

+53
-5
lines changed

benchmark-plot.gemspec

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@ require 'benchmark/plot/version.rb'
66
Benchmark::Plot::DESCRIPTION = <<MSG
77
benchmark-plot is an extension to the Ruby standard benchmarking library.
88
9-
It let's you easily create plots in the form of PNG images of any code that you
10-
want to benchmark over a varied number of inputs. It also supports comparative
11-
benchmarking.
9+
It let's you easily create plots of any code that you want to benchmark over
10+
a varied number of inputs. It also supports comparative benchmarking.
1211
MSG
1312

1413
Gem::Specification.new do |spec|

lib/benchmark/plot.rb

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,53 @@
66
require 'benchmark/plot/version'
77

88
module Benchmark
9+
# Create a plot of the benchmarked code over a varied number of inputs.
10+
#
11+
# @param test_data [Enumerable] An object containing the data that is to be
12+
# used for the benchmarking. This object must be Enumerable, i.e. it must
13+
# have the `#each` method defined. Each of the constituent objects of
14+
# `test_data` should have a `#to_s` method defined, which will be used for
15+
# populating the X axis labels.
16+
# @param [Hash] opts The options for configuring the graph.
17+
# @option opts [Symbol, String] :title (:Benchmarks) Title of the graph.
18+
# @option opts [Symbol, String] :file_name (:benchmark_plot_graph) Name of
19+
# file to which plot is written.
20+
# @option opts [Symbol] :time (:real) The kind of time that should be displayed
21+
# on the graph. Can be `:real` for elapsed real time, `:stime` for System
22+
# CPU time, `:utime` for User CPU time, `:cstime` for System CPU time of children,
23+
# `:cutime` for User CPU time of children, and `:total` for the total time
24+
# that is `:utime + :stime + :cutime + :cstime`.
25+
# @option opts [TrueClass, FalseClass] :x_labels (true) Whether you want X-axis
26+
# labels or not.
27+
# @option opts [String, Symbol] :x_axis_label (nil) X-axis label string.
28+
# @example Benchmark map{}.flatten vs. flat_map
29+
# require 'benchmark/plot'
30+
# class TestArray
31+
# attr_reader :arr
32+
#
33+
# def initialize arr
34+
# @arr = arr
35+
# end
36+
#
37+
# # The to_s method is called for populating the X axis labels.
38+
# def to_s
39+
# @arr.size.to_s
40+
# end
41+
# end
42+
#
43+
# test_data = [5, 25, 50, 75, 100, 125, 150, 175, 200,250,300]
44+
# test_data.map! {|e| TestArray.new(Array.new(e) {|i| i}) }
45+
#
46+
# Benchmark.plot(test_data) do |x|
47+
# x.report("map.flatten") do |data|
48+
# # Here `data` will be of an object of type `TestArray`.
49+
# data.arr.map { [nil] }.flatten
50+
# end
51+
#
52+
# x.report("flat_map") do |data|
53+
# data.arr.flat_map { [nil] }
54+
# end
55+
# end
956
def self.plot test_data, opts={}, &block
1057
include Benchmark::Plot
1158

lib/benchmark/plot/plotter.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ def initialize reporter, test_data, opts
77
@title = opts[:title] || :Benchmarks
88
@time = opts[:time] || :real
99
@x_labels = opts[:x_labels] || true
10+
@x_axis_label = opts[:x_axis_label]
1011
@test_data = test_data
1112
end
1213

@@ -22,8 +23,9 @@ def plot
2223
plot.data label, time_data
2324
end
2425
positions = Array.new(@test_data.size) { |i| i }
25-
puts positions.zip(@test_data.map(&:to_s)).to_h
2626
plot.labels = positions.zip(@test_data.map(&:to_s)).to_h if @x_labels
27+
plot.x_axis_label = @x_axis_label if @x_axis_label
28+
plot.y_axis_label = 'Seconds'
2729
plot.write("#{@file_name}.png")
2830
end
2931

lib/benchmark/plot/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module Benchmark
22
module Plot
3-
VERSION = "0.1"
3+
VERSION = "0.1.1"
44
end
55
end

0 commit comments

Comments
 (0)