Skip to content

Instantly share code, notes, and snippets.

@cashplk
Last active December 26, 2015 15:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cashplk/7173445 to your computer and use it in GitHub Desktop.
Save cashplk/7173445 to your computer and use it in GitHub Desktop.
##单线程测试代码:
## ben_singleton.rb
require 'benchmark'
require 'bigdecimal/math'
require 'rest-client'
require 'httparty'
require 'faraday'
require 'mechanize'
## 通过修改iterations控制请求数
iterations = 500
url = 'http://www.baidu.com/'
# 测试rest-client
Benchmark.bmbm(10) do |bm|
bm.report('rest-client') do
iterations.times do
response = RestClient.get url
end
end
end
# 测试httpary
Benchmark.bmbm(10) do |bm|
bm.report('httparty') do
iterations.times do
response = HTTParty.get(url)
end
end
end
## 测试faraday
Benchmark.bmbm(10) do |bm|
bm.report('faraday') do
iterations.times do
response = Faraday.get url
end
end
end
## 测试Mechanize
Benchmark.bmbm(10) do |bm|
bm.report('Mechanize') do
iterations.times do
response = Mechanize.new.get(url)
end
end
end
单线程测试结果:
循环次数10次,Mechanize速度最快:
Rehearsal -----------------------------------------------
rest-client 0.010000 0.000000 0.010000 ( 0.674061)
-------------------------------------- total: 0.010000sec
user system total real
rest-client 0.020000 0.010000 0.030000 ( 0.762231)
Rehearsal ----------------------------------------------
httparty 0.010000 0.010000 0.020000 ( 0.770611)
------------------------------------- total: 0.020000sec
user system total real
httparty 0.020000 0.000000 0.020000 ( 0.643453)
Rehearsal ----------------------------------------------
faraday 0.020000 0.010000 0.030000 ( 0.744331)
------------------------------------- total: 0.030000sec
user system total real
faraday 0.020000 0.010000 0.030000 ( 0.703317)
Rehearsal ----------------------------------------------
Mechanize 0.040000 0.000000 0.040000 ( 0.419765)
------------------------------------- total: 0.040000sec
user system total real
Mechanize 0.030000 0.000000 0.030000 ( 0.435737)
循环次数100次,Mechanize依然速度最快:
Rehearsal -----------------------------------------------
rest-client 0.240000 0.050000 0.290000 ( 7.440236)
-------------------------------------- total: 0.290000sec
user system total real
rest-client 0.230000 0.060000 0.290000 ( 7.236058)
Rehearsal ----------------------------------------------
httparty 0.200000 0.050000 0.250000 ( 6.777424)
------------------------------------- total: 0.250000sec
user system total real
httparty 0.210000 0.060000 0.270000 ( 6.863603)
Rehearsal ----------------------------------------------
faraday 0.210000 0.060000 0.270000 ( 7.376543)
------------------------------------- total: 0.270000sec
user system total real
faraday 0.220000 0.050000 0.270000 ( 6.731503)
Rehearsal ----------------------------------------------
Mechanize 0.410000 0.030000 0.440000 ( 4.626423)
------------------------------------- total: 0.440000sec
user system total real
Mechanize 0.390000 0.020000 0.410000 ( 4.363442)
循环次数200次,Mechanize还是最快:
Rehearsal -----------------------------------------------
rest-client 0.510000 0.120000 0.630000 ( 16.537392)
-------------------------------------- total: 0.630000sec
user system total real
rest-client 0.500000 0.110000 0.610000 ( 18.882200)
Rehearsal ----------------------------------------------
httparty 0.520000 0.120000 0.640000 ( 16.653378)
------------------------------------- total: 0.640000sec
user system total real
httparty 0.490000 0.120000 0.610000 ( 15.664201)
Rehearsal ----------------------------------------------
faraday 0.500000 0.110000 0.610000 ( 16.269004)
------------------------------------- total: 0.610000sec
user system total real
faraday 0.500000 0.110000 0.610000 ( 16.950023)
Rehearsal ----------------------------------------------
Mechanize 0.890000 0.050000 0.940000 ( 9.062458)
------------------------------------- total: 0.940000sec
user system total real
Mechanize 0.850000 0.040000 0.890000 ( 8.620533)
循环次数500次,出错:
.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/net/protocol.rb:153:in `read_nonblock': Connection reset by peer (Errno::ECONNRESET)
---
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment