Slide 1

Slide 1 text

Ruby HTTP clients comparison Hiroshi Nakamura nahi at Twitter, github Technical Architect at Appirio Japan CRuby and JRuby committer Asakusa.rb: http://qwik.jp/asakusarb/

Slide 2

Slide 2 text

Ruby HTTP Clients Matrix advantages-and-disadvantages comparison of HTTP client libraries http://bit.ly/RubyHTTPClients2012 Disclaimer: I'm the author of "httpclient"

Slide 3

Slide 3 text

Agenda net/http 16 libraries I picked Ruby HTTP Clients Matrix API style Compatibility Supported features Performance Comparisons My Recommendations

Slide 4

Slide 4 text

net/http Net::HTTP::Proxy? net/https?

Slide 5

Slide 5 text

Ruby HTTP Client libraries

Slide 6

Slide 6 text

HTTP client libraries I didn’t evaluate Cannot evaluate • activeresource (Rails specific) • http (under development) • http_request.rb (test doesn't pass) • nestful (no test) • typhoeus (under heavy rewrite) Obsolete • eventmachine (built-in client is obsolete) • right_http_connection (no update) • simplehttp (no update) • rfuzz (no update)

Slide 7

Slide 7 text

Evaluation Axis Project Stats API style Compatibility: CRuby, JRuby, Rubinius Supported features Connection features Basic HTTP features Development support Advanced features http://bit.ly/RubyHTTPClientsFeatureTest (test/unit scripts)

Slide 8

Slide 8 text

Project Stats

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

Project Stats

Slide 13

Slide 13 text

API style

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

sync API (1/5) - Client instance net/http, mechanize, httpclient, patron, curb, faraday client = Net::HTTP.new(host, port) p client.get(path).body client = Mechanize.new client = HTTPClient.new client = Patron::Session.new p client.get(url).body curl = Curl::Easy.new(url) curl.http_get p curl.body_str client = Faraday.new(:url => baseurl) p client.get(path).body

Slide 16

Slide 16 text

sync API (2/5) - Client class restfulie, excon, httpi p Restfulie.at(url).get!.body p Excon.get(url).body p HTTPI.get(url).body

Slide 17

Slide 17 text

sync API (3/5) - Resource rest-client, rufus-verbs rs = RestClient::Resource.new('http://example.com') rs['posts/1/comments'].post 'Good article.', :content_type => 'text/plain' ep = EndPoint.new( :host => "resta.farian.host", :port => 80, :resource => "inventory/tools") res = ep.get :id => 1 res = ep.get :id => 2

Slide 18

Slide 18 text

sync API (4/5) - Include & Customize httparty, weary client = Class.new { include HTTParty } p client.get(url) class WearyClient < Weary::Client domain 'http://api.target.org/' get :retrieve, '{path}' end p WearyClient.new.fetch(:path => path).perform.body

Slide 19

Slide 19 text

sync API (5/5) - Others open-uri.rb, wrest # open-uri.rb p open(url) { |f| f.read } # wrest p 'http://www.google.co.jp/'.to_uri.get.body

Slide 20

Slide 20 text

async API(1/2) - Callback em-http-request body = nil EM.run do req = EM::HttpRequest.new( 'http://www.google.com/').get req.callback do body = req.response EM.stop end req.errback do body = nil end end p body

Slide 21

Slide 21 text

async API(2/2) - Polling httpclient, weary, wrest client = HTTPClient.new conn = client.get_async(url) conn.finished? # => false # ... io = conn.pop.content while str = io.read(4096) p str end

Slide 22

Slide 22 text

parallel API - curb responses = [] m = Curl::Multi.new urls.each do |url| responses[url] = '' m.add(Curl::Easy.new(url) { |curl| curl.on_body { |data| responses[url] << data data.bytesize } }) end m.perform p responses.map { |e| e.bytesize }

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

API style

Slide 26

Slide 26 text

Compatibility

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

Compatibility

Slide 29

Slide 29 text

Connection features

Slide 30

Slide 30 text

No content

Slide 31

Slide 31 text

3 types of HTTP connections

Slide 32

Slide 32 text

No content

Slide 33

Slide 33 text

Keep-Alive in em-http-request body = [] EM.run do conn = EventMachine::HttpRequest.new(server.url) req1 = conn.get(:keepalive => true) req1.callback { body << req1.response req2 = conn.get(:keepalive => true) req2.callback { body << req2.response req3 = conn.get(:keepalive => true) req3.callback { body << req3.response req4 = conn.get(:keepalive => true) req4.callback { body << req4.response EM.stop req4.errback { ... }} req3.errback { ... }} req2.errback { ... }} req1.errback { ... } end

Slide 34

Slide 34 text

Pipelining in em-http-request body = [] EM.run do conn = EventMachine::HttpRequest.new(server.url) req1 = conn.get(:keepalive => true) req2 = conn.get(:keepalive => true) req3 = conn.get(:keepalive => true) req4 = conn.get() req1.callback { body << req1.response } req2.callback { body << req2.response } req3.callback { body << req3.response } req4.callback { body << req4.response; EM.stop } req1.errback { ... } req2.errback { ... } req3.errback { ... } req4.errback { ... } end

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

NO verification by default?! options[:ssl_ca_file] == nil => VERIFY_NONE if http.use_ssl? http.verify_mode = OpenSSL::SSL::VERIFY_NONE if options[:ssl_ca_file] http.ca_file = options[:ssl_ca_file] http.verify_mode = OpenSSL::SSL::VERIFY_PEER end end

Slide 37

Slide 37 text

No content

Slide 38

Slide 38 text

No content

Slide 39

Slide 39 text

No content

Slide 40

Slide 40 text

Connection features

Slide 41

Slide 41 text

Basic HTTP features

Slide 42

Slide 42 text

No content

Slide 43

Slide 43 text

No content

Slide 44

Slide 44 text

IRI: Internationalized Resource Identifier uri.rb doesn't support IRI addressable/uri does client.get("http://www.ebooks.com/797059/some-kind- of-peace/grebe-camilla-träff-åsa-norlen-paul/")

Slide 45

Slide 45 text

No content

Slide 46

Slide 46 text

"Cross-site Cooking" bug in httpclient httpclient eats this cookie and send it to all *.com site Mechanize handles it properly like browsers http://en.wikipedia.org/wiki/Cross-site_cooking Set-Cookie: TEST=test; path=/; domain=.com

Slide 47

Slide 47 text

No content

Slide 48

Slide 48 text

No content

Slide 49

Slide 49 text

No content

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

Streaming upload/download # Chunked upload with Patron client = Patron::Session.new res = client.request(:post, url, {}, :file => path_to_upload) # Chunked download client.get_file(url, path_to_write) # Chunked upload with em-http-request req = EM::HttpRequest.new(url).post :file => path_to_upload # Chunked download req = EM::HttpRequest.new(url).get req.stream do |chunk| p chunk end

Slide 52

Slide 52 text

No content

Slide 53

Slide 53 text

No content

Slide 54

Slide 54 text

Basic HTTP features

Slide 55

Slide 55 text

Development Support

Slide 56

Slide 56 text

No content

Slide 57

Slide 57 text

Response stubbing # Stubbing response body client = HTTPClient.new client.test_loopback_response << 'Hello!' client.get('http://www.example.com/hello').body #=> "Hello!" # Stubbing HTTP response client.test_loopback_http_response << "HTTP/1.0 302 Found¥r¥nLocation: http://foo/¥r¥n¥r¥n" << "HTTP/1.0 200 OK¥r¥n¥r¥nHello!" client.post('http://www.example.com/todo', :follow_redirect => true, :body => '{}').body #=> "Hello!"

Slide 58

Slide 58 text

No content

Slide 59

Slide 59 text

No content

Slide 60

Slide 60 text

IRB like shell rest-client, httpclient, wrest % restclient https://example.com user pass >> delete '/private/resource' % httpclient >> get "https://www.google.com", :q => :ruby % wrest >> 'http://www.google.com?q=ruby'.to_uri.get

Slide 61

Slide 61 text

Replayable log rest-client % RESTCLIENT_LOG=/tmp/restclient.log restclient >> RestClient.get "https://www.google.com/" ... % cat /tmp/restclient.log RestClient.get "https://www.google.com/", "Accept"=>"*/*; q=0.5, application/xml", "Accept-Encoding"=>"gzip, deflate" # => 200 OK | text/html 13354 bytes

Slide 62

Slide 62 text

No content

Slide 63

Slide 63 text

Development Support

Slide 64

Slide 64 text

Advanced features

Slide 65

Slide 65 text

No content

Slide 66

Slide 66 text

No content

Slide 67

Slide 67 text

No content

Slide 68

Slide 68 text

No content

Slide 69

Slide 69 text

HTML form handling of Mechanize agent = Mechanize.new page = agent.get(url) form = page.form('login') form.email = '[email protected]' form.password = 'jKH.P945wruV*qh3' page = agent.submit(form, form.button('submit'))

Slide 70

Slide 70 text

Advanced features

Slide 71

Slide 71 text

Testing your client webmock by Bartosz Blimke (bblimke) Library for stubbing and setting expectations on HTTP requests in Ruby. vcr by Myron Marston (myronmarston) Record your test suite's HTTP interactions and replay them during future test runs for fast, deterministic, accurate tests.

Slide 72

Slide 72 text

Performance Comparisons Server Linode Xen VPS (Linode 512) at Fremont, CA Ubuntu 10.10 Apache 2.2, KeepAlive On Client AWS EC2 (m1.small) at North Virginia (us-east-1b) Ubuntu 12.04 HTTP clients w/ CRuby 1.9.3p286 Multiple downloads of 177B.html and 24MB.zip Don't take it serious! http://bit.ly/RubyHTTPClientsBenchmarkScript

Slide 73

Slide 73 text

Multiple 177B downloads [sec]

Slide 74

Slide 74 text

[sec]

Slide 75

Slide 75 text

[sec]

Slide 76

Slide 76 text

My Recommendations • Speed is the king => em-http-request, curb w/ multi • HTML operation, Cookies => Mechanize • API client => Faraday and adapter based impls • SSL, Connectivity => httpclient Check the matrix before you use the libraries Please let me know when you find incorrect cell

Slide 77

Slide 77 text

Development Timeline