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
# 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

#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



253
254
255
# File 'lib/morpheus/benchmarking.rb', line 253

def to_s
  msg
end