Skip to content

Instantly share code, notes, and snippets.

@agate
Created December 12, 2013 03:06
Show Gist options
  • Save agate/7922625 to your computer and use it in GitHub Desktop.
Save agate/7922625 to your computer and use it in GitHub Desktop.
Curl / Curl with multi / Curl with parallel gem
require 'parallel'
require 'curb'
TEST_URL = 'http://www.baidu.com'
def normal
responses = []
100.times do
easy = Curl::Easy.new(TEST_URL)
easy.perform
responses << easy.body_str
end
end
def curl_multi
responses = []
multi = Curl::Multi.new
100.times do
easy = Curl::Easy.new(TEST_URL) do |curl|
curl.on_complete do |res|
responses << res.body_str
end
end
multi.add easy
end
multi.perform
end
def parallel
responses = Parallel.map 1..100 do
easy = Curl::Easy.new(TEST_URL)
easy.perform
easy.body_str
end
end
[ :normal, :curl_multi, :parallel ].each do |strategy|
puts "using #{strategy} way to do test:"
start_at = Time.now
send(strategy)
stop_at = Time.now
time_spent = stop_at - start_at
puts "spent: #{time_spent}s"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment