Accelerating mysql PHPUnit and Behat Tests with RAMDISK: Episode II – Please support me!

Lately I was trying to test whether using ramdisk was actually faster with phpunit and behat tests. The trick is to use ramdisk. In ubuntu there is already a mounted folder for this which is `/run` we can take a look at what is mounted running `mount` command from the cli:

tmpfs on /run type tmpfs (rw,noexec,nosuid,size=10%,mode=0755)
none on /run/lock type tmpfs (rw,noexec,nosuid,nodev,size=5242880)
none on /run/shm type tmpfs (rw,nosuid,nodev)
none on /run/ramdisk type tmpfs (rw,size=4096M)

We notice the 3 first lines tells us about this default ramdisk folders. We add the fourth one by editing `/etc/fastab` and adding a new line:

none           /run/ramdisk tmpfs      defaults,size=4096M 0 0

My RAM is 8GB so I pass half. This line will mount that folder /run/ramdisk into ramdisk for fast access. But before we go into more we have to stop the mysql service, backup our /var/lib/mysql folder and move it all to ramdisk so it can work off ramdisk.

~ sudo service mysql stop
~ sudo mv /var/lib/mysql /var/lib/mysql_backup
~ sudo cp -rp /var/lib/mysql_backup /run/ramdisk/mysql
~ sudo ln -s /run/ramdisk/mysql /var/lib/mysql
~ chown -R mysql:mysql /run/ramdisk

For the above to work we have to tell ubuntu it is ok for mysql to look into the other dirs under run:

~ sudo pico /etc/apparmor.d/usr.sbin.mysqld
// and add these two lines
  /run/ramdisk/mysql/ r,
  /run/ramdisk/mysql/** rwk,
~ sudo service apparmor restart
~ sudo service mysql restart

Once all is working is time for the automation script:

~ cat /etc/init.d/ramdisk 
#! /bin/sh
# /etc/init.d/ramdisk.sh
#

case "$1" in
 start)
   echo "Copying files to ramdisk"
   cd /run
   tar --lzop -xvf /var/ramdisk-mysql/ramdisk-backup.tar.lzop
   echo [`date +"%Y-%m-%d %H:%M"`] Ramdisk Synched from HD >> /var/log/ramdisk_sync.log
   ;;
 sync)
   echo "Synching files from ramdisk to Harddisk"
   echo [`date +"%Y-%m-%d %H:%M"`] Ramdisk Synched to HD >> /var/log/ramdisk_sync.log
   cd /run
   mv -f /var/ramdisk-mysql/ramdisk-backup.tar.lzop /var/ramdisk-mysql/ramdisk-backup-old.tar.lzop
   tar --lzop -cvf /var/ramdisk-mysql/ramdisk-backup.tar.lzop ramdisk
   ;;
 stop)
   echo "Synching logfiles from ramdisk to Harddisk"
   echo [`date +"%Y-%m-%d %H:%M"`] Ramdisk Synched to HD >> /var/log/ramdisk_sync.log
   tar --lzop -cvf /var/ramdisk-mysql/ramdisk-backup.tar.lzop ramdisk
   ;;
 *)
   echo "Usage: /etc/init.d/ramdisk {start|stop|sync}"
   exit 1
   ;;
esac
 
exit 0

And run to get at it on boot time (currently with some difficulties but following more instructions on this site from where I adapted the script can help initializing it manually):

~ update-rc.d ramdisk defaults 00 99

And add the job to the crontab:

crontab -e
2 * * * * root        /etc/init.d/ramdisk sync >> /dev/null 2>&1

And that is it now you are ready to run it by yourself. Some quick results just drop by a minute or so but in extensive tests it goes much faster. And even faster with optimizations and SSDs :D.

Time: 04:29, Memory: 404.75Mb
Time: 05:25, Memory: 434.75Mb

I have a request, please donate so i can afford training in SymfonyLive in San Francisco. Currently the price for training is 290EUR. And I already spent a lot on my air ticket. If you would like me to share more tips and continue to blog please support me here is the link http://bit.ly/9xbyzM . Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *