Ruby: HTTPClient is was Sloooooow
9 September 2008, 03:16pm
Update: A new version of HTTPClient has fixed my slowness problems. More info in the comments.
Is this just me? I think it must be—I am getting terrible performance using HTTPClient vs Net::HTTP. I was looking for an alternative to Net::HTTP because its API is ugly as hell, but the performance difference leaves me with no choice.
Let’s take the example of loading this blog’s last four entries using the Google AJAX Feed API. Net::HTTP consistently takes less than a second to load the results, while HTTPClient consistently takes between 10 and 15 seconds! This can’t be right, I must be doing something wrong…
Here’s the script I’m using to test:
require 'httpclient'
require 'net/http'
require 'uri'
require 'cgi'
def time(label)
t_start = Time.now.to_f
yield if block_given?
puts "#{label}: #{Time.now.to_f - t_start}s"
end
feed_url = "http://taylorbarstow.com/feed/"
url = "http://www.google.com/uds/Gfeeds?v=1.0&output=json&q=#{CGI.escape(feed_url)}"
time "Net::HTTP" do
Net::HTTP.get(URI.parse(url))
end
time "HTTPClient" do
HTTPClient.new.get_content(url)
end
Tags: programming, ruby

4 Comments
With httpclient/2.1.3-RC it gives;
Net::HTTP: 0.13388991355896s
HTTPClient: 0.122214078903198s
httpclient/2.1.2 wrongly tried to get source port address for logging that can be slow under some host configuration.
@NaHi thanks for the info - I’ll definitely check out a different version and see if that solves my problems.
Have you seen HTTParty? It looks pretty sweet for simple stuff.
Yep, confirmed, the new version fixes my problem. Thanks, @NaHi!
Good to know it works now.
I benchmarked HTTParty and posted [ruby-talk:322934]. And I’ll check those interfaces to know what is missing for httpclient. Thanks!