Skip to content

Instantly share code, notes, and snippets.

@sadovnik
Created March 30, 2018 13:25
Show Gist options
  • Save sadovnik/1d597e1da023ff759dcbeb5e53688585 to your computer and use it in GitHub Desktop.
Save sadovnik/1d597e1da023ff759dcbeb5e53688585 to your computer and use it in GitHub Desktop.
Keep-alive vs new TCP connection each time
require "excon"
require "benchmark"
n = 10000
Benchmark.bm(30) do |benchmark|
benchmark.report("reuse connection") do
client = Excon.new("http://localhost:8080", persistent: true)
n.times do
client.get(path: "/")
end
end
benchmark.report("open new connection each time") do
n.times do
Excon.get("http://localhost:8080/")
end
end
end
user system total real
reuse connection 9.080000 5.310000 14.390000 ( 34.927759)
open new connection each time 13.800000 14.900000 28.700000 ( 71.979245)
require 'rack'
app = Proc.new do |env|
current_time = Time.new
response_body = "Current time is #{current_time}"
[
'200',
{ 'Content-Type' => 'text/html' },
[response_body]
]
end
Rack::Handler::WEBrick.run app
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment