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.



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/morpheus/benchmarking.rb', line 169

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.



167
168
169
# File 'lib/morpheus/benchmarking.rb', line 167

def command
  @command
end

#end_timeObject (readonly)

Returns the value of attribute end_time.



167
168
169
# File 'lib/morpheus/benchmarking.rb', line 167

def end_time
  @end_time
end

#errorObject (readonly)

Returns the value of attribute error.



167
168
169
# File 'lib/morpheus/benchmarking.rb', line 167

def error
  @error
end

#exit_codeObject (readonly)

Returns the value of attribute exit_code.



167
168
169
# File 'lib/morpheus/benchmarking.rb', line 167

def exit_code
  @exit_code
end

#idObject (readonly)

Returns the value of attribute id.



167
168
169
# File 'lib/morpheus/benchmarking.rb', line 167

def id
  @id
end

#nameObject (readonly)

Returns the value of attribute name.



167
168
169
# File 'lib/morpheus/benchmarking.rb', line 167

def name
  @name
end

#start_timeObject (readonly)

Returns the value of attribute start_time.



167
168
169
# File 'lib/morpheus/benchmarking.rb', line 167

def start_time
  @start_time
end

Instance Method Details

#durationObject



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

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



211
212
213
214
215
216
217
218
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
# File 'lib/morpheus/benchmarking.rb', line 211

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



185
186
187
188
189
190
# File 'lib/morpheus/benchmarking.rb', line 185

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

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



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

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



263
264
265
# File 'lib/morpheus/benchmarking.rb', line 263

def to_s
  msg
end