Skip to content

Instantly share code, notes, and snippets.

@danmayer
Forked from adriaant/nethttp_vs_restclient.rb
Last active December 20, 2015 08:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danmayer/6104806 to your computer and use it in GitHub Desktop.
Save danmayer/6104806 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'rest_client'
require 'net/http'
require 'benchmark'
URL = 'https://www.livingsocial.com/deals/753290-tacos-and-margaritas-for-two-or-four'
time = Benchmark.realtime do
(1..100).each {
url = URI.parse(URL)
req = Net::HTTP::Get.new(url.path)
res = Net::HTTP.start(url.host, url.port, :use_ssl => url.scheme == 'https') {|http| http.request(req) }
}
end
printf("NET::HTTP => Time elapsed %0.3f seconds\n", "#{time}")
time = Benchmark.realtime do
(1..100).each {
RestClient.get(URL)
}
end
printf("RestClient => Time elapsed %0.3f seconds\n", "#{time}")
@danmayer
Copy link
Author

Hmmm confused why using rest client head requests on a bitly url has much worse perf with rest client

https://gist.github.com/adriaant/716155

(modified the original gist to do a 100.each iterations, bitly URL and HEAD requests)
NET::HTTP => Time elapsed 3.781 seconds
RestClient => Time elapsed 82.085 seconds

While my updated gist above with a get request to a non bitly URL shows little difference between rest-client and Net::HTTP?

[master][~/projects/deals] ruby benchmark.rb
NET::HTTP => Time elapsed 37.432 seconds
RestClient => Time elapsed 32.331 seconds

Oddly enough if I change the URL back to the original bitly url, 'http://bit.ly/4okpb2' and run this with the 100 get requests each, again rest-client is much faster when hitting bitly

NET::HTTP => Time elapsed 3.376 seconds
RestClient => Time elapsed 112.337 seconds

Anyone got ideas as to why?

@danmayer
Copy link
Author

Yeah so looks like Adam Keys pointed out it likely is just rest-client follows redirects by default and Net::Http doesn't. Doug Ramsay confirmed that net http just immediately returns the 301

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment