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
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:
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