Last active
March 11, 2018 08:36
-
-
Save mahemoff/0d747d749e42cf84ebc17ecf046ef4cb to your computer and use it in GitHub Desktop.
Benchmarking performance of persistent HTTP requests
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
#!/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 } | |
} |
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
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