Class: Riemann::Tools::Bench

Inherits:
Object
  • Object
show all
Includes:
Riemann::Tools
Defined in:
lib/riemann/tools/bench.rb

Constant Summary

Constants included from Riemann::Tools

VERSION

Instance Attribute Summary collapse

Attributes included from Riemann::Tools

#argv

Instance Method Summary collapse

Methods included from Riemann::Tools

#attributes, #endpoint_name, included, #options, #report, #riemann

Constructor Details

#initializeBench

Returns a new instance of Bench.



15
16
17
18
19
20
21
22
# File 'lib/riemann/tools/bench.rb', line 15

def initialize
  super

  @hosts = [nil] + (0...10).map { |i| "host#{i}" }
  @hosts = %w[a b c d e f g h i j]
  @services = %w[test1 test2 test3 foo bar baz xyzzy attack cat treat]
  @states = {}
end

Instance Attribute Details

#hostsObject

Returns the value of attribute hosts.



13
14
15
# File 'lib/riemann/tools/bench.rb', line 13

def hosts
  @hosts
end

#servicesObject

Returns the value of attribute services.



13
14
15
# File 'lib/riemann/tools/bench.rb', line 13

def services
  @services
end

#statesObject

Returns the value of attribute states.



13
14
15
# File 'lib/riemann/tools/bench.rb', line 13

def states
  @states
end

Instance Method Details

#evolve(state) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/riemann/tools/bench.rb', line 24

def evolve(state)
  m = state[:metric] + ((rand - 0.5) * 0.1)
  m = m.clamp(0, 1)

  s = case m
      when 0...0.75
        'ok'
      when 0.75...0.9
        'warning'
      when 0.9..1.0
        'critical'
      end

  {
    metric: m,
    state: s,
    host: state[:host],
    service: state[:service],
    description: "at #{Time.now}",
  }
end

#runObject



53
54
55
56
57
58
59
# File 'lib/riemann/tools/bench.rb', line 53

def run
  start
  loop do
    sleep 0.05
    tick
  end
end

#startObject



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/riemann/tools/bench.rb', line 61

def start
  hosts.product(services).each do |host, service|
    states[[host, service]] = {
      metric: 0.5,
      state: 'ok',
      description: 'Starting up',
      host: host,
      service: service,
    }
  end
end

#tickObject



46
47
48
49
50
51
# File 'lib/riemann/tools/bench.rb', line 46

def tick
  #    pp @states
  hosts.product(services).each do |id|
    report(states[id] = evolve(states[id]))
  end
end