Module: Droid::Utilization

Extended by:
Utilization
Included in:
Utilization
Defined in:
lib/droid/utilization.rb

Constant Summary collapse

@@start =
Time.now.getutc
@@data =
{ }

Instance Method Summary collapse

Instance Method Details

#calc_utilization(topic, t2 = nil) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/droid/utilization.rb', line 39

def calc_utilization(topic, t2=nil)
  d = data(topic)
  t1 = @@start
  t2 ||= Time.now.getutc.to_i
  secs = (t2 - t1)
  secs = 1 if secs <= 0.0
  if d['msgs'] == 0
    avg = 0.0
  else
    avg = d['time'] / d['msgs']
  end
  utilization = d['time'] / secs
  {
    'avg' => avg,
    'secs' => secs,
    'utilization' => utilization,
    'msgs' => d['msgs'],
    'msgs_per_sec' => d['msgs'] / secs
  }
end

#data(topic) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/droid/utilization.rb', line 21

def data(topic)
  @@data[topic] ||= {
    'msgs' => 0,
    'time' => 0.0
  }
  @@data[topic]
end

#latencyObject



7
# File 'lib/droid/utilization.rb', line 7

def latency; @latency; end

#latency=(val) ⇒ Object



6
# File 'lib/droid/utilization.rb', line 6

def latency=(val); @latency = val; end

#monitor(topic, opts = {}) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/droid/utilization.rb', line 60

def monitor(topic, opts={})
  topic = 'temporary' if opts[:temp]

  t1 = Time.now.getutc
  begin
    yield if block_given?
  ensure
    t2 = Time.now.getutc
    record(topic, t2 - t1)
  end
end

#record(topic, secs) ⇒ Object



33
34
35
36
37
# File 'lib/droid/utilization.rb', line 33

def record(topic, secs)
  d = data(topic)
  d['msgs'] += 1
  d['time'] += secs
end

#reinitObject



16
17
18
19
# File 'lib/droid/utilization.rb', line 16

def reinit
  @@start = Time.now.getutc
  @@data = { }
end

#reportObject



107
108
109
110
111
# File 'lib/droid/utilization.rb', line 107

def report
  data = report_data
  summary = report_summary(data)
  { 'data' => data, 'summary' => summary }
end

#report_dataObject



72
73
74
75
76
77
78
79
# File 'lib/droid/utilization.rb', line 72

def report_data
  data = {}
  t2 = Time.now.getutc
  topics.each do |topic|
    data[topic] = calc_utilization(topic, t2)
  end
  data
end

#report_summary(data = nil) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/droid/utilization.rb', line 81

def report_summary(data=nil)
  data ||= report_data

  summary = {
    'avg' => 0.0,
    'utilization' => 0.0,
    'msgs' => 0,
    'msgs_per_sec' => 0.0,
    'secs' => 0.0
  }
  data.each do |topic, d|
    summary['utilization'] += d['utilization']
    summary['msgs'] += d['msgs']
    summary['msgs_per_sec'] += d['msgs_per_sec']
    summary['avg'] += d['avg']
    summary['secs'] += d['secs']
  end
  if data.size < 1
    summary['avg'] = 0.0
  else
    summary['avg'] /= data.size
  end

  summary
end

#startObject



12
13
14
# File 'lib/droid/utilization.rb', line 12

def start
  @@start
end

#topicsObject



29
30
31
# File 'lib/droid/utilization.rb', line 29

def topics
  @@data.keys
end