Skip to content

Instantly share code, notes, and snippets.

@nikushi
Last active December 18, 2015 01:29
Show Gist options
  • Save nikushi/5704182 to your computer and use it in GitHub Desktop.
Save nikushi/5704182 to your computer and use it in GitHub Desktop.
GrowthForecastにランダムURLにグラフPOST
#!/usr/bin/env ruby
require 'growthforecast-client'
require 'parallel'
$requests = 20000 # number of requests to perform
$concurrency = 128 # number of multiple requests to make
puts "requests = #{$requests}"
puts "concurrency = #{$concurrency}"
$gf = GrowthForecast::Client.new("http://server:5125")
srand(Time.now.to_i)
def gen_number
rand(10000)
end
# @num = requests number
def path_for(num)
# generate path by requests number
# $requestsと同数のユニークなpathを生成する
[(num/100).to_s, (num/50).to_s, num.to_s, gen_number]
end
def post(path)
puts "posting to /#{path[0]}/#{path[1]}/#{path[2]}" if ENV['DEBUG']
begin
$gf.post_graph(path[0], path[1], path[2], {"number" => path[3]})
rescue => e
puts e.message
end
end
paths = []
$requests.times{|i| paths << path_for(i) }
stime = Time.now
Parallel.each_with_index(paths, :in_processes => $concurrency) do |path, i|
puts "processing #{i}" if i % 1000 == 0
post(path)
end
etime = Time.now
duration = etime - stime
req_per_sec = ( duration > 0 ) ? $requests/duration : 0
puts "req/sec = #{req_per_sec}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment