ampeco/fast-sqlite-refresh-database

Alternative to migrations for each test

1.0.6 2024-02-19 13:52 UTC

This package is auto-updated.

Last update: 2024-05-19 14:45:56 UTC


README

Alternative to the default laravel behaviour that will refresh the sqlite database for each test.

Also, an alternative to squashing migrations, since that still has to import the squashed migration on each test.

In this implementation no transactions are used when starting a test and the database is refreshed by deleting the sqlite file and recreating it from a clean template!

Instructions

  1. Install the package
composer require --dev ampeco/fast-sqlite-refresh-database
  1. Make sure you are not using in-memory database, but a sqlite file based one for your test setup
  2. In your test instead of use RefreshDatabase; use use FastSqliteRefreshDatabase;
  3. Run your tests

Performance

We tested it in an internal Feature testing suit with 86 tests. Using LazyRefreshDatabase we got:

ParaTest v6.9.1 upon PHPUnit 9.6.7 by Sebastian Bergmann and contributors.

................................................................. 65 / 86 ( 75%)
.....................                                             86 / 86 (100%)

Time: 01:09.101, Memory: 39.12 MB

Then we switched to FastSqliteRefreshDatabase and got:

ParaTest v6.9.1 upon PHPUnit 9.6.7 by Sebastian Bergmann and contributors.

................................................................. 65 / 86 ( 75%)
.....................                                             86 / 86 (100%)

Time: 00:06.894, Memory: 39.12 MB

Additional speed improvement

Preheating the database

If you are running a lot of test in CI for example. You can call php artisan fast-sqlite-refresh-database:preheat before running your tests. This will create a clean sqlite database file that can be copied for each test. This will speed up your tests even more!

In order to tell your tests to use the preheated database, you can set the environment variable FAST_SQLITE_REFRESH_DATABASE_PREHEATED to true before running your tests.

FAST_SQLITE_REFRESH_DATABASE_PREHEATED=true php vendor/bin/paratest

Here is the same test with preheated database:

ParaTest v6.9.1 upon PHPUnit 9.6.7 by Sebastian Bergmann and contributors.

................................................................. 65 / 86 ( 75%)
.....................                                             86 / 86 (100%)

Time: 00:05.916, Memory: 39.12 MB

Ramdisk

Running a large set of tests can create a lot of IO, for this reason you can use a ramdisk to store the sqlite database file.

On macOS you could use APFS or HFS+ ramdisk. From our tests HFS+ appears to be faster by 3-4%.

# APFS
diskutil apfs create $(hdiutil attach -nomount ram://1048576) ramdisk && touch /Volumes/ramdisk/.metadata_never_index &&  touch /Volumes/ramdisk/.fseventsd/no_log
#HFS+
diskutil erasevolume HFS+ ramdisk $(hdiutil attach -nomount ram://1048576) && touch /Volumes/ramdisk/.metadata_never_index &&  touch /Volumes/ramdisk/.fseventsd/no_log

To unmount the ramdisk:

diskutil eject /Volumes/ramdisk

On linux you can use tmpfs