Skip to content

Instantly share code, notes, and snippets.

@nevans
Last active August 29, 2015 14:00
Show Gist options
  • Save nevans/259d2182b79bc5886290 to your computer and use it in GitHub Desktop.
Save nevans/259d2182b79bc5886290 to your computer and use it in GitHub Desktop.
HTTP with or without TCP_NODELAY (client or server)
require "benchmark"
require "typhoeus"
require "excon"
require "net/http/persistent"
reps = ENV.fetch("HTTP_BM_REPS", 1000).to_i
url = ENV.fetch("HTTP_BM_URL", "http://localhost:5984/")
nodelay = ENV.fetch("HTTP_BM_NODELAY", "true") == "true"
Excon.defaults[:tcp_nodelay] = nodelay
typh_opts = {tcp_nodelay: nodelay}
uri = URI(url)
typh_req = Typhoeus::Request.new(url, typh_opts)
persistent = Net::HTTP::Persistent.new "benchmark"
excon = Excon.new(url)
hydra = Typhoeus::Hydra.new
persistent.socket_options << [Socket::IPPROTO_TCP, Socket::TCP_NODELAY, nodelay ? 1 : 0]
$stderr.puts "Fetching #{url} #{reps}x with TCP_NODELAY=#{nodelay}"
Benchmark.bm(13) do |x|
x.report("net/http:") do reps.times do Net::HTTP.get(uri) end end
x.report("hydra 10x:") do per = 10; (reps/per).times do per.times do hydra.queue(typh_req) end; hydra.run end end
x.report("hydra 100x:") do per = 100; (reps/per).times do per.times do hydra.queue(typh_req) end; hydra.run end end
x.report("Excon:") do reps.times do excon.get end end
x.report("typhoeus:") do reps.times do Typhoeus.get(url, typh_opts) end end
x.report("persistent:") do reps.times do persistent.request(uri) end end
end
$stderr.puts "done"
Fetching http://localhost:5984/ 1000x with TCP_NODELAY=true
user system total real
net/http: 1.480000 0.370000 1.850000 ( 2.945348) # not pipelined, no penalty
hydra 10x: 1.160000 0.170000 1.330000 ( 5.062462) # adds 40ms * 100
hydra 100x: 0.620000 0.050000 0.670000 ( 0.974187) # adds 40ms * 10
Excon: 0.930000 0.130000 1.060000 ( 41.587622) # adds 40ms * 1000
typhoeus: 1.140000 0.120000 1.260000 ( 42.066603) # adds 40ms * 1000
persistent: 1.630000 0.110000 1.740000 ( 42.139139) # adds 40ms * 1000
done
Fetching http://localhost:5984/ 1000x with TCP_NODELAY=true
user system total real
net/http: 1.510000 0.340000 1.850000 ( 3.072962)
hydra 10x: 0.630000 0.040000 0.670000 ( 1.085322)
hydra 100x: 0.460000 0.040000 0.500000 ( 0.581485)
Excon: 0.490000 0.070000 0.560000 ( 1.390845)
typhoeus: 0.760000 0.080000 0.840000 ( 1.905800)
persistent: 0.920000 0.050000 0.970000 ( 1.955291)
done
curl -X PUT http://localhost:5984/_config/httpd/socket_options -d '"[{nodelay, true}]"'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment