Rack Middleware, Rails Metal, Rails Controller简单性能比较

[b]1 rack middleware中间件[/b]:

文件位于app/racks/irack.rb

class Irack
def initialize(app)
@app = app
end
def call(env)
request = Rack::Request.new env
if request.path_info == '/i/kitty'
[200, {}, ['i kitty']]
else
@app.call(env)
end
end
end

修改config.ru,在run Blog::Application上面添加下面代码

require ::File.expand_path('../app/racks/irack.rb', __FILE__)
use Irack


[b]2 rails3 metal [/b]:
文件位于app/controllers/hi_controller.rb

class HiController < ActionController::Metal
def kitty
self.response_body = ['hi kitty']
end
end

路由配置, 修改config/routes.rb,添加下面代码

get "hi/kitty"


[b]3 rails controller [/b]:
在终端输入: [quote]rails g controller hello kitty[/quote]
在kitty方法代码

render :text => "hello kitty", :layout => nil


测试,ab一千个请求
[quote]
ab -n 1000 http://localhost:3000/i/kitty
ab -n 1000 http://localhost:3000/hi/kitty
ab -n 1000 http://localhost:3000/hello/kitty
[/quote]

[b]测试结果[/b]:
[table]
|方式|一千请求总时间|平均一个请求的时间(ms)|
|rack|2.225 seconds|2.225|
|rails3 metal|8.875 seconds|8.875|
|rails3 controller|19.048 seconds|19.048|
[/table]

从测试结果来说,rack中间件性能无疑是最好的,但难维护.
metal相对controller来说,在追求性能的时候,是一个不错的选择
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值