Rails Performance Test

Filed under:rails — posted by admin on July 19, 2009 @ 2:46 pm
  • Install ruby-prof gem

  • gem install ruby-prof

  • Prepare fixtures test/fixtures/xxx for testing. Test one by one as the following:

  • ruby test/unit/xxx_test.rb

  • Edit test/performance/browsing_test.rb

  • require 'test_helper'
    require 'performance_test_help'

    # Profiling results for each test method are written to tmp/performance.
    class BrowsingTest < ActionController::PerformanceTest
    def test_homepage
    get '/'
    end
    end

  • Benchmarking

  • rake test:benchmark


    Started
    BrowsingTest#test_homepage (1062 ms warmup)
    process_time: 406 ms
    memory: 0.00 KB
    objects: 0
    gc_runs: 0
    gc_time: 0 ms
    .
    Finished in 10.501 seconds.

    5 tests, 0 assertions, 0 failures, 0 errors

  • Profiling

  • rake test:profile


    Started
    BrowsingTest#test_homepage (858 ms warmup)
    process_time: 655 ms
    memory: unsupported
    objects: unsupported
    .
    Finished in 32.845 seconds.

    3 tests, 0 assertions, 0 failures, 0 errors

Sending data from rails to javascript using JSON

Filed under:rails — posted by admin on January 6, 2008 @ 12:38 am

RJS templates are a beauty, really. They allow you to run javascript code generated by rails, to be executed on the browser. Usually, this javascript code affects the DOM. But RJS, as it is often used now, is for affecting the look and behavior of a page with javascript. What if you have data that needs to be transferred from Ruby on Rails to the Javascript in the browser?

Well, my first inkling was to write a helper function that converted a matrix in Ruby, one by one, to a matrix in Javascript. But it felt ugly, like specialized complexity that’s just in your face. There has to be a better way, since I can’t be the first one to run into this problem.

Of course, there is and that’s using JSON–JavaScript Object Notation. A simple way to think of it is as XML, with less overhead, because JSON is simply that, a way to represent data in a standard format that’s easy for both humans to read and machines to parse.

In Rails 1.1+ (apparently, this has been around since at least May 2006), there’s been an extension added to Object class, and that’s to_json(). That’s right. You can serialize any object in rails to JSON, simply by calling to_json(). However, it’s not documented in the Rails API, so your best bet is to google for it.

As a quick and dirty way to demonstrate it, you can have this in your controller:

def some_action
# some type of data from somewhere in the database
@data = Data.find(1)
end

and then the following in your view somewhere:

You should be able to create helper functions that generate the javascript tags and the enclosing javascript so that it’d be a bit prettier, so it’d look like this in your view instead:

<%= act_on_data(@data) %>

Neat. So in this way, you can pass your data from your server to code in your views that isn’t to be displayed in HTML, but in some other form. One person has tied Adobe’s Flex in with Rails for this very purpose. Tip!

But one other thing that I realized after reading through JSON for the masses, is somewhat startling…at least to me. And in hindsight, perhaps it was because it never seemed easy to do in XML.

Usually data transfer languages such as XML and the like are declarative languages. They say the “what” instead of the “how”. That hasn’t stopped people from trying to do remote procedure calls with XML-RPC. But with JSON, it really is an object notation, meaning that you can put functions as data. So the example I gave before of javascript classes, they can instead be written in JSON notion in the source code and it would still work.

Therefore, the javascript interpreter itself IS the parser for JSON. You don’t need a separate parser in javascript like you do in XML. If JSON really does have cross-language compatibility, that’s actually rather neat. This implies that JSON-RPC is possible, and of course, a quick search on Google reveals that this is exactly what people are trying.

This kind of reminds me of how Von Neumann’s computer architecture was novel at the time because it treated program commands and data as the same thing. Before, computers had one place for data, and one place for a program. You didn’t store programs the same place that you stored data. By the same token, it also reminds me of how Lisp’s data is pretty much its code, and vice versa, and that’s where it derives a lot of its power.

Looking into the future (meaning, here on out, I’m talking out of my ass), perhaps that makes it easier to do distributed computing, and the stitching together of applications so very much desired by the Service Orientated Architecture crowd, but also a host of security issues. You might be able to get javascript programs that update each other on the fly in the field, instead of going all the way back to the server for updates (maybe).

Rails and relative_url_root

Filed under:rails — posted by admin on December 30, 2007 @ 7:31 am

And add a line at the end of config/environment.rb

ActionController::AbstractRequest.relative_url_root = "/netvibez"  

 

What this does is change the Rails application to run starting with a netvibez in the first part of the URL. It is useful!!!

For example, we need this when we develop facebook application. It is the canvas_path (http://apps.facebook.com/netvibez)

Facebook, RFacebook, Rails and url_for

Filed under:rails — posted by admin on December 29, 2007 @ 2:47 pm

It caused me trouble to use url_for when developing applications on facebook with RFacebook. After tracing the source code both in RFacebook and Rails; I found I didn’t configure facebook.yml correctly and didn’t use url_for(is aliased by url_for__RFACEBOOK) correctly. Please take a look at the source code lib/rfacebook_on_rails/controller_exceptions.rb in the following and pay attention to my comment(GOOD and BAD).

[ruby]
def url_for__RFACEBOOK(options={}, *parameters) # :nodoc:
# fix problems that some Rails installations had with sending nil options
options ||= {}

# use special URL rewriting when inside the canvas
# setting the full_callback option to true will override this
# and force usage of regular Rails rewriting
if options.is_a? Hash
if options[:mock_ajax]
RAILS_DEFAULT_LOGGER.info “** RFACEBOOK DEPRECATION WARNING: don’t use :mock_ajax => true in your link_to anymore. Instead, use :full_callback => true.”
end
fullCallback = (options[:full_callback] == true) ? true : false
options.delete(:full_callback)
end

if ((in_facebook_canvas? or in_mock_ajax? or in_ajax?) and !fullCallback) #TODO: do something separate for in_facebook_frame?

if options.is_a? Hash
options[:only_path] = true if options[:only_path].nil?
end

# try to get a regular URL
path = url_for__ALIASED(options, *parameters)
# replace anything that references the callback with the
# Facebook canvas equivalent (apps.facebook.com/*)
if path.starts_with?(self.facebook_callback_path)
#GOOD! Go to here if you set the right facebook.yml in the common case
path.sub!(self.facebook_callback_path, self.facebook_canvas_path)
path = “http://apps.facebook.com#{path}”
elsif “#{path}/”.starts_with?(self.facebook_callback_path)
path.sub!(self.facebook_callback_path.chop, self.facebook_canvas_path.chop)
path = “http://apps.facebook.com#{path}”
elsif (path.starts_with?(”http://www.facebook.com”) or path.starts_with?(”https://www.facebook.com”))
# be sure that URLs that go to some other Facebook service redirect back to the canvas
if path.include?(”?”)
path = “#{path}&canvas=true”
else
path = “#{path}?canvas=true”
end
elsif (!path.starts_with?(”http://”) and !path.starts_with?(”https://”))
#BAD! Go to here if you set the wrong facebook.yml
# default to a full URL (will link externally)
RAILS_DEFAULT_LOGGER.debug “** RFACEBOOK INFO: failed to get canvas-friendly URL (”+path+”) for ["+options.inspect+"], creating an external URL instead”
path = “#{request.protocol}#{request.host}:#{request.port}#{path}”
end

# full callback rewriting
elsif fullCallback
options[:only_path] = true
path = “#{request.protocol}#{request.host}:#{request.port}#{url_for__ALIASED(options, *parameters)}”

# regular Rails rewriting
else
path = url_for__ALIASED(options, *parameters)
end
return path
end[/ruby]

For instance, we configure facebook.yml in Rails config directory as the following. When we call url_for(:controller=>’facebook’, :action => ‘test’), it actually calls url_for__RFACEBOOK. url_for_RFACEBOOK will replace /facebook/test with /netvibez/test. However, if we call url_for(:controller=>’somethingelse’, :action=> ‘test’), url_for_RFACEBOOK will generate http://www.netvibez.com/facebook/test which is NOT canvas friendly URL.

p. p.s. However, url_for(:controller=>’facebook’,:action=>’index’) will return http://www.netvibez.com/facebook. This is NOT what I expect. The reason is url_for_RFACEBOOK will only replace /facebook/ with /netvibez/. The ’slash’ makes big difference. If anyone knows how to fix, please drop me a line.

[code]development:
key: xxxxxxxxxx
secret: xxxxxxxxxx
canvas_path: /netvibez
callback_path: /facebook[/code]

Rails Migration Cheat Sheet

Filed under:rails — posted by admin on July 17, 2007 @ 3:05 pm

Rails CMS Rubricks

Filed under:rails — posted by admin on July 14, 2007 @ 3:38 pm

I am trying to setup up a CMS site based on Rails. I installed Radiant and Rubricks. Although Radiant is very succint, but I love Rubricks more because of features. But I hardly find useful document for Rubricks. Any comment?

OpenID in Rails

Filed under:rails, security — posted by admin on June 22, 2007 @ 8:46 pm

After reading Dan Webb’s article about OpenID , I decide to have a bash at supporting OpenID in Rails application.

First of all, install ruby-openid gem.

  • gem install ruby-openid -y

You can unpack them into vendor directory

  • gem unpack ruby-openid

Mashup Site for YouTube and Flickr – Netvibez

Filed under:rails, web2.0 — posted by admin on May 29, 2007 @ 11:33 pm

The great YouTube and Flickr Mashup site can be found in http://www.netvibez.com. It is very easy to watch Video on YouTube, AOL, Yahoo, Metacafe, asterpix and ‘hide me’ is freindly at work:>

Great Rails Mashup Site

Filed under:rails — posted by admin on December 9, 2006 @ 8:06 pm

Netvibez is an YouTube/Flickr mashup for finding video, images(photos) and music.



image: detail of installation by Bronwyn Lace