ruby ben_multi.rb
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby ben_multi.rb相关的知识,希望对你有一定的参考价值。
# ben_multi.rb
require 'benchmark'
require 'bigdecimal/math'
require 'typhoeus'
require 'em-http-request'
## 通过修改iterations控制请求数
iterations = 500
url = 'http://www.google.com/'
# 测试typhoeus
Benchmark.bmbm(20) do |bm|
bm.report('typhoeus-singleton') do
iterations.times do
request = Typhoeus::Request.new(url)
request.on_complete do |response|
## 处理结果
end
request.run
end
end
end
# 多线程
Benchmark.bmbm(10) do |bm|
hydra = Typhoeus::Hydra.hydra
bm.report('typhoeus-multi') do
iterations.times do
#p url
first_request = Typhoeus::Request.new(url)
first_request.on_complete do |response|
#p response
end
hydra.queue first_request
end
hydra.run
end
end
## em-http-request
Benchmark.bmbm(20) do |bm|
bm.report('em-http-request-singleton') do
iterations.times do
EventMachine.run {
http = EventMachine::HttpRequest.new(url).get
http.errback{ p 'ERROR'; EM.stop}
http.callback{
## p http.response
EventMachine.stop
}
}
end
end
end
## em-http-request
Benchmark.bmbm(20) do |bm|
bm.report('em-http-request-multi') do
EventMachine.run do
multi = EventMachine::MultiRequest.new
number = 0
iterations.times do
number += 1
multi.add number, EventMachine::HttpRequest.new(url).get
multi.callback do
multi.responses[
## 处理回复信息
number
]
EventMachine.stop
end
end
end
end
end
多线程测试结果:
循环100次,时间差不多:
Rehearsal --------------------------------------------------------
typhoeus-singleton 0.090000 0.050000 0.140000 ( 14.776684)
----------------------------------------------- total: 0.140000sec
user system total real
typhoeus-singleton 0.090000 0.040000 0.130000 ( 15.510564)
Rehearsal --------------------------------------------------
typhoeus-multi 0.170000 0.190000 0.360000 ( 3.720471)
----------------------------------------- total: 0.360000sec
user system total real
typhoeus-multi 0.140000 0.150000 0.290000 ( 3.872118)
Rehearsal -------------------------------------------------------------
em-http-request-singleton 0.280000 0.060000 0.340000 ( 14.191702)
---------------------------------------------------- total: 0.340000sec
user system total real
em-http-request-singleton 0.260000 0.060000 0.320000 ( 15.185372)
Rehearsal ---------------------------------------------------------
em-http-request-multi 0.250000 0.070000 0.320000 ( 4.049698)
------------------------------------------------ total: 0.320000sec
user system total real
em-http-request-multi 0.210000 0.060000 0.270000 ( 4.092637)
循环200次,时间差不多:
Rehearsal --------------------------------------------------------
typhoeus-singleton 0.220000 0.090000 0.310000 ( 27.345565)
----------------------------------------------- total: 0.310000sec
user system total real
typhoeus-singleton 0.180000 0.080000 0.260000 ( 26.062616)
Rehearsal --------------------------------------------------
typhoeus-multi 0.530000 0.590000 1.120000 ( 10.548426)
----------------------------------------- total: 1.120000sec
user system total real
typhoeus-multi 0.410000 0.530000 0.940000 ( 9.945984)
Rehearsal -------------------------------------------------------------
em-http-request-singleton 0.570000 0.110000 0.680000 ( 33.332747)
---------------------------------------------------- total: 0.680000sec
user system total real
em-http-request-singleton 0.540000 0.110000 0.650000 ( 28.981736)
Rehearsal ---------------------------------------------------------
em-http-request-multi 0.490000 0.170000 0.660000 ( 12.083304)
------------------------------------------------ total: 0.660000sec
user system total real
em-http-request-multi 0.490000 0.150000 0.640000 ( 10.417084)
循环500次,typhoeus快:
Rehearsal --------------------------------------------------------
typhoeus-singleton 0.330000 0.070000 0.400000 (205.598741)
----------------------------------------------- total: 0.400000sec
user system total real
typhoeus-singleton 0.290000 0.060000 0.350000 (211.323986)
Rehearsal --------------------------------------------------
typhoeus-multi 0.830000 0.210000 1.040000 ( 5.836144)
----------------------------------------- total: 1.040000sec
user system total real
typhoeus-multi 0.220000 0.140000 0.360000 ( 2.936126)
Rehearsal -------------------------------------------------------------
em-http-request-singleton 1.260000 0.220000 1.480000 (466.522993)
---------------------------------------------------- total: 1.480000sec
user system total real
em-http-request-singleton 1.270000 0.240000 1.510000 (492.234620)
Rehearsal ---------------------------------------------------------
em-http-request-multi 0.820000 0.350000 1.170000 ( 20.715418)
------------------------------------------------ total: 1.170000sec
user system total real
em-http-request-multi 0.670000 0.230000 0.900000 ( 6.991797)
循环1000次,typhoeus速度快多了:
Rehearsal --------------------------------------------------------
typhoeus-singleton 0.640000 0.140000 0.780000 (478.474353)
----------------------------------------------- total: 0.780000sec
user system total real
typhoeus-singleton 0.600000 0.120000 0.720000 (449.769972)
Rehearsal --------------------------------------------------
typhoeus-multi 0.850000 0.480000 1.330000 ( 7.703259)
----------------------------------------- total: 1.330000sec
user system total real
typhoeus-multi 0.650000 0.390000 1.040000 ( 8.215158)
Rehearsal -------------------------------------------------------------
em-http-request-singleton 2.510000 0.450000 2.960000 (1045.448796)
---------------------------------------------------- total: 2.960000sec
user system total real
em-http-request-singleton 2.460000 0.440000 2.900000 (949.633308)
Rehearsal ---------------------------------------------------------
em-http-request-multi 0.410000 0.170000 0.580000 (242.618425)
------------------------------------------------ total: 0.580000sec
user system total real
em-http-request-multi 0.370000 0.170000 0.540000 (246.199577)
以上是关于ruby ben_multi.rb的主要内容,如果未能解决你的问题,请参考以下文章
Ruby 25 岁了!Ruby 之父说 Ruby 3 有望 3 倍提速