Steve ConoverSteve Conover
net/http alternatives
edit Posted by Steve Conover on Monday December 29, 2008 at 09:00PM

net/http is slow. (and so are libraries that depend on it, like open-uri)

Performance Disclaimer: this ought to matter in your app, measurably, before you do anything about it. If you profile and ruby-prof is showing a bunch of classes like BufferedRead and Timeout at the top of the list, your app qualifies. And in addition if you know that your app is dependent on data transfer over http (let's say you're interacting with a Solr server, and you're storing sizable documents in Solr), you should be aware of the problem.

Otherwise net/http or open-uri might be just fine for you.

The problems with net/http, and benchmarks of ruby http client lbraries are nicely written about in An analysis of Ruby 1.8.x HTTP client performance.

Some good alternatives:

Our findings matched the article referenced above - the alternatives have pros and cons but each was at least 10x faster than net/http for transfers of 50-300k response bodies.

The fastest solution we found was curb, reusing the Curl::Easy object:

require "curb"

curl = Curl::Easy.new

2.times do
    curl.url = "http://www.pivotaltracker.com"
    curl.perform
    puts curl.body_str
end

Comments

  1. Dr Nic Dr Nic on December 29, 2008 at 11:03PM

    Thanks for the summary!

  2. NaHi (httpclient) NaHi (httpclient) on December 30, 2008 at 12:39AM

    For downloading purpose, Curl::Easy.download might be faster and has a small memory footprint thought I've not yet checked in detail. -> http://dev.ctor.org/http-access2/browser/trunk/bench/bm_download.rb

    net/http is enough fast when you apply a patch at http://7fff.com/2008/12/20/faster-nethttp-for-ruby-186/ -> http://dev.ctor.org/http-access2/browser/trunk/bench/bm_common.rb

    On ruby 1.9.1, httpclient is faster than curb and rfuzz on my simple try (not benchmarked enough; I don't have foreign test server), but the difference is a little.

  3. TJ Holowaychuk TJ Holowaychuk on January 03, 2009 at 04:45AM

    Ah! fantastic, I was always complaining about open-uri haha, curb <3