Forked from dylanahsmith/tcp-no-delay-benchmark.rb
Last active
April 27, 2023 10:21
-
-
Save alazycoder101/4efc1ab4ce242583a77df61fbf14d4b0 to your computer and use it in GitHub Desktop.
Benchmarking Net::HTTP with and without keep-alive and TCP_NODELAY
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 'net/http' | |
require 'net/http/persistent' # gem install net-http-persistent | |
require 'benchmark' | |
class HttpNoTcpDelay < Net::HTTP | |
def on_connect | |
@socket.io.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1) | |
nil | |
end | |
end | |
ES_URI = URI("http://localhost:9200/") | |
ES_REQUEST = Net::HTTP::Get.new(ES_URI) | |
CONNECTION_NAME = "elasticsearch" | |
def net_http_persistent_test(n) | |
http = Net::HTTP::Persistent.new(name: CONNECTION_NAME) | |
n.times do | |
http.request(ES_URI, ES_REQUEST) | |
end | |
end | |
def net_http_test(http_class, keep_alive, n) | |
http = http_class.new(ES_URI.host, ES_URI.port) | |
if keep_alive | |
http.keep_alive_timeout = 60 | |
http.start | |
end | |
n.times do | |
http.request(ES_REQUEST) | |
end | |
end | |
ITERATIONS = 1000 | |
Benchmark.bmbm do |x| | |
x.report("Net::HTTP::Persistent:") { net_http_persistent_test(ITERATIONS) } | |
x.report("Net::HTTP keep-alive:") { net_http_test(Net::HTTP, true, ITERATIONS) } | |
x.report("HttpNoTcpDelay keep-alive:") { net_http_test(HttpNoTcpDelay, true, ITERATIONS) } | |
x.report("Net::HTTP no keep-alive:") { net_http_test(Net::HTTP, false, ITERATIONS) } | |
x.report("HttpNoTcpDelay no keep-alive:") { net_http_test(HttpNoTcpDelay, false, ITERATIONS) } | |
end |
Author
alazycoder101
commented
Apr 27, 2023
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment