Class: RateThrottleClient::Demo

Inherits:
Object
  • Object
show all
Defined in:
lib/rate_throttle_client/demo.rb

Defined Under Namespace

Classes: TimeIsUpError

Constant Summary collapse

MINUTE =
60
THREAD_COUNT =
ENV.fetch("THREAD_COUNT") { 5 }.to_i
PROCESS_COUNT =
ENV.fetch("PROCESS_COUNT") { 2 }.to_i
DURATION =
ENV.fetch("DURATION") { 30 }.to_i * MINUTE
TIME_SCALE =
ENV.fetch("TIME_SCALE", 1).to_f
RACKUP_FILE =
Pathname.new(__dir__).join("servers/gcra/config.ru")

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client:, thread_count: THREAD_COUNT, process_count: PROCESS_COUNT, duration: DURATION, log_dir: nil, time_scale: TIME_SCALE, stream_requests: false, json_duration: 30, rackup_file: RACKUP_FILE, starting_limit: 0, remaining_stop_under: nil) ⇒ Demo

Returns a new instance of Demo.



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
# File 'lib/rate_throttle_client/demo.rb', line 63

def initialize(client:,thread_count: THREAD_COUNT, process_count: PROCESS_COUNT, duration: DURATION, log_dir: nil, time_scale: TIME_SCALE, stream_requests: false, json_duration: 30, rackup_file: RACKUP_FILE, starting_limit: 0, remaining_stop_under: nil)
  @client = client
  @thread_count = thread_count
  @process_count = process_count
  @duration = duration
  @time_scale = time_scale.to_f
  @stream_requests = stream_requests
  @rackup_file = rackup_file
  @starting_limit = starting_limit
  @remaining_stop_under = remaining_stop_under

  if log_dir
    @log_dir = Pathname.new(log_dir)
  else
    @log_dir = Pathname.new(__dir__).join("../../logs/clients/#{Time.now.strftime('%Y-%m-%d-%H-%M-%s-%N')}-#{client.class}")
  end

  @mutex = Mutex.new
  @json_duration = 30 # seconds
  @port = UniquePort.call
  @threads = []
  @pids = []
  Timecop.scale(@time_scale)

  FileUtils.mkdir_p(@log_dir)
end

Instance Attribute Details

#log_dirObject (readonly)

Returns the value of attribute log_dir.



61
62
63
# File 'lib/rate_throttle_client/demo.rb', line 61

def log_dir
  @log_dir
end

#rackup_fileObject (readonly)

Returns the value of attribute rackup_file.



61
62
63
# File 'lib/rate_throttle_client/demo.rb', line 61

def rackup_file
  @rackup_file
end

Instance Method Details

#callObject



130
131
132
133
134
135
136
137
138
# File 'lib/rate_throttle_client/demo.rb', line 130

def call
  WaitForIt.new("bundle exec puma #{@rackup_file.to_s} -p #{@port}", env: {"TIME_SCALE" => @time_scale.to_i.to_s, "STARTING_LIMIT" => @starting_limit.to_s}, wait_for: "Use Ctrl-C to stop") do |spawn|
    @process_count.times.each do
      boot_process
    end

    @pids.map { |pid| Process.wait(pid) }
  end
end

#chart(open_file) ⇒ Object



106
107
108
109
# File 'lib/rate_throttle_client/demo.rb', line 106

def chart(open_file)
  chart = RateThrottleClient::Chart.new(log_dir: @log_dir, name: @client.class.to_s.gsub("RateThrottleClient::", ""), time_scale: @time_scale)
  chart.call(open_file)
end


90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/rate_throttle_client/demo.rb', line 90

def print_results(io = STDOUT)
  result_hash = self.results
  io.puts
  io.puts "### #{@client.class} results (duration: #{@duration/60.0} minutes, multiplier: #{@client.multiplier})"
  io.puts
  io.puts "```"
  io.puts "Avg retry rate:      #{"%.2f" % (result_hash["retry_ratio"].mean * 100)} %"
  io.puts "Max sleep time:      #{"%.2f" % result_hash["max_sleep_val"].max} seconds"
  io.puts "Stdev Request Count: #{"%.2f" % result_hash["request_count"].stdev}"
  io.puts
  result_hash.each do |key, value|
    io.puts "Raw #{key}s: [#{ value.map {|x| "%.2f" % x}.join(", ")}]"
  end
  io.puts "```"
end

#resultsObject



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/rate_throttle_client/demo.rb', line 111

def results
  result_hash = {}

  @log_dir.entries.map do |entry|
    @log_dir.join(entry)
  end.select do |file|
    file.file? && file.extname == ".json"
  end.sort.map do |file|
    JSON.parse(file.read)
  end.each do |json|
    json.each_key do |key|
      result_hash[key] ||= []
      result_hash[key] << json[key]
    end
  end

  result_hash
end