Class: Perf::MeterFactory
- Inherits:
-
Object
- Object
- Perf::MeterFactory
- Includes:
- Singleton
- Defined in:
- lib/perf/meter_factory.rb
Overview
Very simple Perf::Meter factory and singleton management.
Useful to not have to pass around Perf::Meter objects and still be able to generate stats in various parts of the code. For complex situations where you have multiple Perf::Meter objects you might need to consider either creating a factory that fulfills your needs, or create Perf::Meter objects and pass them around or use this factory will well planned out key values so that you won’t have conflicts and overrides.
Example of usage where it would be inconvenient to pass around the Perf::Meter object from example to function2:
def example
Perf::MeterFactory.instance.get.measure(:function1)
function1()
end
end
def function1()
..
function2()
..
end
def function2()
..
Perf::MeterFactory.instance.get.measure(:some_hot_code_in_function2)
...
end
..
end
Constant Summary collapse
- DEFAULT_METER =
:default
Instance Method Summary collapse
-
#all ⇒ Object
Returns a hash of existing meters.
-
#clear_all! ⇒ Object
Clears the entire cache of meters.
-
#clear_factory_options! ⇒ Object
Clear factory options.
-
#clear_meter(key = DEFAULT_METER) ⇒ Object
Removes an existing meter from the cache.
-
#exists?(key = DEFAULT_METER) ⇒ Boolean
If you use set_new_meters_options, or if you pass options to Perf::MeterFactory.get, you are setting options only for if the meter is created.
-
#get(key = DEFAULT_METER, new_meter_options = nil) ⇒ Object
Returns a Perf::Meter with a given key, and creates it lazily if it doesn’t exist’.
-
#initialize ⇒ MeterFactory
constructor
A new instance of MeterFactory.
-
#meter(key = DEFAULT_METER) ⇒ Object
meter is like get, but if the meter doesn’t already exists it returns a NoOpMeter.
-
#no_op_instance ⇒ Object
Used by ProductionMeterFactory to return the instance ensuring that no Perf::Meters will be created if they do not exist.
-
#set_default(meter) ⇒ Object
Sets the default meter.
-
#set_factory_options(options) ⇒ Object
Set options for the factory behaviour.
-
#set_meter(key, meter) ⇒ Object
Pushes a Perf::Meter into a key.
-
#set_new_meters_options(options) ⇒ Object
To set options for new meters created by get, when specific options are not passed, you can do so with this method.
Constructor Details
#initialize ⇒ MeterFactory
Returns a new instance of MeterFactory.
41 42 43 44 45 |
# File 'lib/perf/meter_factory.rb', line 41 def initialize @perf_meters = {} @new_meter_options = {} @factory_options = {:noop=>false} end |
Instance Method Details
#all ⇒ Object
Returns a hash of existing meters.
109 110 111 |
# File 'lib/perf/meter_factory.rb', line 109 def all @perf_meters.dup end |
#clear_all! ⇒ Object
Clears the entire cache of meters.
121 122 123 |
# File 'lib/perf/meter_factory.rb', line 121 def clear_all! @perf_meters.clear end |
#clear_factory_options! ⇒ Object
Clear factory options.
84 85 86 |
# File 'lib/perf/meter_factory.rb', line 84 def @factory_options.clear end |
#clear_meter(key = DEFAULT_METER) ⇒ Object
Removes an existing meter from the cache
115 116 117 |
# File 'lib/perf/meter_factory.rb', line 115 def clear_meter(key=DEFAULT_METER) @perf_meters.delete(key) if @perf_meters end |
#exists?(key = DEFAULT_METER) ⇒ Boolean
If you use set_new_meters_options, or if you pass options to Perf::MeterFactory.get, you are setting options only for if the meter is created. For this reason you might need to find out if the meter already exist.
91 92 93 |
# File 'lib/perf/meter_factory.rb', line 91 def exists?(key=DEFAULT_METER) !@perf_meters[key].nil? end |
#get(key = DEFAULT_METER, new_meter_options = nil) ⇒ Object
Returns a Perf::Meter with a given key, and creates it lazily if it doesn’t exist’. NOTE: The options are set ONLY the first time that get is called on a specific key.
After that the will be ignored!
51 52 53 54 55 56 57 58 59 60 |
# File 'lib/perf/meter_factory.rb', line 51 def get(key=DEFAULT_METER,=nil) if !@factory_options[:noop] # Creates a real meter @perf_meters[key] ||= Perf::Meter.new( || @new_meter_options) else # If noop is set, creates a no-nop version of the meter, unless a meter with this key has already been # created. @perf_meters[key] ||= Perf::NoOpMeter.new end end |
#meter(key = DEFAULT_METER) ⇒ Object
meter is like get, but if the meter doesn’t already exists it returns a NoOpMeter. You can use this every time that you want “somebody else” make the decision of what meter to use.
65 66 67 |
# File 'lib/perf/meter_factory.rb', line 65 def meter(key=DEFAULT_METER) @perf_meters[key] ||= Perf::NoOpMeter.new end |
#no_op_instance ⇒ Object
Used by ProductionMeterFactory to return the instance ensuring that no Perf::Meters will be created if they do not exist.
128 129 130 131 |
# File 'lib/perf/meter_factory.rb', line 128 def no_op_instance @factory_options[:noop] = true self end |
#set_default(meter) ⇒ Object
Sets the default meter.
103 104 105 |
# File 'lib/perf/meter_factory.rb', line 103 def set_default(meter) set_meter(DEFAULT_METER,meter) end |
#set_factory_options(options) ⇒ Object
Set options for the factory behaviour.
78 79 80 |
# File 'lib/perf/meter_factory.rb', line 78 def () @factory_options.merge!() end |
#set_meter(key, meter) ⇒ Object
Pushes a Perf::Meter into a key
97 98 99 |
# File 'lib/perf/meter_factory.rb', line 97 def set_meter(key,meter) @perf_meters[key]=meter end |
#set_new_meters_options(options) ⇒ Object
To set options for new meters created by get, when specific options are not passed, you can do so with this method.
72 73 74 |
# File 'lib/perf/meter_factory.rb', line 72 def () @new_meter_options.merge() end |