module Morpheus::Benchmarking
Provides global Benchmarking
functionality This provides a store of benchmarking records which can be looked up by name. There is also a global enabled flag that can be used. There is a mixin HasBenchmarking
which provides start_benchmark(), stop_benchmark() and with_benchmark()
Public Class Methods
# File lib/morpheus/benchmarking.rb, line 36 def self.benchmark_id_store @@benchmark_id_store end
# File lib/morpheus/benchmarking.rb, line 42 def self.benchmark_name_store @@benchmark_name_store end
# File lib/morpheus/benchmarking.rb, line 30 def self.benchmark_record_list @@benchmark_record_list end
# File lib/morpheus/benchmarking.rb, line 19 def self.enabled @@enabled end
# File lib/morpheus/benchmarking.rb, line 23 def self.enabled=(val) @@enabled = !!val end
# File lib/morpheus/benchmarking.rb, line 15 def self.enabled? @@enabled end
get last benchmark started. useful if the name, so ‘benchmark stop` can work use a unique name or else your record may be overwritten!
# File lib/morpheus/benchmarking.rb, line 106 def self.last() (@@benchmark_record_list || []).last end
lookup a BenchmarkRecord
identified by name or options, usually just name. @params cmd [String or Hash] Name like “my routine” or with a Hash
like {id: ID} @returns BenchmarkRecord
that looks like {id: ID, name:“my routine”,start_time:Time}
# File lib/morpheus/benchmarking.rb, line 85 def self.lookup(opts={}) benchmark_record = nil if opts.nil? || opts.empty? benchmark_record = nil elsif opts.is_a?(Hash) if opts[:id] benchmark_record = benchmark_name_store[opts[:id].to_s] elsif opts[:name] benchmark_record = benchmark_id_store[opts[:name].to_s] end elsif opts.is_a?(String) || opts.is_a?(Symbol) benchmark_record = benchmark_name_store[opts.to_s] || benchmark_id_store[opts.to_s] else Morpheus::Logging::DarkPrinter.puts "Benchmarking lookup passed a bad lookup argument: #{opts}" if Morpheus::Logging.debug? end # could to slow traversal of benchmark_record_list here.. return benchmark_record end
start a new BenchmarkRecord
@params opts [String or Hash] String
as name like like “my routine” or with a Hash
like {name: “my routine”}
Just a String for name is fine because there are no other settings of interest at the moment. Optional, a test can be created without a name. A random :id will be available in the response.
Examples:
Morpheus::Benchmarking.start() Morpheus::Benchmarking.start("my routine")
@returns BenchmarkRecord
that looks like {id: ID, name:“my routine”,start_time:Time}
# File lib/morpheus/benchmarking.rb, line 54 def self.start(opts={}) benchmark_record = BenchmarkRecord.new(opts) benchmark_record_list << benchmark_record # index name and id if benchmark_record.name benchmark_name_store[benchmark_record.name.to_s] = benchmark_record end if benchmark_record.id benchmark_id_store[benchmark_record.id.to_s] = benchmark_record end #benchmark_record.start() # initialize does it return benchmark_record end
stop a BenchmarkRecord
identified by name or options maybe: if opts is nil, the last record is returned @params opts [String or Hash] String
as name like like “my routine” or with a Hash
like {name: “my routine”} or {id:ID} @returns BenchmarkRecord
that looks like {id: ID, name:“my routine”,start_time:Time}
# File lib/morpheus/benchmarking.rb, line 72 def self.stop(opts, exit_code=0, error=nil) benchmark_record = self.lookup(opts) if benchmark_record benchmark_record.stop(exit_code, error) return benchmark_record else return nil end end