Last active
December 15, 2015 12:39
-
-
Save spritle/5261906 to your computer and use it in GitHub Desktop.
Fetch data in parallel using Typhoeus Typhoeus took 0.99148906 seconds
Httparty took 3.370132528 seconds
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "json" | |
# option 1 | |
require "typhoeus" | |
#option 2 | |
require "httparty" | |
$url1 = "https://api.twitter.com/1/statuses/user_timeline.json?screen_name=dchelimsky&count=2" | |
$url2 = "https://api.twitter.com/1/statuses/user_timeline.json?screen_name=spritlesoftware&count=50" | |
$url3 = "https://api.twitter.com/1/statuses/user_timeline.json?screen_name=TouchVu&count=1" | |
$url4 = "https://api.twitter.com/1/statuses/user_timeline.json?screen_name=ibmmobile&count=30" | |
def print_tweet(url, raw_response_body) | |
puts url | |
puts JSON.parse(raw_response_body)[0]["text"], "-----" | |
end | |
@first_request = Typhoeus::Request.new($url1) | |
@first_request.on_complete do |response| | |
print_tweet($url1, response.body) | |
end | |
@second_request = Typhoeus::Request.new($url2) | |
@second_request.on_complete do |response| | |
print_tweet($url2, response.body) | |
end | |
@third_request = Typhoeus::Request.new($url3) | |
@third_request.on_complete do |response| | |
print_tweet($url3, response.body) | |
end | |
@fourth_request = Typhoeus::Request.new($url4) | |
@fourth_request.on_complete do |response| | |
print_tweet($url4, response.body) | |
end | |
def fetch_with_typhoeus | |
hydra = Typhoeus::Hydra.new | |
hydra.queue @first_request | |
hydra.queue @second_request | |
hydra.queue @third_request | |
hydra.queue @fourth_request | |
hydra.run | |
end | |
def fetch_with_httparty | |
response = HTTParty.get($url1) | |
print_tweet($url1, response.body) | |
response = HTTParty.get($url2) | |
print_tweet($url2, response.body) | |
response = HTTParty.get($url3) | |
print_tweet($url3, response.body) | |
response = HTTParty.get($url4) | |
print_tweet($url4, response.body) | |
end | |
start_time = Time.now | |
fetch_with_typhoeus | |
typhoeus_time = Time.now - start_time | |
start_time = Time.now | |
fetch_with_httparty | |
httparty_time = Time.now - start_time | |
puts "Typhoeus took #{typhoeus_time} seconds" | |
puts "Httparty took #{httparty_time} seconds" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment