|
| 1 | +require 'rubygems' if RUBY_VERSION < '1.9' |
| 2 | +require 'bundler' |
| 3 | + |
| 4 | +Bundler.require(:default) |
| 5 | +Bundler.require(:benchmark) |
| 6 | + |
| 7 | +require 'benchmark' |
| 8 | +require 'net/http' |
| 9 | +require 'open-uri' |
| 10 | +require 'uri' |
| 11 | + |
| 12 | +url = ARGV[0] || "http://localhost:8080/" # nginx default |
| 13 | +uri = URI.parse(url) |
| 14 | +iters = (ARGV[1] || 1000).to_i |
| 15 | + |
| 16 | +Benchmark.bmbm do |x| |
| 17 | + x.report('em-http-request') do |
| 18 | + iters.times do |
| 19 | + EventMachine.run { |
| 20 | + http = EventMachine::HttpRequest.new(url).get |
| 21 | + |
| 22 | + http.callback { |
| 23 | + http.response |
| 24 | + EventMachine.stop |
| 25 | + } |
| 26 | + } |
| 27 | + end |
| 28 | + end |
| 29 | + |
| 30 | + x.report('Excon') do |
| 31 | + iters.times do |
| 32 | + Excon.get(url).body |
| 33 | + end |
| 34 | + end |
| 35 | + |
| 36 | + excon = Excon.new(url) |
| 37 | + x.report('Excon (persistent)') do |
| 38 | + iters.times do |
| 39 | + excon.request(:method => 'get').body |
| 40 | + end |
| 41 | + end |
| 42 | + |
| 43 | + x.report('HTTParty') do |
| 44 | + iters.times do |
| 45 | + HTTParty.get(url).body |
| 46 | + end |
| 47 | + end |
| 48 | + |
| 49 | + x.report('Net::HTTP') do |
| 50 | + # Net::HTTP.get('localhost', '/data/1000', 9292) |
| 51 | + iters.times do |
| 52 | + Net::HTTP.start(uri.host, uri.port) {|http| http.get('/data/1000').body } |
| 53 | + end |
| 54 | + end |
| 55 | + |
| 56 | + Net::HTTP.start(uri.host, uri.port) do |http| |
| 57 | + x.report('Net::HTTP (persistent)') do |
| 58 | + iters.times do |
| 59 | + http.get('/data/1000').body |
| 60 | + end |
| 61 | + end |
| 62 | + end |
| 63 | + |
| 64 | + x.report('open-uri') do |
| 65 | + iters.times do |
| 66 | + open(url).read |
| 67 | + end |
| 68 | + end |
| 69 | + |
| 70 | + x.report('RestClient') do |
| 71 | + iters.times do |
| 72 | + RestClient.get(url) |
| 73 | + end |
| 74 | + end |
| 75 | + |
| 76 | + x.report('Typhoeus') do |
| 77 | + iters.times do |
| 78 | + Typhoeus::Request.get(url).body |
| 79 | + end |
| 80 | + end |
| 81 | + |
| 82 | + x.report('StreamlyFFI (Persistent)') do |
| 83 | + conn = StreamlyFFI::Connection.new |
| 84 | + iters.times do |
| 85 | + conn.get(url) |
| 86 | + end |
| 87 | + end |
| 88 | +end |
| 89 | + |
| 90 | +# +------------------------+----------+ |
| 91 | +# | tach | total | |
| 92 | +# +------------------------+----------+ |
| 93 | +# | em-http-request | 3.828347 | |
| 94 | +# +------------------------+----------+ |
| 95 | +# | Excon | 1.541997 | |
| 96 | +# +------------------------+----------+ |
| 97 | +# | Excon (persistent) | 1.454728 | |
| 98 | +# +------------------------+----------+ |
| 99 | +# | HTTParty | 2.551734 | |
| 100 | +# +------------------------+----------+ |
| 101 | +# | Net::HTTP | 2.342450 | |
| 102 | +# +------------------------+----------+ |
| 103 | +# | Net::HTTP (persistent) | 2.434209 | |
| 104 | +# +------------------------+----------+ |
| 105 | +# | open-uri | 2.898245 | |
| 106 | +# +------------------------+----------+ |
| 107 | +# | RestClient | 2.834506 | |
| 108 | +# +------------------------+----------+ |
| 109 | +# | Typhoeus | 1.828265 | |
| 110 | +# +------------------------+----------+ |
0 commit comments