Ruby/Rails Performance: OpenURI vs NET:HTTP vs Curb vs Rest-Client
Asked Answered
C

1

8

I'm accessing different servers for data and I tried different methods in different classes, using the basic http::net, curb, rest-client and open-uri

(1) How to measure performance in Ruby / Rails in General? (2) Which method do you think is faster?

Sample code from all 4 different methods:

  url = "..."
  begin 
    io_output = open(url, :http_basic_authentication => [@user_id, @user_password])
  rescue => e
    error = e.message #for debugging return this
    return '-'
  else 
    output = io_output.read 

or

require 'net/https'
uri = URI.parse("...")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true 
http.verify_mode = OpenSSL::SSL::VERIFY_PEER 
data = http.get(uri.request_uri) #http request status
res = Net::HTTP.get_response(uri)
puts res.body if res.is_a?(Net::HTTPSuccess)

or

require 'curb'
url = "..."
c = Curl::Easy.new(url) do |curl| 
curl.headers["Content-type"] = "application/json"
curl.headers["Authorization"] = "Token ..."
end
c.perform
puts c.body_str

or

url = "..." 
resource = RestClient::Resource.new(url, 
       :headers => { :Authorization => "Token ...",  
       :content_type => "application/json"})  
 begin 
 output = resource.get
rescue => e
error = e.message #for debugging return this
return '-'
else ...
end 
Chapfallen answered 12/7, 2013 at 15:56 Comment(3)
I found ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html I will try itChapfallen
Post your results as an answer. I'd be curious to see them.Postpositive
I'm going to do more testing but! running this for 5 times for each client: http::net ~ 13 seconds open-uri ~ 8 seconds rest-client ~6.9 and curb ~ 6.3 secondsChapfallen
B
6

I get this kind of results with the next benchmark, by retrieving data from Google.

Warming up --------------------------------------
             OpenURI     3.000  i/100ms
           Net::HTTP     3.000  i/100ms
                curb     3.000  i/100ms
         rest_client     3.000  i/100ms
Calculating -------------------------------------
             OpenURI     34.848  (±11.5%) i/s -    687.000  in  20.013469s
           Net::HTTP     35.433  (±14.1%) i/s -    594.000  in  20.006947s
                curb     31.612  (±19.0%) i/s -    465.000  in  20.021108s
         rest_client     34.331  (±11.7%) i/s -    675.000  in  20.044486s

Comparison:
           Net::HTTP:       35.4 i/s
             OpenURI:       34.8 i/s - same-ish: difference falls within error
         rest_client:       34.3 i/s - same-ish: difference falls within error
                curb:       31.6 i/s - same-ish: difference falls within error

And here is a source code of the benchmark

require 'benchmark/ips'
require 'open-uri'
require 'net/http'
require 'curb'
require 'rest-client'

google_uri = URI('http://www.google.com/')
google_uri_string = google_uri.to_s

Benchmark.ips do |x|
  x.config(time: 20, warmup: 10)
  x.report('OpenURI') { open(google_uri_string) }
  x.report('Net::HTTP') { Net::HTTP.get(google_uri) }
  x.report('curb') { Curl.get(google_uri_string) }
  x.report('rest_client') { RestClient.get(google_uri_string) }
  x.compare!
end

ENVIRONMENT:

  1. AWS EC2 server
  2. Ruby version - 2.5.1p57
  3. Gems: curb-0.9.6, rest-client-2.0.2, benchmark-ips-2.7.2

NOTES:

Don't forget to install gems before running this benchmark

gem install curb rest-client benchmark-ips

To get more accurate results run in a stable network environment, like production servers

Bufflehead answered 19/6, 2014 at 20:48 Comment(4)
I remember curb being the fastest too from my testsChapfallen
If you still have it, could you add your code here so others could do this for themselves?Lepper
In Ruby 2.3.6, the differences are negligible.Sternutation
@Sternutation yes on latest versions of these gems and ruby 2.5.1 benchmarks gives almost same results, I updated the answer, thx for the note ;)Bufflehead

© 2022 - 2024 — McMap. All rights reserved.