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 http://google.co.za/

-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 http://mysite.co.za/secured_page

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

Earlier this week myself in collaboration with Shuntyard Technologies launched the rubysa.co.za domain. This has been an idea I had floating around for a while and I’m glad it’s finally seen the light of day. This portal serves the local Ruby and Ruby on Rails community and it is my hope that job seekers and business’s requiring work will use the site and help it grow into something useful.

If there any missing features just pop me a mail and we can look at adding it.

rubysa.co.za

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"
end

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

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

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"
end

now you can query the star schema like so:

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

which generates sql like so:

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