1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
|
# encoding: utf-8
# frozen_string_literal: true
require 'ethon'
require 'open-uri'
require 'patron'
require 'curb'
require 'net/http'
require 'cgi'
require 'benchmark/ips'
require_relative '../spec/support/server'
require_relative '../spec/support/localhost_server'
LocalhostServer.new(TESTSERVER.new, 3000)
LocalhostServer.new(TESTSERVER.new, 3001)
LocalhostServer.new(TESTSERVER.new, 3002)
url = 'http://localhost:3000/'.freeze
uri = URI.parse('http://localhost:3000/').freeze
ethon = Ethon::Easy.new(url: url)
patron = Patron::Session.new
patron_url = Patron::Session.new(base_url: url)
curb = Curl::Easy.new(url)
puts '[Creation]'
Benchmark.ips do |x|
x.report('String.new') { '' }
x.report('Easy.new') { Ethon::Easy.new }
end
puts '[Escape]'
Benchmark.ips do |x|
x.report('CGI.escape') { CGI.escape("まつもと") }
x.report('Easy.escape') { ethon.escape("まつもと") }
end
puts '[Requests]'
Benchmark.ips do |x|
x.report('net/http') { Net::HTTP.get_response(uri) }
x.report('open-uri') { open url }
x.report('patron') do
patron.base_url = url
patron.get('/')
end
x.report('patron reuse') { patron_url.get('/') }
x.report('curb') do
curb.url = url
curb.perform
end
x.report('curb reuse') { curb.perform }
x.report('Easy.perform') do
ethon.url = url
ethon.perform
end
x.report('Easy.perform reuse') { ethon.perform }
end
puts "[ 4 delayed Requests ]"
Benchmark.ips do |x|
x.report('net/http') do
3.times do |i|
uri = URI.parse("http://localhost:300#{i}/?delay=1")
Net::HTTP.get_response(uri)
end
end
x.report("open-uri") do
3.times do |i|
open("http://localhost:300#{i}/?delay=1")
end
end
x.report("patron") do
sess = Patron::Session.new
3.times do |i|
sess.base_url = "http://localhost:300#{i}/?delay=1"
sess.get("/")
end
end
x.report("Easy.perform") do
easy = Ethon::Easy.new
3.times do |i|
easy.url = "http://localhost:300#{i}/?delay=1"
easy.perform
end
end
x.report("Multi.perform") do
multi = Ethon::Multi.new
3.times do |i|
easy = Ethon::Easy.new
easy.url = "http://localhost:300#{i}/?delay=1"
multi.add(easy)
end
multi.perform
end
end
|