Monthly Archives: January 2012

During this last months I’ve been involved in project written in Python and using the Django web Framework. Developers knows that during the development of a project tests are a very important part of your code and it has to be taken seriously to avoid bad surprises in the future.

Luckily Django has a very good testing environment which makes very easy to the developer to write their tests.

As you may know, every time you run a test in Django it will create a testing database and as the project grows up you will have more and more models and obviously this leads to a slower creation process of  this testing database and of course after a certain point this can get really painful and frustrating. Those developers who have worked with MySQL knows exactly what am I talking about.

During this last week I have been writing a couple of tests which in our case was impossible to use a in memory SQLite database as we had a MySQL specific piece of code which needed to be tested and that’s where my nightmare began.

After some hours of frustration I did some research and I found mysql-ram. This script is just amazing as it points MySQL data directory to a tmpfs partition, in other words, mounts this data directory in memory increasing drastically the speed. The database will no longer write to the normal ext3/4 partition instead it will write your data to memory.

The results are absolutely impressive. I did tests that came down from 77 seconds to less than 3. Try it yourself

Open the file, go to lines 22, 23 and 24 and configure the your server. You might want to assign another port number for your test database server.

22 BIND_SOCKET=/var/run/mysqld/mysqld-ram.sock
24 BIND_PORT=3307

Now, to run all you need to do its to give execute permission and run it as a normal bash script.

[apalma@insys aproject] chmod +x
[apalma@insys aproject] sudo ./

Now on you project root create a new file:

from settings import *

    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'my_db',
        'USER': 'my_user', # Not used with sqlite3.
        'PASSWORD': '', # Not used with sqlite3.
        'HOST': '', # Set to empty string for localhost. Not used with sqlite3.

Ok!! All set, you can now run the Django test command.

[apalma@insys aproject] ./ test --settings=settings_test

Hope this can be useful for you as it was for me.

%d bloggers like this: