Skip to content

Instantly share code, notes, and snippets.

@pixeltrix
Created September 4, 2015 14:25
Show Gist options
  • Save pixeltrix/3f499bfda07c8cbb9d26 to your computer and use it in GitHub Desktop.
Save pixeltrix/3f499bfda07c8cbb9d26 to your computer and use it in GitHub Desktop.
Compare different Faraday adapters against the parliament postcode to constituency API
begin
require 'bundler/inline'
rescue LoadError => e
$stderr.puts 'Bundler version 1.10 or later is required. Please update your Bundler'
raise e
end
gemfile(true) do
source 'https://rubygems.org'
gem 'faraday', '0.9.1'
gem 'faraday_middleware', '0.10.0'
gem 'net-http-persistent', '2.9.4'
gem 'httpclient', '2.6.0.1'
gem 'patron', '0.4.20'
gem 'em-http-request', '1.1.2'
gem 'em-synchrony', '1.0.4'
gem 'excon', '0.45.4'
gem 'benchmark-ips', '2.3.0'
end
require 'benchmark/ips'
API_HOST = 'http://data.parliament.uk'
API_ENDPOINT = '/membersdataplatform/services/mnis/Constituencies/CV66PS/'
def configure_faraday(conn, adapter)
conn.response :follow_redirects
conn.response :raise_error
conn.adapter adapter
end
def faraday_net_http
@faraday_net_http ||= Faraday.new(API_HOST) { |conn| configure_faraday(conn, :net_http) }
end
def faraday_net_http_persistent
@faraday_net_http_persistent ||= Faraday.new(API_HOST) { |conn| configure_faraday(conn, :net_http_persistent) }
end
def faraday_httpclient
@faraday_httpclient ||= Faraday.new(API_HOST) { |conn| configure_faraday(conn, :httpclient) }
end
def faraday_patron
@faraday_patron ||= Faraday.new(API_HOST) { |conn| configure_faraday(conn, :patron) }
end
def faraday_em_synchrony
@faraday_em_synchrony ||= Faraday.new(API_HOST) { |conn| configure_faraday(conn, :em_synchrony) }
end
def faraday_excon
@faraday_excon ||= Faraday.new(API_HOST) { |conn| configure_faraday(conn, :excon) }
end
Benchmark.ips do |x|
x.report("Net::HTTP ") { faraday_net_http.get(API_ENDPOINT) }
x.report("Net::HTTP::Persistent") { faraday_net_http_persistent.get(API_ENDPOINT) }
x.report("HTTPClient ") { faraday_httpclient.get(API_ENDPOINT) }
x.report("Patron ") { faraday_patron.get(API_ENDPOINT) }
x.report("EventMachine ") { faraday_em_synchrony.get(API_ENDPOINT) }
x.report("Excon ") { faraday_excon.get(API_ENDPOINT) }
x.compare!
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment