Rails Optimization

January 8th, 2009

Find bottlenecks with NewRelic RPM

  1. Register at https://rpm.newrelic.com for the free light version of the RPM benchmarking tool.
  2. An email will be sent with a configuration file which goes into rails-application/config/.
  3. Go into rails-application/ and run
    script/plugin install http://svn.newrelic.com/rpm/agent/newrelic_rpm
    This will automatically fetch and install the plugin.
  4. Restart the server
  5. Play around with your rails app like any user would. The benchmarking results will be available almost instantly when you go back to https://rpm.newrelic.com (requires being logged in).

Grep

December 2nd, 2008

grep is an other most useful unix command line text search utility. The name is taken from the first letters in global / regular expression / print.

It can be used to find occurrences of a specific string or pattern in a file or in all files in a large directory in a few seconds.

Some useful option for the unix command grep

just COUNT occurrences:

grep -c find text.txt

using NOT:

grep -v notfind text.txt

ignore the case:

grep -i UPorLOW text.txt

using OR:

egrep 'this|that' text.txt

show context:

also show 2 previous lines: grep -B2 find text.txt

also show 2 next lines: grep -A2 find text.txt

Sanger Web-Proxy

November 20th, 2008

If you work at the Welcome Trust Sanger Institute and would like to browse web pages at home the same way you do at work, here is how to set it up: You need an ssh login, open a terminal window and connect like this



ssh -L3128:webcache.sanger.ac.uk:3128 YOUR-LOGIN-NAME@ssh.sanger.ac.uk

Then change the proxy settings in your web browser to point to:

localhost 3128

In Firefox this can be found at Edit / Preferences / Advanced / Network / Connection

This will create a "tunnel", forwarding the pages and other data you request through the Sanger network. You can see the intweb and journal pages, etc.

Even more conveniently, you can store the tunnel set up in the ~/ssh/config.ssh file and just connect ssh username@ssh.sanger.ac.uk.

Exmple:



Host ssh.sanger.ac.uk

LocalForward 14301 imap.sanger.ac.uk:143

LocalForward 25001 mail.sanger.ac.uk:25

LocalForward 3128 wwwcache.sanger.ac.uk:3128

LocalForward 2222 deskproXXXX.dynamic.sanger.ac.uk:22

A good ressource for more SSH productivity tips is this blog entry.

Ruby & Rails Terminology

November 4th, 2008

Basic Terminology from troubleshooters.com

Rails

A framework for developing web applications.

Ruby on Rails

Synonym for Rails.

Ruby

The computer language used to write Rails, and also the language you use to turn the Rails framework into an application. Ruby is a loosely typed interpreter with a full yet simple object model, and in my opinion is a very productive computer language.

Web application

A computer program that interfaces with the user through a web browser.

Framework

A ready made bunch of code and code generators to perform the majority of a software program. It is then up to the application developer to add the code that makes his application unique. Such code is typically added in many different spots throughout the framework.

MVC

Stands for Model, View and Controller. Many web application frameworks, including Ruby, partition their code into models, views and controllers. Doing so makes it easier to change and scale the program.

Model

The part of the application that interfaces to persistant data, whether that data is stored in a DBMS (MySQL, Postgres, MS SQL Server, Oracle and the like), or as a flat file on the local disk, or some other way. The persistent data is accessed and validated by code in the model.

There is typically one model for each database table, and one for each relevent flat file.

View

The part of the application that paints screens. Ideally, code in the view paints the screen but does nothing else. Lookups and calculations are done elsewhere, and the view simply sends results of those lookups and calculations to the screen, properly formatted.

There is typically one view for each type of screen, although often one view is used for several similar but slightly different screens. For instance, screens for data insert, modification and deletion are all similar enough to be accomplished with one view using flags set by the controller.

Controller

The part of the application that does what the model and view don't. Some people claim the controller contains the "business rules". I consider that a little pompous. After all, many applications are not intended to be used just for business. Also, some business rules, such as "we don't accept anyone with a credit score under 500" are typically implemented in a model as validation routines.

Every Rails application has at least one controller. There might be more, but usually not a large number. One way of splitting the work is to create a controller for each type of person using the system. For instance, there might be one controller called DataEntryPerson, another called Accountant, and a third called Administrator.

DRY

Stands for Don't Repeat Yourself. This means have each piece of information in one place. This is a basic part of the Ruby philosophy, and of course is also the philosophy behind data normalization.

AJAX

Stands for Asynchronous JavaScript And XML. This technology enables a web page to communicate with the server and update parts of itself without refreshing the whole page, thereby saving bandwidth.

Webrick

The web server that comes with Rails. You run it with this command:

script/server

It can serve only a single application on a single port, so it's more useful for development and testing than for production. Luckily, other web servers can serve Rails pages in production.

Apache

The market leader in web servers. Apache can serve Rails pages if you're willing to put in some deployment work.

InstantRails

Ruby, Gems and Rails, with production quality web server, in one bundle. Unfortunately, as of 1/18/2006 it's Windows only, but a Linux/Unix/BSD version is being worked on.

Locomotive

A production quality Rails-capable web server, which unfortunately is Mac only.

fastcgi

A system whereby CGI (Common Gateway Interface) programs stay in memory rather than being spawned as individual process when requested. This makes for much better efficiency. The lighttpd server comes with a fastcgi interface.

lighttpd

Production quality, Rails-capable, Ruby-centric web server available for Linux/Unix/BSD. Requires fastcgi. See http://wiki.rubyonrails.com/rails/pages/Lighttpd and http://www.lighttpd.net/.

RubyGems

A package manager for Ruby packages. Used to install Rails.

scaffold

An autocoded chunk of code facilitating creation ofscreens to list out a data table, and to provide create, edit and delete facilities for a data table, based on the structure of that data table, which the scaffold generator reads and uses as a specification. You can use a few scaffolds to create a quick and dirty web app to show your client.

session

A hash like structure within Rails apps to hold state between pages. It's a front end to cookies, where the state info is really held.

flash

This is NOT Macromedia flash, and is nothing like Macromedia flash!

In Rails, the term "flash" refers to a facility to pass temporary objects between actions. It's a module: ActionController::Flash. Whatever you place in flash will be exposed in the very next action, but then deleted, so you don't need to delete it manually (which is why it's better than the session for this type of thing). It's often used for error, warning and informational messages displayed on the screen after one the user has just filled out.

Gerp score

October 9th, 2008

GERP (Genomic Evolutionary Rate Profiling)

"GERP identifies constrained elements in multiple alignments by quantifying substitution deficits. These deficits represent substitutions that would have occurred if the element were neutral DNA, but did not occur because the element has been under functional constraint. We refer to these deficits as "Rejected Substitutions". Rejected substitutions are a natural measure of constraint that reflects the strength of past purifying selection on the element." [Sidow lab]

It was developed primarily by Greg Cooper in the lab of Arend Sidow at Stanford University (Depts of Pathology and Genetics), in close collaboration with Eric Stone (Biostatistics, NC State), and George Asimenos and Eugene Davydov in the lab of Serafim Batzoglou (Dept. of Computer Science, Stanford).

For more information, see the GERP section of the

track description for the ENCODE TBA Conservation

track in the Human May 2004 (hg17) genome browser,

and the GERP web page at the Sidow lab:

http://mendel.stanford.edu/sidowlab/downloads/gerp/index.html

and the publication:

Cooper, et. al.

"Distribution and intensity of constraint in mammalian genomic sequence"

Genome Research, July 2005

http://www.genome.org/cgi/content/abstract/15/7/901

Blog entry with more details.