Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Net:HTTP tutorial #1: start block and keep alive requests
# Use the right Net::HTTP api for the right job
#
# use start block and do all requests within it
#
# usecase examples: to build a proxy, a scraper or similars
require 'net/http'
class Getter # simplifies the Net::HTTP api a bit
attr_reader :uri
def initialize(host, protocol=nil)
prot = protocol || "http"
url = "#{prot}://#{host}"
@uri = URI.parse url
end
def start(&block)
Net::HTTP.start(@uri.host, @uri.port) do |http|
@http = http
block.call(self)
end
end
def get(path)
resp = @http.get path
resp.body
end
end
NUM = 10
require 'benchmark'
bench1 = Benchmark.measure do
g = Getter.new "makevoid.com"
g.start do |g|
NUM.times do
g.get "/"
end
end
end
bench2 = Benchmark.measure do
NUM.times do
uri = URI.parse "http://makevoid.com"
Net::HTTP.get_response uri
end
end
puts "#{NUM} requests"
puts "With Keep-Alive HEADER (all gets within a start block)"
puts bench1
puts "without (normal get_response)"
puts bench2
# results:
#
# 100 requests
# With Keep-Alive HEADER (all gets within a start block)
# 0.090000 0.040000 0.130000 ( 10.459873)
# without (normal get_response)
# 0.160000 0.090000 0.250000 ( 23.070951)
# we're saving a HEAD request for each request! sometimes this is almost twice as fast!
#
# @drogus slides from talk at @rupy: http://rupy-http.strobeapp.com/ - very recommended!
#
# [next chapter: concurrent http requests]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment