#150
Feb 23, 2009

Rails Metal

Rails Metal is a way to bypass the standard Rails request process for a performance boost. In this episode you will learn how to shave off a few milliseconds using Metal.
Download (15.2 MB, 9:49)
alternative download for iPod & Apple TV (11.5 MB, 9:49)

Resources

script/generate metal processes_list
script/server -e production -d
ab -n 100 http://127.0.0.1:3000/processes/list
ab -n 100 http://127.0.0.1:3000/processes_list
# app/metal/processes_list.rb
require(File.dirname(__FILE__) + "/../../config/environment") unless defined?(Rails)

class ProcessesList
  def self.call(env)
    if env["PATH_INFO"] =~ /^\/processes_list/
      [200, {"Content-Type" => "text/html"}, [`ps -axcr -o "pid,pcpu,pmem,time,comm"`]]
    else
      [404, {"Content-Type" => "text/html"}, ["Not Found"]]
    end
  end
end
<!-- processes/index.html.erb -->
<pre id="processes">Gathering processes...</pre>

<%= periodically_call_remote :url => "/processes_list", :update => "processes", :frequency => 3, :method => :get %>

RSS Feed for Episode Comments 36 comments

1. Sascha Feb 23, 2009 at 00:29

Thanks Ryan for this screencast and congratulation for #150!


2. Willem van Bergen Feb 23, 2009 at 00:45

Again a great episode on performance tuning!

Determining what actions might benefit from metal should be determined by analysis of request times, to avoid the trap of premature optimization. You can use request-log-analyzer (wiki.github.com/wvanbergen/request-log-analyzer) to analyze the request times from your logs. (Full disclosure: I am one of the authors.)

Note that after you have rewritten an action using metal, it will NOT show up in the logs and therefore it will no longer be included in performance reports generated by request-log-analyzer.


3. Russ Jones Feb 23, 2009 at 01:21

Ryan, how Metal is different than Rack? Is Metal just Rails effort to be Rack compatible? Please excuse my ignorance.


4. Andreas Heim Feb 23, 2009 at 01:41

Great screencast Ryan. Very interesting topic, and a very good use case for Metal.


5. Roland Feb 23, 2009 at 02:55

Congratulations and a big Thank You! for your 150 railscasts. Great job!


6. Simon Feb 23, 2009 at 03:10

Great screencast... I've heard of metal before but this made me go and try it out. Thanks!


7. Casen Feb 23, 2009 at 03:54

I was testing a few things out using apache bench after I watched the episode, and perhaps something you should have mentioned was how to kill the detached server process. I never knew how to do this, and I ended up killing ruby and restarting it to kill the server listening on port 3000. If anyone does know how to do this off hand, I would love to know.

Thanks


8. Deger Feb 23, 2009 at 04:04

Haha~, I am refreshing the home page many times today for this episode. Great screencast! Thank you.


9. Sohdubom Feb 23, 2009 at 05:13

Rails Metal is a thin layer above Rack, just like Sinatra is, and Sinatra is doing this way before Rails Metal appeared ... Rails Metal like Sinatra is a thin DSL above Rack so you don't have to code pure Rack and instead can code a simplified language above Rack. What I don't get, is why someone would use Sinatra app inside a Rails app (see some sample on the web), since you can use Metal or better, pure Rack inside Rails.


10. Jon Feb 23, 2009 at 12:06

Hey Ryan, congratulations on episode 150. I've really appreciated these railscasts every week. Keep up the great work!


11. Eric Berry Feb 23, 2009 at 21:23

Hey Ryan,

You are truly the man! Thanks so much for all the work you do. It has really helped me over the past year, and I hope it will continue for years to come!


12. Tex Feb 24, 2009 at 03:51

Hi Ryan, many thanks for your wonderful screencast !

A simple question: is it possible to use metal for multithread execution or large upload handling ?


13. Trevor Turk Feb 24, 2009 at 10:58

Please make sure to read over this post before getting too involved with your metal :)

http://weblog.rubyonrails.org/2008/12/20/performance-of-rails-metal


14. RailsCasts Fan Feb 25, 2009 at 06:56

150 amazing screencasts ! A big thanks Ryan !


15. Navjeet Feb 25, 2009 at 11:51

Another Great episode! Keep up the good work.


16. Squiddhartha Feb 25, 2009 at 11:53

Ryan, with today's episode, I've just caught up on all the Railscasts -- well, except all the e-commerce ones because I don't do that. Yet.

Congratulations on 150 episodes, and thanks for doing this for two years! You've provided me with a much better insight into the way Rails operates, and I've incorporated at least a dozen tips from your casts into my daily coding.


17. James Feb 27, 2009 at 15:03

Ryan, I just want to say thank you for all your great work. Each week a great highlight. Please continue !!!
Thanks !!!


18. Macario Ortega Feb 28, 2009 at 02:31

Ryan, thanks for those excellent screencasts, I owe you too much.

I've a problem with my Rails 2.3 installation in development mode:

When I load a page in Safari I've get no response at all, nul, the source code is an empty page, the same for curl but in Firefox it displays allright.

Any sugestions?


19. Melvin Ram - BrainBank Mar 08, 2009 at 16:17

Here's the notes I took during this screencast:

RailsCasts Notes: #150 Rails Metal

* Bypass rails routing

* Simple request that happens frequently.

* to generate: ruby script/generate metal name_of_metal

* stored under app/metal/

* in the generated code, the self.call(env) method should return an array.
    1. response code ex. 404 or 200
    2. hash of header options
    3. Body content

* Won't be recorded in log file since it bypass that as well.

* When creating/changing a metal, you need to restart server (may change)

* If there is a conflict between Rails routing and metal, metal wins because it gets the request before rails has had a chance to process it.

* "ab -n 100 http://127.0.0.1:3000/processes/list" will run a benchmark that you can use to compare performance (just replace the url of page)

Originally at: http://railsnotes.wordpress.com/2009/03/08/rails-metal-screencast-by-railscasts/


20. ismael Celis Apr 20, 2009 at 08:57

Is there any special requirement to make metal apps work with Passenger? In my setup Mongrels work Ok but Passenger silently ignores metal apps.


21. Dimitrian Sep 22, 2009 at 10:15

Thanks may be it will be help me for my course


22. Enyasha Oct 07, 2009 at 01:07

Nice,great info...thanks for blog


23. buy skelaxin uk Jan 10, 2010 at 15:05

greg regergreg


24. obtain skelaxin Jan 10, 2010 at 15:09

dwdwd d wr3r werwefwe


25. freerealmscoins Jan 12, 2010 at 16:37

It is very good.I have learned how to use Rails Metal.Thank you.Have a good time.


26. wholesale nike shoes Jan 13, 2010 at 22:55

A very good article, I will always come in.


27. wholesale scarves Jan 13, 2010 at 22:55

Such a good article, caught my sympathy!
-


28. {{!COL5)) Jan 23, 2010 at 19:04

{{!COL4))


29. {{!COL5)) Jan 23, 2010 at 19:53

{{!COL4))


30. augmentin overnight delivery Jan 23, 2010 at 20:43

love this info i will read this more often


31. Nike Air Max 90 Jan 24, 2010 at 18:59

Thank you for sharing.Nice post.


32. javon Jan 25, 2010 at 22:38

Thank you for sharing.Nice post.


33. Kreditkarten Vergleich -einfach Jan 28, 2010 at 13:59

Mmhhh somehow i don't get it. lol


34. cheap adidas shoes Jan 31, 2010 at 19:11

Adidas Shoes Online Shop-Hot Selling Adidas Shoes & Cheap Adidas Shoes.
ki


35. buy drugs online Feb 18, 2010 at 13:15

great post here as always wroth checking like usual when i come here


36. Louis Vuitton handbags Mar 09, 2010 at 18:47

railscasts.com/episodes/150-rails-metal

Add your comment:

(SKIP THIS ONE)

(required)

(not shown)


(use pastie or gist for code)

sponsored by:
if you want to help:
required:
Get Quicktime Player
Give Back to Open Source