Skip to content

Speed up your Unit Tests. Run detached MySQL instance with data folder in RAM (tmpfs), i.e. based completely in memory. The purpose is to increase execution speed of MySQL based application unit tests with minimal efforts.

Notifications You must be signed in to change notification settings

cryptooman/mysql-in-tmpfs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 

Repository files navigation

Speed up your Unit Tests

Run detached MySQL instance with data folder in RAM (tmpfs), i.e. based completely in memory. The purpose is to increase execution speed of MySQL based application unit tests with minimal efforts.

A common bottleneck that takes up most of the time when running unit tests is loading data into the database. For example, if we take a look at "Codeception" testing framework guide we can find that it suggests to load test data before each test action in the "Cest" class. If your database is a large collection of 100+ tables and has many "statuses" and "types" tables that need to be populated, then you might face up with slow loading of your data into MySQL. Slow unit tests are very annoying during development.

So:

  • Unit tests with MySQL completely in RAM are faster than SSD/HDD MySQL
  • No need to modify unit tests code
  • Each data dump populate can be noticeably accelerated
  • Each quicker populate x many tests = good time savings

Usage:

1) Run as sudo: sudo bash mysql-in-tmpfs.sh
   This will create detached mysqld instance with data folder in RAM (tmpfs)
2) Connect your application unit tests to the created mysqld instance
   See connection params below or in the script output

To verbose each command of the script run:
sudo bash -x mysql-in-tmpfs.sh

If need to autorun on server boot:
sudo crontab -e
    @reboot /bin/bash mysql-in-tmpfs.sh &>> /var/log/mysql-in-tmpfs.log

Defined params:
MYSQL_APPARMOR_FILE="/etc/apparmor.d/usr.sbin.mysqld"
MYSQL_TMP_DATA_DIR="/dev/shm/mysql-in-tmpfs"
MYSQL_TMP_PID_FILE="/dev/shm/mysql-in-tmpfs/mysqld.pid"
MYSQL_TMP_SOCKET="/dev/shm/mysql-in-tmpfs/mysqld.sock"
MYSQL_TMP_ERR_LOG="/var/log/mysql/mysql-in-tmpfs-error.log"
MYSQL_TMP_HOST="127.0.0.1"
MYSQL_TMP_PORT=33069
MYSQL_TMP_OWNER="mysql:mysql"
MYSQL_TMP_USER="tmpDatabase"
MYSQL_TMP_PASSWORD="tmpDatabase"
MYSQL_TMP_DATABASE="tmpDatabase"

Compatibility:

The script was tested on Ubuntu 20.04.1 LTS, MySQL version 8.0.*

Notes:

Same behaviour can be also achieved by using Docker with the appropriate configuration.

Warning:

ALL MYSQL TEMP DATA WILL BE LOST ON SERVER REBOOT / SHUTDOWN OR ACCIDENTAL POWER LOST

About

Speed up your Unit Tests. Run detached MySQL instance with data folder in RAM (tmpfs), i.e. based completely in memory. The purpose is to increase execution speed of MySQL based application unit tests with minimal efforts.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages