Skip to content

Instantly share code, notes, and snippets.

@timonv
Created June 21, 2016 14:09
Show Gist options
  • Save timonv/13512bc767c4c56e8db55ea7678fdcdd to your computer and use it in GitHub Desktop.
Save timonv/13512bc767c4c56e8db55ea7678fdcdd to your computer and use it in GitHub Desktop.
# Benchmark of several http clients on the api
#
require 'net/http'
require 'typhoeus'
require 'curb'
URL = "https://nltk.tolq.com/split"
POST_DATA = File.open("test_data/simple.json", &:read)
HEADERS = { 'Content-Type': 'application/json' }
require 'benchmark'
NUMBER = 1000
CONCURRENCY = 100
def run_net_http
with_times_and_concurrency do
net = Net::HTTP.new(URI.parse(URL).host)
res = net.post('/split', POST_DATA, HEADERS)
raise if res.code.to_i != 200
end
end
def run_net_http_keepalive
with_times_and_concurrency do
Net::HTTP.start(URI.parse(URL).host) do |conn|
res = conn.post('/split', POST_DATA, HEADERS)
raise if res.code.to_i != 200
end
end
end
def run_typhoeus
with_times_and_concurrency do
Typhoeus.post(URL, body: POST_DATA, headers: HEADERS)
end
end
def with_times_and_concurrency
counter = 0
threads = []
mutex = Mutex.new
CONCURRENCY.times do |c|
threads << Thread.new do
while counter < NUMBER
mutex.synchronize { counter += 1 }
begin
yield
rescue
# print "E"
next
end
# print "."
end
end
end
threads.map(&:join)
puts "fin"
end
def benchmark(label)
startt = Time.now
yield
endt = Time.now
elapsed = endt - startt
puts "#{label}: #{NUMBER.to_f / elapsed} req/s"
sleep(5) # Give server time to rest
end
benchmark("Net::HTTP no keep alive") { run_net_http }
benchmark("Net::HTTP keep alive") { run_net_http_keepalive }
b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment