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

I get access to different servers for data and I tried different methods in different classes using 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 
+7
ruby-on-rails curl open-uri rest-client curb
source share
1 answer

I get such results using a test, getting data from Google.

 Warming up -------------------------------------- OpenURI 1.000 i/100ms Net::HTTP 1.000 i/100ms curb 1.000 i/100ms rest_client 1.000 i/100ms Calculating ------------------------------------- OpenURI 10.2589.7%) i/s - 199.000 in 20.003783s Net::HTTP 18.2725.5%) i/s - 362.000 in 20.047560s curb 17.8735.6%) i/s - 356.000 in 20.019155s rest_client 13.68821.9%) i/s - 258.000 in 20.032109s Comparison: Net::HTTP: 18.3 i/s curb: 17.9 i/s - same-ish: difference falls within error rest_client: 13.7 i/s - 1.33x slower OpenURI: 10.3 i/s - 1.78x slower 

And here is the source code of the test

 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 

NOTES:

Remember to set gems before running tests.

 gem install curb rest-client benchmark-ips 

To get more accurate results in a stable network environment, such as production servers

+5
source

All Articles