Class: ReqSample::Generator
- Inherits:
-
Object
- Object
- ReqSample::Generator
- Defined in:
- lib/reqsample/generator.rb
Overview
Main class for creating randomized data.
Constant Summary collapse
- DEFAULT_COUNT =
1000- DEFAULT_DOMAIN =
'http://example.com'.freeze
- DEFAULT_FORMAT =
:apache- DEFAULT_MAX_BYTES =
512
Instance Attribute Summary collapse
-
#agents ⇒ Object
Returns the value of attribute agents.
-
#codes ⇒ Object
Returns the value of attribute codes.
-
#connectivity ⇒ Object
Returns the value of attribute connectivity.
-
#dist ⇒ Object
Returns the value of attribute dist.
-
#max_bytes ⇒ Object
Returns the value of attribute max_bytes.
-
#networks ⇒ Object
Returns the value of attribute networks.
-
#verbs ⇒ Object
Returns the value of attribute verbs.
Instance Method Summary collapse
- #format(style, country, sample) ⇒ Object
-
#initialize(peak_sd = 4.0) ⇒ Generator
constructor
A new instance of Generator.
-
#produce(opts = {}) ⇒ Array<String, Hash>
The collection of generated log events.
- #sample(time = nil, fmt = nil) ⇒ Object
- #sample_address(country = nil) ⇒ Object
-
#sample_time(peak, truncate) ⇒ Object
Limit the normal distribution to +/- 12 hours (assume we want to stay within a 24-hour period).
Constructor Details
#initialize(peak_sd = 4.0) ⇒ Generator
Returns a new instance of Generator.
29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/reqsample/generator.rb', line 29 def initialize(peak_sd = 4.0) @agents = ReqSample::Hash.weighted(vendor('user_agents.json')) @codes = ReqSample::Hash.weighted(ReqSample::RESPONSE_CODES) # Peak at zero (will be summed with the Time object) @connectivity = ReqSample::Hash.weighted( vendor('country_connectivity.json') ) @dist = Rubystats::NormalDistribution.new(0, peak_sd) @max_bytes = DEFAULT_MAX_BYTES @networks = vendor('country_networks.json') @verbs = ReqSample::Hash.weighted(ReqSample::REQUEST_VERBS) end |
Instance Attribute Details
#agents ⇒ Object
Returns the value of attribute agents.
15 16 17 |
# File 'lib/reqsample/generator.rb', line 15 def agents @agents end |
#codes ⇒ Object
Returns the value of attribute codes.
15 16 17 |
# File 'lib/reqsample/generator.rb', line 15 def codes @codes end |
#connectivity ⇒ Object
Returns the value of attribute connectivity.
15 16 17 |
# File 'lib/reqsample/generator.rb', line 15 def connectivity @connectivity end |
#dist ⇒ Object
Returns the value of attribute dist.
15 16 17 |
# File 'lib/reqsample/generator.rb', line 15 def dist @dist end |
#max_bytes ⇒ Object
Returns the value of attribute max_bytes.
15 16 17 |
# File 'lib/reqsample/generator.rb', line 15 def max_bytes @max_bytes end |
#networks ⇒ Object
Returns the value of attribute networks.
15 16 17 |
# File 'lib/reqsample/generator.rb', line 15 def networks @networks end |
#verbs ⇒ Object
Returns the value of attribute verbs.
15 16 17 |
# File 'lib/reqsample/generator.rb', line 15 def verbs @verbs end |
Instance Method Details
#format(style, country, sample) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/reqsample/generator.rb', line 89 def format(style, country, sample) case style.to_s when 'apache' [ "#{sample[:address]} - -", "[#{sample[:time].strftime('%d/%b/%Y:%H:%M:%S %z')}]", %("#{sample[:verb]} #{sample[:path]} HTTP/1.1"), sample[:code], sample[:bytes], %("#{sample[:domain]}"), %("#{sample[:agent]}") ].join ' ' else { country => sample } end end |
#produce(opts = {}) ⇒ Array<String, Hash>
Returns the collection of generated log events.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/reqsample/generator.rb', line 48 def produce(opts = {}) opts[:count] ||= DEFAULT_COUNT opts[:format] ||= DEFAULT_FORMAT opts[:peak] ||= Time.now - (12 * 60 * 60) opts[:truncate] ||= 12 1.upto(opts[:count]).map do |_| sample_time opts[:peak], opts[:truncate] end.sort.map do |time| if block_given? if (delay = time - Time.now) > 0 then sleep delay end yield sample time, opts[:format] else sample time, opts[:format] end end end |
#sample(time = nil, fmt = nil) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/reqsample/generator.rb', line 66 def sample(time = nil, fmt = nil) # Pull a random country, but ensure it's a valid country code for the # list of networks that we have available. country = connectivity.weighted_sample do |ccodes| ccodes.detect do |ccode| networks.keys.include? ccode end end sample = { address: sample_address(country), agent: agents.weighted_sample, bytes: rand(max_bytes), code: codes.weighted_sample, domain: DEFAULT_DOMAIN, path: ReqSample::REQUEST_PATHS.sample, time: time || sample_time(opts), verb: verbs.weighted_sample } format fmt, country, sample end |
#sample_address(country = nil) ⇒ Object
106 107 108 109 110 111 112 113 114 |
# File 'lib/reqsample/generator.rb', line 106 def sample_address(country = nil) country ||= networks.keys.sample head, tail = networks[country].sample IPAddr.new( rand(IPAddr.new(head).to_i..IPAddr.new(tail).to_i), Socket::AF_INET ) end |
#sample_time(peak, truncate) ⇒ Object
Limit the normal distribution to +/- 12 hours (assume we want to stay within a 24-hour period).
118 119 120 121 122 123 |
# File 'lib/reqsample/generator.rb', line 118 def sample_time(peak, truncate) loop do sample = ReqSample::Time.at((peak + (dist.rng * 60 * 60)).to_i) break sample if sample.within peak, truncate end end |