Skip to content

Instantly share code, notes, and snippets.

@dmcclory
Created May 1, 2015 18:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmcclory/1b9203d56659b1ab86ea to your computer and use it in GitHub Desktop.
Save dmcclory/1b9203d56659b1ab86ea to your computer and use it in GitHub Desktop.
# some real quick code for running requests in parallel
# usage:
# $ ruby parallel_req.rb http://url_place/ post 5 data.json
# doesn't work with get requests (yet)!
require 'http'
url = ARGV[0]
method = ARGV[1]
thread_count = ARGV[2].to_i
data = File.read(ARGV[3])
results = {}
threads = thread_count.times.map do |n|
Thread.new do |t|
start_time = Time.now
HTTP.send(method, url, :json => data)
end_time = Time.now
duration = end_time - start_time
results[n] = duration
end
end
threads.each do |t|
t.join
end
results.each do |id, duration|
puts "thread[#{id}]: #{duration}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment