File: traffic-creator

package info (click to toggle)
chef-expander 10.12.0-1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 208 kB
  • sloc: ruby: 1,533; sh: 146; makefile: 2
file content (97 lines) | stat: -rwxr-xr-x 2,846 bytes parent folder | download
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
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/usr/bin/env ruby
#
# Author:: Daniel DeLeo (<dan@opscode.com>)
# Author:: Seth Falcon (<seth@opscode.com>)
# Author:: Chris Walters (<cw@opscode.com>)
# Copyright:: Copyright (c) 2010-2011 Opscode, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

require "rubygems"

$:.unshift(File.expand_path(File.dirname(__FILE__) + '/../lib'))

require 'pp'
require 'bunny'
require 'yajl'
require 'uuidtools'
require 'word_salad'

require 'chef_search/expander/configuration'

Chef::Expander.init_config(ARGV)

MESSAGES_TO_SEND = 10_000

NUM_RAND_KEY_PAIRS = 50
NUM_RAND_VALUE_PAIRS = 50

PERSISTENT_MESSAGES = true

KEYS = NUM_RAND_VALUE_PAIRS.words

SAMPLE_NODES = []
Dir.glob(File.expand_path(File.dirname(__FILE__)) + '/../data/*_node.json') do |node_file|
  SAMPLE_NODES << Yajl::Parser.parse(IO.read(node_file))
end

NUM_NODES = SAMPLE_NODES.size
puts "Read #{NUM_NODES} sample nodes"

puts "Using rabbitmq config #{Chef::Expander.config.amqp_config.inspect}"

puts "connecting to rabbitmq"
amqp_client = Bunny.new(Chef::Expander.config.amqp_config)
amqp_client.start

puts 'declaring queues'
queues = {}
0.upto(1023) do |vnode|
  queues[vnode] = amqp_client.queue("vnode-#{vnode}", :durable => true)
end
  
def add_rand_keys(node)
  rand_key_vals = Hash[*((2 * NUM_RAND_KEY_PAIRS).words)]
  rand_vals = Hash[*(KEYS.zip(NUM_RAND_VALUE_PAIRS.words)).flatten]
  node.merge(rand_key_vals.merge(rand_vals))
end

puts "sending #{MESSAGES_TO_SEND} messages"
start_time = Time.now
sent_messages = 0
1.upto(MESSAGES_TO_SEND) do
  node = SAMPLE_NODES[rand(NUM_NODES)]
  node = add_rand_keys(node)
  index_data = {:action => :add}
  index_data[:payload] = {:item => node}
  index_data[:payload][:type] = :node
  index_data[:payload][:database] = :testdb
  index_data[:payload][:enqueued_at] = Time.now.utc.to_i

  id = node["name"]
  vnode = rand(1024)
  index_data[:payload][:id] = id

  puts "queue: vnode-#{vnode} (#{sent_messages} / #{MESSAGES_TO_SEND})"
  amqp_client.tx_select if PERSISTENT_MESSAGES
  queues[vnode].publish(Yajl::Encoder.encode(index_data), :persistent => PERSISTENT_MESSAGES)
  amqp_client.tx_commit if PERSISTENT_MESSAGES
  sent_messages += 1
end
end_time = Time.now

total_time = end_time - start_time
rate = MESSAGES_TO_SEND.to_f / total_time
puts "done (#{total_time}s, #{rate} msg/s)"