Putting a Rails test database on Ramdisk

Want to run your Rails tests in a super fast Ramdisk?

Ingredients

  1. Postgres (8.0 or later)
  2. Ubuntu (12.04 is what I tested but will likely work on just about any version)

Method

My test database is only about 30MB when seeded, so I can get away with a 200MB Ramdisk. Here's a quick bash script to create a 200MB Ramdisk and a tablespace on that disk:

1
2
3
4
5
6
7
8
9
echo "Creating ramdb, make sure your test db has 'tablespace test'"
echo "in database.yml"
sudo mkdir -p /mnt/ram/pgtest
sudo mount -t ramfs -o size=200m ramfs /mnt/ram/pgtest
sudo chown postgres:postgres /mnt/ram/pgtest

psql postgres -c \
"create tablespace fastspace location '/mnt/ram/pgtest'"

Once you have done that, edit your database.yml and get it to be created in the new tablespace:

1
2
3
4
5
6
test:
  adapter: postgresql
  database: mydatabase_test
  ...
  tablespace: fastspace

A bash script to drop everything again:

1
2
3
4
5
psql postgres -c "drop database mydatabase_test"
psql postgres -c "drop tablespace fastspace"
sudo rm -rf /mnt/ram/pgtest
sudo umount /mnt/ram

My "benchmarks"

Disk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
1.
real  3m43.338s
user  2m35.018s
sys 0m25.614s

2.
real  3m42.426s
user  2m34.654s
sys 0m26.246s

3.
real  3m45.447s
user  2m38.186s
sys 0m26.182s

Ramdisk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
1.
real  3m25.656s
user  2m38.334s
sys 0m25.746s

2.
real  3m23.970s
user  2m36.798s
sys 0m25.682s

3.
real  3m24.451s
user  2m37.226s
sys 0m25.702s

Results

It looks like I saved about 20s. Not that much, considering this is around 1300 tests. Most of my tests seem to be CPU bound.

Back

Metadata

Home

Leslie

This is the website of Leslie Viljoen. More info



This website is powered by Rails 4
log in