Created
September 5, 2016 23:55
-
-
Save sschepens/a8d1afdad979ec533402ceeea44ab1d0 to your computer and use it in GitHub Desktop.
Faraday concurrency test of adapters
This file contains hidden or 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
require 'webrick' | |
require 'faraday' | |
require 'em-http' | |
require 'net/http/persistent' | |
require 'httpclient' | |
require 'typhoeus' | |
require 'typhoeus/adapters/faraday' | |
require 'patron' | |
require 'excon' | |
adapters = [:net_http, :net_http_persistent, :httpclient, :em_http, :em_synchrony, :typhoeus, :patron, :excon] | |
requests = 0 | |
server = WEBrick::HTTPServer.new Port: 8000, | |
AccessLog: [] | |
server.mount_proc '/' do |req, res| | |
requests += 1 | |
sleep 3 | |
res.body = 'Hello, world!' | |
end | |
server_thread = Thread.new do | |
server.start | |
end | |
adapters.each do |a| | |
requests = 0 | |
conn = Faraday.new(url: 'http://localhost:8000') do |f| | |
f.adapter a | |
end | |
puts "Testing adapter #{a}" | |
threads = 10.times.collect do |i| | |
sleep 0.01 | |
Thread.new do | |
start = Time.now | |
begin | |
res = conn.get('/') | |
rescue Exception => e | |
puts "Exception #{e.message}" | |
end | |
puts "Thread #{i} took #{Time.now - start}" | |
end | |
end | |
threads.map(&:join) | |
puts "received #{requests} requests" | |
end | |
server.stop | |
server_thread.join |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment