class Morpheus::Benchmarking::BenchmarkRecord

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

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

Attributes

command[R]
end_time[R]
error[R]
exit_code[R]
id[R]
name[R]
start_time[R]

Public Class Methods

new(opts={}) click to toggle source
# 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

Public Instance Methods

duration() click to toggle source
# 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
msg() click to toggle source
# 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} s"
  elsif @start_time
    time_str = "#{seconds} s (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 = ""
  
  # show exit only if non 0
  # so it looks like:
  # command  time   [exit: 1]  [error:]
  # instances list foo         0.049 seconds
  # foo         0.001 seconds   exit: 1
  out << "#{command_str.ljust(45, ' ')}"
  out << "\t"
  out << "#{time_str.ljust(15, ' ')}" # maybe use the format time: 0.021s
  out << "\t"
  if @end_time
    if @exit_code && @exit_code != 0
      out << "\texit: #{exit_str.ljust(2, ' ')}"
    end
    if @error && @error != ""
      out << "\terror: #{error_str.ljust(12, ' ')}"
    end
  end
  #out << reset
  return out
end
start() click to toggle source
# 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) click to toggle source
# 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_s() click to toggle source
# File lib/morpheus/benchmarking.rb, line 253
def to_s
  msg
end