Class: Benchmark::BigO::Job

Inherits:
IPS::Job
  • Object
show all
Includes:
Chartkick::Helper
Defined in:
lib/benchmark/bigo/job.rb

Defined Under Namespace

Classes: Entry

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Job

Returns a new instance of Job.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# 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

Instance Attribute Details

#min_sizeObject

Returns the value of attribute min_size.



40
41
42
# File 'lib/benchmark/bigo/job.rb', line 40

def min_size
  @min_size
end

#step_sizeObject

Returns the value of attribute step_size.



40
41
42
# File 'lib/benchmark/bigo/job.rb', line 40

def step_size
  @step_size
end

#stepsObject

Returns the value of attribute steps.



40
41
42
# File 'lib/benchmark/bigo/job.rb', line 40

def steps
  @steps
end

Instance Method Details

#chart!(filename = 'chart.html') ⇒ Object



85
86
87
# File 'lib/benchmark/bigo/job.rb', line 85

def chart! filename='chart.html'
  @chart_file = filename
end

#compare?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/benchmark/bigo/job.rb', line 89

def compare?
  @compare
end

#config(opts) ⇒ Object



78
79
80
81
82
83
# 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') ⇒ Object



101
102
103
# File 'lib/benchmark/bigo/job.rb', line 101

def csv! filename='data.csv'
  @csv_file = filename
end

#generate(sym) ⇒ Object

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



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# 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_chartObject



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# 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_csvObject



191
192
193
194
195
196
197
198
199
200
201
202
203
204
# 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_jsonObject



181
182
183
184
185
186
187
188
189
# 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_outputObject



174
175
176
177
178
179
# File 'lib/benchmark/bigo/job.rb', line 174

def generate_output
  generate_json
  generate_csv
  generate_chart
  generate_termplot
end

#generate_termplotObject



222
223
224
225
226
227
# 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) ⇒ Object

Raises:

  • (ArgumentError)


105
106
107
108
109
# File 'lib/benchmark/bigo/job.rb', line 105

def generator &blk
  @generator = blk

  raise ArgumentError, "no block" unless @generator
end

#item(label = "", str = nil, &blk) ⇒ Object Also known as: report

:yield:

Raises:

  • (ArgumentError)


155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# 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

#json!(filename = 'data.json') ⇒ Object



97
98
99
# File 'lib/benchmark/bigo/job.rb', line 97

def json! filename='data.json'
  @json_file = filename
end

#max_sizeObject



73
74
75
76
# File 'lib/benchmark/bigo/job.rb', line 73

def max_size
  @min_size + (@step_size * (@steps-1))
  # should also equal step(@steps-1)
end

#sizesObject



147
148
149
150
151
152
153
# File 'lib/benchmark/bigo/job.rb', line 147

def sizes
  @sizes ||=
  (0...@steps).collect do |n|
    step n
  end
  @sizes
end

#step(n) ⇒ Object

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



143
144
145
# File 'lib/benchmark/bigo/job.rb', line 143

def step n
  @min_size + (n * @step_size)
end

#termplot!Object



93
94
95
# File 'lib/benchmark/bigo/job.rb', line 93

def termplot!
  @term_plot = true
end