summaryrefslogtreecommitdiff
path: root/features/data/node-load-test.rb
blob: bc83a5f32021a84d9068b663849d11b1e2749b39 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/ruby
#
# A simple load test

$: << File.join(File.dirname(__FILE__), "..", "..", "chef", "lib")
$: << File.join(File.dirname(__FILE__), "..", "..", "chef-solr", "lib")

require 'chef'
require 'chef/client'
require 'tmpdir'

unless ARGV[0] && ARGV[1] && ARGV[2] 
  puts "USAGE: ./node-load-test.rb [nodes] [interval] [splay] (json_attribs)"
  exit 10
end

Chef::Config.from_file("/etc/chef/client.rb")
json_attrs = Hash.new
if ARGV[3]
  json_attrs = Chef::JSON.from_json(IO.read(ARGV[3]))
end
Chef::Log.level = :info
processes = Array.new
STDOUT.sync = true
STDERR.sync = true

Kernel.srand

0.upto(ARGV[0].to_i) do |i|
  cid = Process.fork
  if cid
    puts "Spawned #{cid}"
    processes << cid 
  else
    dir = File.join(Dir.tmpdir, "chef-#{i.to_s}")
    Dir.mkdir(dir) unless File.exists?(dir)
    Chef::Config[:file_store_path] = File.join(dir, "file_store")
    Chef::Config[:file_cache_path] = File.join(dir, "cache")
    Chef::Config[:client_key] = File.join(dir, "client.pem")
    Chef::Config[:splay] = ARGV[2] 
    Chef::Config[:interval] = ARGV[1] 
    Chef::Config[:log_location] = File.join(dir, "client.log")
    Chef::Config[:node_name] = "test#{i}"
    Chef::Log.info("Starting test#{i}")
    loop do 
      begin
        c = Chef::Client.new
        c.node_name = "test#{i}"
        c.safe_name = "test#{i}"
        c.json_attribs = json_attrs
        c.run
        
        Chef::Log.info("Child #{i} is finished")
        Chef::Log.info("Sleeping for #{Chef::Config[:interval]} interval seconds")
        sleep Chef::Config[:interval].to_i

        splay = rand Chef::Config[:splay].to_i
        Chef::Log.info("Splay sleep #{splay} seconds")
        sleep splay
      rescue
        Chef::Log.info("Child #{i} died!")
        Chef::Log.info("Sleeping for #{Chef::Config[:interval]} interval seconds")
        sleep Chef::Config[:interval].to_i
        retry
      end
    end
    puts "Child #{i} is exiting!"
    exit 0
  end
end

Signal.trap("INT") do
  processes.each do |pid|
    Process.kill("INT", pid)
  end
  Process.waitall
  puts "Killed all children - Exiting!"
  exit 0
end

while(true) do
  sleep 1
end