【前沿生活】Rails Metal - 让Rails2.3很好很强大

本文介绍RailsMetal作为EdgeRails的一部分如何显著提升Rails应用性能。通过使用Rack中间件,RailsMetal可以实现比传统控制器快2.8倍的速度。文章提供了一个简单的Hello World示例并进行了基准测试比较。
Rails Metal是Edge Rails的新特性。最近的Rails core team的工作是用rack取代了Rails老的request处理代码,并且集成了中间件的支持。Rails Meta允许你的Rails 应用使用Rack中间件来创建一个超级快的action。 


无耻的转载 国外同行 写的简单的Rails Metal的hello word例子:
InBlock.gif class Poller < Rails::Rack::Metal
InBlock.gif        def call(env)
InBlock.gif             if env[ "PATH_INFO"] =~ /^\/poller/
InBlock.gif                [[200], { "Content-Type" => "text/html"}, "Hello, World!"]
InBlock.gif             else
InBlock.gif                [[404], { "Content-Type" => "text/html"}, "Not Found"]
InBlock.gif            end
InBlock.gif        end
InBlock.gif    end

对比一下老的controller:
InBlock.gif class OldPollerController < ApplicationController
InBlock.gif            def poller
InBlock.gif                render :text => "Hello World!"
InBlock.gif            end
InBlock.gif        end

看一下两者的差距:
InBlock.gif# first, let's benchmark the traditional controller
InBlock.gif    $ ab -n 1000 http: //127.0.0.1:3000/old_poller/poller
InBlock.gif    ... snip ...
InBlock.gif    Requests per second:        408.45 [#/sec] (mean)
InBlock.gif    Time per request:             2.448 [ms] (mean)
InBlock.gif
    # now for the Metal middleware
InBlock.gif    $ ab -n 1000 http: //127.0.0.1:3000/poller
InBlock.gif    ... snip ...
InBlock.gif    Requests per second:        1154.66 [#/sec] (mean)
InBlock.gif    Time per request:             0.866 [ms] (mean)

看吧,Rails Metal 的性能要比老版本的controller快2.8倍 。。。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值