Simple load testing for Rails apps

If your looking for a really quick and easy way to load test a deployed Rails app you can’t get much simpler than Apaches ab tool.

To load test public pages just use

ab -n100 -c5 -t10

-n requests Number of requests to perform
-c concurrency Number of multiple requests to make
-t timelimit Seconds to max. wait for responses

To load test secured pages you will need pass cookie information

ab -n100 -c5 -t10 -C identifier=cookie

Getting the cookie details using Chrome is really easy.

1. Login to the secured area

2. Find the request in Developer Tools => Network

3. Look for the cookie details in the response headers

Happy load testing!

South African Ruby and Ruby on Rails job portal



Rails: Getting MySQL, PostgreSQL or SQLite installed on Ubuntu

I don’t how many times I had to google this so here it for posterity:

Install the sqlite3 connector:
sudo apt-get install libsqlite3-dev
sudo gem install sqlite3-ruby

Or install the MySQL database and it’s connector:
sudo apt-get install mysql-server mysql-client
sudo apt-get install libmysql-ruby libmysqlclient-dev
sudo gem install mysql

install the PostgreSQL database and it’s connector:
sudo apt-get install postgresql-server postgresql-client
sudo apt-get install libpq-dev
sudo gem install pg

Rails star schema query using vanilla active record

In a recent project I was faced with generated SQL to query a star schema i.e. a fact table with surrounding dimension tables. For the sake of this post  lets presume we have the following tables in PostgreSQL:

create table calendars (
 id serial,
 calendar_date date

create table locations (
 id serial,
 name varchar(100)

create table products (
 id serial,
 name varchar(100)

create table sales (
 id serial,
 calendar_id integer,
 location_id integer,
 product_id integer,
 quantity integer

and the following models setup:

class Calendar < ActiveRecord::Base
  has_many :sales, :foreign_key => "calendar_id"

class Location < ActiveRecord::Base
  has_many :sales, :foreign_key => "location_id"

class Product < ActiveRecord::Base
  has_many :sales, :foreign_key => "product_id"

class Sale < ActiveRecord::Base
  belongs_to :calendar, :foreign_key => "calendar_id"
  has_one :location, :foreign_key => "location_id"
  has_one :product, :foreign_key => "product_id"

now you can query the star schema like so:

@sales = Sale.find(
  :all, :select => "calendars.calendar_date,,, sales.quantity",
  :joins => [:location, :calendar, :product]

which generates sql like so:

select calendars.calendar_date,,,
from   "sales"
       inner join "locations"
         on = sales.location_id
       inner join "calendars"
         on = sales.calendar_id
       inner join "products"
         on = sales.product_id