Skip to content

Instantly share code, notes, and snippets.

@chrisbloom7
Created July 30, 2013 18:17
Show Gist options
  • Save chrisbloom7/6115392 to your computer and use it in GitHub Desktop.
Save chrisbloom7/6115392 to your computer and use it in GitHub Desktop.
Benchmarking several different ways of fetching a URI
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
@chrisbloom7
Copy link
Author

Sample output:

Rehearsal ---------------------------------------------------------
http by request         9.490000   0.930000  10.420000 ( 64.419095)
http by get_response    9.030000   1.010000  10.040000 ( 65.556192)
http by request_get     8.290000   1.070000   9.360000 ( 64.260960)
http by head            0.250000   0.100000   0.350000 ( 28.397489)
https by request       11.170000   1.380000  12.550000 ( 76.095995)
https by get_response  10.630000   1.250000  11.880000 ( 80.938285)
https by request_get   10.140000   1.250000  11.390000 ( 78.245785)
https by head           1.040000   0.140000   1.180000 ( 37.990245)
----------------------------------------------- total: 67.170000sec

                            user     system      total        real
http by request         6.550000   1.020000   7.570000 ( 64.455071)
http by get_response    6.450000   1.000000   7.450000 ( 65.958293)
http by request_get     5.880000   0.970000   6.850000 ( 63.008249)
http by head            0.230000   0.080000   0.310000 ( 25.244908)
https by request        8.570000   1.200000   9.770000 ( 75.061067)
https by get_response   8.330000   1.200000   9.530000 ( 74.342267)
https by request_get    7.960000   1.200000   9.160000 ( 72.481586)
https by head           0.920000   0.140000   1.060000 ( 37.312157)

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