Skip to content

Instantly share code, notes, and snippets.

@mahemoff
Last active March 11, 2018 08:36
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 mahemoff/0d747d749e42cf84ebc17ecf046ef4cb to your computer and use it in GitHub Desktop.
Save mahemoff/0d747d749e42cf84ebc17ecf046ef4cb to your computer and use it in GitHub Desktop.
Benchmarking performance of persistent HTTP requests
#!/usr/bin/env ruby
require 'uri'
require 'net/http'
require 'benchmark'
# dummy fetch first URL to baseline setup (ensures DNS and any routing
# optimisations done)
def prime_fetching(urls)
ignored = Net::HTTP.get_response(URI(urls.first))
end
# fetch all URLs using keep-alive if server supports it
def fetch_persistent(urls)
uri = URI(urls.first)
Net::HTTP.start(uri.host, uri.port) do |http|
urls.each { |url|
uri = URI(url)
resp = http.get(uri)
}
end
end
# fetch URLs independently
def fetch_independent(urls)
urls.each { |url|
uri = URI(url)
resp = Net::HTTP.get_response(uri)
}
end
# MAIN
# Read a file containing list of URLs - we'll get 100 URLs
# by reading the first 10 and repeating that 9 more times
urls = open('urls.txt').read.split.first(10) * 10
prime_fetching urls
Benchmark.bm { |b|
b.report('persistent') { fetch_persistent urls }
b.report('independent') { fetch_independent urls }
}
start
user system total real
persistent 0.060151 0.008257 0.068408 ( 8.449892)
independent 0.098917 0.028537 0.127454 ( 16.261410)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment