您的位置:首页 > 大数据 > 人工智能

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

2008-12-21 03:35 281 查看
Rails Metal是Edge Rails的新特性。最近的Rails core team的工作是用rack取代了Rails老的request处理代码,并且集成了中间件的支持。Rails Meta允许你的Rails 应用使用Rack中间件来创建一个超级快的action。 

无耻的转载国外同行写的简单的Rails Metal的hello word例子:

class Poller < Rails::Rack::Metal


        def call(env)


            if env["PATH_INFO"] =~ /^\/poller/


                [[200], {"Content-Type" => "text/html"}, "Hello, World!"]


            else


                [[404], {"Content-Type" => "text/html"}, "Not Found"]


            end


        end


    end
对比一下老的controller:

class OldPollerController < ApplicationController


            def poller


                render :text => "Hello World!"


            end


        end
看一下两者的差距:

# first, let's benchmark the traditional controller


    $ ab -n 1000 http://127.0.0.1:3000/old_poller/poller

    ... snip ...


    Requests per second:        408.45 [#/sec] (mean)


    Time per request:             2.448 [ms] (mean)



    # now for the Metal middleware


    $ ab -n 1000 http://127.0.0.1:3000/poller

    ... snip ...


    Requests per second:        1154.66 [#/sec] (mean)


    Time per request:             0.866 [ms] (mean)
看吧,Rails Metal 的性能要比老版本的controller快2.8倍 。。。

本文出自 “悟道集” 博客,请务必保留此出处http://blackanger.blog.51cto.com/140924/121460
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: