Skip to content

Instantly share code, notes, and snippets.

@brucek
Created November 17, 2016 01:36
Show Gist options
  • Save brucek/2f8e85bba9a59463f6d6ccc7f31d8831 to your computer and use it in GitHub Desktop.
Save brucek/2f8e85bba9a59463f6d6ccc7f31d8831 to your computer and use it in GitHub Desktop.
Ruby HTTP gems quick benchmarking via Faraday
#!/usr/bin/env ruby
require 'bundler'
# Bundler.require(:default)
# Bundler.require(:benchmark)
require 'faraday'
require 'excon'
require 'em-http-request'
require 'httparty'
require 'net/http'
require 'net/http/persistent'
require 'open-uri'
require 'rest-client'
require 'tach'
require 'typhoeus'
require 'patron'
require "em-synchrony"
require "em-synchrony/em-http"
require 'benchmark'
require 'benchmark/ips'
def setup_conn(adapter=Faraday.default_adapter, url="https://www.google.com")
Faraday.new(url: url) do |faraday|
faraday.adapter adapter # make requests with Net::HTTP
end
end
Benchmark.ips do |x|
conn = setup_conn :net_http
x.report('Net::HTTP') do
conn.get('/').body
end
conn = setup_conn :net_http_persistent
x.report('Net::HTTP::Persistent') do
conn.get('/').body
end
conn = setup_conn :typhoeus
x.report('Typhoeus') do
conn.get('/', {accept_encoding: "gzip", followlocation: true}).body
end
conn = setup_conn :patron
x.report('Patron') do
conn.get('/').body
end
conn = setup_conn :em_synchrony
x.report('em_synchrony') do
conn.get('/').body
end
conn = setup_conn :em_http
x.report('em_http') do
conn.get('/').body
end
conn = setup_conn :excon
x.report('excon') do
conn.get('/').body
end
conn = setup_conn :rack
x.report('rack') do
conn.get('/').body
end
conn = setup_conn :httpclient
x.report('httpclient') do
conn.get('/').body
end
x.compare!
end
Warming up --------------------------------------
Net::HTTPCookie#domain returns dot-less domain name now. Use Cookie#dot_domain if you need "." at the beginning.
1.000 i/100ms
Net::HTTP::Persistent
1.000 i/100ms
Typhoeus 1.000 i/100ms
Patron 1.000 i/100ms
em_synchrony 1.000 i/100ms
em_http 1.000 i/100ms
excon 1.000 i/100ms
rack 1.000 i/100ms
httpclient 1.000 i/100ms
Calculating -------------------------------------
Net::HTTP 7.946 (±12.6%) i/s - 39.000 in 5.073157s
Net::HTTP::Persistent
7.701 (±13.0%) i/s - 38.000 in 5.030249s
Typhoeus 7.978 (±12.5%) i/s - 40.000 in 5.068402s
Patron 6.912 (±43.4%) i/s - 15.000 in 6.935920s
em_synchrony 0.505 (± 0.0%) i/s - 3.000 in 6.034548s
em_http 0.375 (± 0.0%) i/s - 2.000 in 5.364837s
excon 0.463 (± 0.0%) i/s - 3.000 in 6.530681s
rack 0.603 (± 0.0%) i/s - 3.000 in 5.428011s
httpclient 0.521 (± 0.0%) i/s - 3.000 in 6.067653s
Comparison:
Typhoeus: 8.0 i/s
Net::HTTP: 7.9 i/s - same-ish: difference falls within error
Net::HTTP::Persistent: 7.7 i/s - same-ish: difference falls within error
Patron: 6.9 i/s - same-ish: difference falls within error
rack: 0.6 i/s - 13.22x slower
httpclient: 0.5 i/s - 15.30x slower
em_synchrony: 0.5 i/s - 15.79x slower
excon: 0.5 i/s - 17.24x slower
em_http: 0.4 i/s - 21.29x slower
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment