Module: Morpheus::Benchmarking

Defined in:
lib/morpheus/benchmarking.rb

Overview

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()

Defined Under Namespace

Modules: HasBenchmarking Classes: BenchmarkRecord

Constant Summary collapse

@@enabled =

a global toggle switch for benchmarking

false
@@benchmark_record_list =

internal Array to store benchmark records for recording todo: garbage cleanup, roll these off to disk probably.

[]
@@benchmark_id_store =

internal Hash to lookup benchmark records by id

{}
@@benchmark_name_store =

internal Hash to lookup benchmark records by name

{}

Class Method Summary collapse

Class Method Details

.benchmark_id_storeObject



36
37
38
# File 'lib/morpheus/benchmarking.rb', line 36

def self.benchmark_id_store
  @@benchmark_id_store
end

.benchmark_name_storeObject



42
43
44
# File 'lib/morpheus/benchmarking.rb', line 42

def self.benchmark_name_store
  @@benchmark_name_store
end

.benchmark_record_listObject



30
31
32
# File 'lib/morpheus/benchmarking.rb', line 30

def self.benchmark_record_list
  @@benchmark_record_list
end

.enabledObject



19
20
21
# File 'lib/morpheus/benchmarking.rb', line 19

def self.enabled
  @@enabled
end

.enabled=(val) ⇒ Object



23
24
25
# File 'lib/morpheus/benchmarking.rb', line 23

def self.enabled=(val)
  @@enabled = !!val
end

.enabled?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/morpheus/benchmarking.rb', line 15

def self.enabled?
  @@enabled  
end

.lastObject

get last benchmark started. useful if the name, so ‘benchmark stop` can work use a unique name or else your record may be overwritten!



106
107
108
# File 'lib/morpheus/benchmarking.rb', line 106

def self.last()
  (@@benchmark_record_list || []).last
end

.lookup(opts = {}) ⇒ Object

lookup a BenchmarkRecord identified by name or options, usually just name.



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# 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(opts = {}) ⇒ Object

start a new BenchmarkRecord Examples:

Morpheus::Benchmarking.start()
Morpheus::Benchmarking.start("my routine")


54
55
56
57
58
59
60
61
62
63
64
65
66
# 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(opts, exit_code = 0, error = nil) ⇒ Object

stop a BenchmarkRecord identified by name or options maybe: if opts is nil, the last record is returned



72
73
74
75
76
77
78
79
80
# 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