Skip to content

Instantly share code, notes, and snippets.

@philsturgeon
Last active April 27, 2021 11:04
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save philsturgeon/ea4c3966731c8e2e92e70b11fe10d1bc to your computer and use it in GitHub Desktop.
Save philsturgeon/ea4c3966731c8e2e92e70b11fe10d1bc to your computer and use it in GitHub Desktop.
HTTP 2 Client Call Benchmark
user system total real
faraday (http/1.1): 0.260000 0.030000 0.290000 ( 12.647877)
net-http2: 0.080000 0.010000 0.090000 ( 2.466356)
require 'benchmark'
require 'json'
require 'faraday'
require 'net-http2'
n = 20
host = 'https://secret.example.com/'
params = {
foo: 'something'
}
Benchmark.bm(15) do |x|
x.report("faraday (http/1.1):") do
client = Faraday.new(url: host, ssl: {verify: false}) do |conn|
conn.adapter :net_http
end
n.times do |i|
client.post do |req|
req.url '/check'
req.headers['Content-Type'] = 'application/json'
req.body = JSON.generate(params)
end
end
end
x.report("net-http2:") do
client = NetHttp2::Client.new(host)
n.times do
# prepare request
request = client.prepare_request(:post, '/check', body: JSON.generate(params), headers: {
'content-type' => 'application/json'
})
client.call_async(request)
end
# Wait for all outgoing stream to be closed
client.join
# close the connection
client.close
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment