Class: TimeBandits::TimeConsumers::BaseConsumer

Inherits:
Object
  • Object
show all
Defined in:
lib/time_bandits/time_consumers/base_consumer.rb

Direct Known Subclasses

Beetle, Dalli, Database, Memcache, Memcached, Redis, Sequel

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBaseConsumer

Returns a new instance of BaseConsumer.



40
41
42
43
# File 'lib/time_bandits/time_consumers/base_consumer.rb', line 40

def initialize
  @counters = self.class.struct.new
  reset
end

Class Attribute Details

.metrics_prefixObject (readonly)

Returns the value of attribute metrics_prefix.



33
34
35
# File 'lib/time_bandits/time_consumers/base_consumer.rb', line 33

def metrics_prefix
  @metrics_prefix
end

.runtime_formatObject (readonly)

Returns the value of attribute runtime_format.



33
34
35
# File 'lib/time_bandits/time_consumers/base_consumer.rb', line 33

def runtime_format
  @runtime_format
end

.runtime_keysObject (readonly)

Returns the value of attribute runtime_keys.



33
34
35
# File 'lib/time_bandits/time_consumers/base_consumer.rb', line 33

def runtime_keys
  @runtime_keys
end

.structObject (readonly)

Returns the value of attribute struct.



33
34
35
# File 'lib/time_bandits/time_consumers/base_consumer.rb', line 33

def struct
  @struct
end

.timer_nameObject (readonly)

Returns the value of attribute timer_name.



33
34
35
# File 'lib/time_bandits/time_consumers/base_consumer.rb', line 33

def timer_name
  @timer_name
end

Class Method Details

.fields(*symbols) ⇒ Object

first symbol is used as time measurement



18
19
20
21
22
23
24
25
26
# File 'lib/time_bandits/time_consumers/base_consumer.rb', line 18

def fields(*symbols)
  @struct = Struct.new(*(symbols.map{|s| "#{@metrics_prefix}_#{s}".to_sym}))
  symbols.each do |name|
    class_eval(<<-"EVA", __FILE__, __LINE__ + 1)
      def #{name}; @counters.#{@metrics_prefix}_#{name}; end
      def #{name}=(v); @counters.#{@metrics_prefix}_#{name} = v; end
    EVA
  end
end

.format(f, *keys) ⇒ Object



28
29
30
31
# File 'lib/time_bandits/time_consumers/base_consumer.rb', line 28

def format(f, *keys)
  @runtime_format = f
  @runtime_keys = keys.map{|s| "#{@metrics_prefix}_#{s}".to_sym}
end

.instanceObject



4
5
6
7
# File 'lib/time_bandits/time_consumers/base_consumer.rb', line 4

def instance
  Thread.current.thread_variable_get(key) ||
    Thread.current.thread_variable_set(key, new)
end

.keyObject



9
10
11
# File 'lib/time_bandits/time_consumers/base_consumer.rb', line 9

def key
  @key ||= name.to_sym
end

.method_missing(m, *args) ⇒ Object



35
36
37
# File 'lib/time_bandits/time_consumers/base_consumer.rb', line 35

def method_missing(m, *args)
  (i = instance).respond_to?(m) ? i.send(m,*args) : super
end

.prefix(sym) ⇒ Object



13
14
15
# File 'lib/time_bandits/time_consumers/base_consumer.rb', line 13

def prefix(sym)
  @metrics_prefix = sym
end

Instance Method Details

#consumedObject Also known as: current_runtime



53
54
55
# File 'lib/time_bandits/time_consumers/base_consumer.rb', line 53

def consumed
  @counters[0]
end

#metricsObject



49
50
51
# File 'lib/time_bandits/time_consumers/base_consumer.rb', line 49

def metrics
  @counters.members.each_with_object({}){|m,h| h[m] = @counters.send(m)}
end

#resetObject



45
46
47
# File 'lib/time_bandits/time_consumers/base_consumer.rb', line 45

def reset
  @counters.length.times{|i| @counters[i] = 0}
end

#runtimeObject



59
60
61
62
63
64
65
66
# File 'lib/time_bandits/time_consumers/base_consumer.rb', line 59

def runtime
  values = metrics.values_at(*self.class.runtime_keys)
  if values.all?{|v|v==0}
    ""
  else
    self.class.runtime_format % values
  end
end