Rails Metal是Edge Rails的新特性。最近的Rails core team的工作是用rack取代了Rails老的request处理代码,并且集成了中间件的支持。Rails Meta允许你的Rails 应用使用Rack中间件来创建一个超级快的action。
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
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)









对比一下老的controller:





看一下两者的差距:






# now for the Metal middleware




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