Created
July 30, 2013 18:17
-
-
Save chrisbloom7/6115392 to your computer and use it in GitHub Desktop.
Benchmarking several different ways of fetching a URI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'benchmark' | |
def validate_by_request(value) | |
uri = URI.parse(value) | |
http = Net::HTTP.new(uri.host, uri.port) | |
if uri.scheme == "https" | |
http.use_ssl = true | |
http.verify_mode = OpenSSL::SSL::VERIFY_NONE | |
end | |
request = Net::HTTP::Get.new(uri.request_uri) | |
http.request(request) | |
end | |
def validate_by_get_response(value) | |
uri = URI.parse(value) | |
http = Net::HTTP.new(uri.host, uri.port) | |
if uri.scheme == "https" | |
http.use_ssl = true | |
http.verify_mode = OpenSSL::SSL::VERIFY_NONE | |
end | |
http.get(value) | |
end | |
def validate_by_request_get(value) | |
uri = URI.parse(value) | |
http = Net::HTTP.new(uri.host, uri.port) | |
if uri.scheme == "https" | |
http.use_ssl = true | |
http.verify_mode = OpenSSL::SSL::VERIFY_NONE | |
end | |
http.request_get(uri.request_uri) | |
end | |
def validate_by_head(value) | |
uri = URI.parse(value) | |
http = Net::HTTP.new(uri.host, uri.port) | |
if uri.scheme == "https" | |
http.use_ssl = true | |
http.verify_mode = OpenSSL::SSL::VERIFY_NONE | |
end | |
http.head(uri.request_uri) | |
end | |
n = 100 | |
http_value = "http://news.google.com/" | |
https_value = "https://news.google.com/" | |
Benchmark.bmbm do |x| | |
x.report("http by request") {n.times do; validate_by_request(http_value); end} | |
x.report("http by get_response") {n.times do; validate_by_get_response(http_value); end} | |
x.report("http by request_get") {n.times do; validate_by_request_get(http_value); end} | |
x.report("http by head") {n.times do; validate_by_head(http_value); end} | |
x.report("https by request") {n.times do; validate_by_request(https_value); end} | |
x.report("https by get_response") {n.times do; validate_by_get_response(https_value); end} | |
x.report("https by request_get") {n.times do; validate_by_request_get(https_value); end} | |
x.report("https by head") {n.times do; validate_by_head(https_value); end} | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sample output: