class MCollective::Application::Ping

Public Instance Methods

main() click to toggle source
   # File lib/mcollective/application/ping.rb
51 def main
52   # If the user did not override the default timeout include the discovery timeout
53   if options[:timeout] == 5
54     discovery_timeout = options[:disctimeout] || Config.instance.discovery_timeout || 0
55     options[:timeout] = options[:timeout] + discovery_timeout
56   end
57   client = MCollective::Client.new(options)
58 
59   start = Time.now.to_f
60   times = []
61 
62   client.req("ping", "discovery") do |resp|
63     times << (Time.now.to_f - start) * 1000
64 
65     puts "%-40s time=%.2f ms" % [resp[:senderid], times.last]
66   end
67 
68   puts("\n\n---- ping statistics ----")
69 
70   if times.size > 0
71     sum = times.inject(0){|acc,i|acc +i}
72     avg = sum / times.length.to_f
73 
74     puts "%d replies max: %.2f min: %.2f avg: %.2f %s" % [times.size, times.max, times.min, avg, spark(times)]
75   else
76     puts("No responses received")
77   end
78 
79   halt client.stats
80 end
spark(resp_times) click to toggle source

Convert the times structure into a array representing buckets of responses in 50 ms intervals. Return a small sparkline graph using UTF8 characters

   # File lib/mcollective/application/ping.rb
15 def spark(resp_times)
16   return "" unless configuration[:graph] || Config.instance.pluginconf["rpc.graph"]
17 
18   ticks=%w[▁ ▂ ▃ ▄ ▅ ▆ ▇]
19 
20   histo = {}
21 
22   # round each time to its nearest 50ms
23   # and keep a count for each 50ms
24   resp_times.each do |time|
25     time = Integer(time + 50 - (time % 50))
26     histo[time] ||= 0
27     histo[time] += 1
28   end
29 
30   # set the 50ms intervals that saw no traffic to 0
31   ((histo.keys.max - histo.keys.min) / 50).times do |i|
32     time = (i * 50) + histo.keys.min
33     histo[time] = 0 unless histo[time]
34   end
35 
36   # get a numerically sorted list of times
37   histo = histo.keys.sort.map{|k| histo[k]}
38 
39   range = histo.max - histo.min
40   scale = ticks.size - 1
41   distance = histo.max.to_f / scale
42 
43   histo.map do |val|
44     tick = (val / distance).round
45     tick = 0 if tick < 0
46 
47     ticks[tick]
48   end.join
49 end