Class: Morpheus::Benchmarking::BenchmarkRecord

Inherits:
Object
  • Object
show all
Defined in:
lib/morpheus/benchmarking.rb

Overview

An internal class for modeling benchmark info on a single run. Examples:

BenchmarkRecord.new()
BenchmarkRecord.new("my routine")
BenchmarkRecord.new({name:"my routine"})

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ BenchmarkRecord

Returns a new instance of BenchmarkRecord.



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/morpheus/benchmarking.rb', line 177

def initialize(opts={})
  # no info is fine, anonymous benchmark is cool
  if opts.nil? || opts.empty?
    opts = {}
  end
  # support String
  opts = opts.is_a?(Hash) ? opts : {name: opts.to_s}
  @id = opts[:id] || self.object_id
  @name = opts[:name]
  #@command = opts[:command]
  # store the list of commands would be cool... to record adhoc scripts
  # @commands = []
  # @commands << @command if @command
  start()
end

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



175
176
177
# File 'lib/morpheus/benchmarking.rb', line 175

def command
  @command
end

#end_timeObject (readonly)

Returns the value of attribute end_time.



175
176
177
# File 'lib/morpheus/benchmarking.rb', line 175

def end_time
  @end_time
end

#errorObject (readonly)

Returns the value of attribute error.



175
176
177
# File 'lib/morpheus/benchmarking.rb', line 175

def error
  @error
end

#exit_codeObject (readonly)

Returns the value of attribute exit_code.



175
176
177
# File 'lib/morpheus/benchmarking.rb', line 175

def exit_code
  @exit_code
end

#idObject (readonly)

Returns the value of attribute id.



175
176
177
# File 'lib/morpheus/benchmarking.rb', line 175

def id
  @id
end

#nameObject (readonly)

Returns the value of attribute name.



175
176
177
# File 'lib/morpheus/benchmarking.rb', line 175

def name
  @name
end

#start_timeObject (readonly)

Returns the value of attribute start_time.



175
176
177
# File 'lib/morpheus/benchmarking.rb', line 175

def start_time
  @start_time
end

Instance Method Details

#durationObject



209
210
211
212
213
214
215
216
217
# File 'lib/morpheus/benchmarking.rb', line 209

def duration
  if @start_time && @end_time
    return @end_time - @start_time
  elsif @start_time
    return Time.now - @start_time
  else
    return 0
  end
end

#msgObject



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/morpheus/benchmarking.rb', line 219

def msg
  time_str = ""
  seconds = self.duration
  if seconds > 0.002
    seconds = seconds.round(3)
  else
    #seconds = seconds.round(3)
  end
  duration_str = duration
  if @start_time && @end_time
    time_str = "#{seconds} seconds"
  elsif @start_time
    time_str = "#{seconds} seconds (running)"
  else
    time_str = "(unstarted)"
  end
  command_str = "#{@name}" # or "#{@name || @id}"
  exit_str = "#{@exit_code}"
  error_str = "#{@error}" # should inspect and format this
  out = ""
  
  if @end_time
    out << "#{command_str.ljust(30, ' ')}"
  else
    out << "#{command_str.ljust(30, ' ')}"
  end

  # if @end_time
  #   out << "finished: #{command_str.ljust(30, ' ')}"
  # else
  #   out << "running: #{command_str.ljust(30, ' ')}"
  # end
  
  #out = "benchmark: #{command_str.ljust(22, ' ')}   time: #{time_str.ljust(9, ' ')}   exit: #{exit_str.ljust(2, ' ')}"
  # out = "benchmark: #{command_str.ljust(27, ' ')}   time: #{time_str.ljust(9, ' ')}   exit: #{exit_str.ljust(2, ' ')}"
  #out = "time: #{time_str.ljust(9, ' ')}   exit: #{exit_str.ljust(2, ' ')}   exec: #{command_str}"
  # how about a command you can copy and paste?
  # out = "time: #{time_str.ljust(9, ' ')}   exit: #{exit_str.ljust(2, ' ')}   #{command_str}"
  # out = "time: #{time_str.ljust(9, ' ')}   exit: #{exit_str.ljust(4, ' ')}   benchmark exec '#{command_str}'"
  if @end_time || @exit_code
    out << "\texit: #{exit_str.ljust(2, ' ')}"
  end
  if @end_time && @exit_code != 0 && @error
    out << "\terror: #{error_str.ljust(12, ' ')}"
  end

  out << "\t#{time_str.ljust(9, ' ')}"
  

  return out
end

#startObject



193
194
195
196
197
198
# File 'lib/morpheus/benchmarking.rb', line 193

def start()
  if !@start_time
    @start_time = Time.now
  end
  return self
end

#stop(exit_code = 0, error = nil) ⇒ Object



200
201
202
203
204
205
206
207
# File 'lib/morpheus/benchmarking.rb', line 200

def stop(exit_code=0, error=nil)
  if !@end_time
    @end_time = Time.now
    @exit_code = exit_code
    @error = error
  end
  return self
end

#to_sObject



271
272
273
# File 'lib/morpheus/benchmarking.rb', line 271

def to_s
  msg
end