JoeTheCoder's avatar

How to speed up in memory db tests with fake seeds

Hey, I was wondering. How could I speed up my tests? I have in memory sqlite databse with one table and one small and simple seed file using Faker library. It is generating 10 records in a foreach range loop with 7 calls to faker to get the data.

The problem is that when I migrate + seed this databse it took Time: 8.05 seconds, Memory: 22.25Mb which is I believe too long for such as simple seed (have just one simple assertion for now). How long would it take later when I will be seeding products, categories and so on? Since I am using in memory database I need to migrate and seed it before every test. The seeder uses User::create() method. I've noticed that when I first build the array in a loop and then used DB::table('users')->insert($fakeObjects); it took Time: 1.93 seconds, Memory: 22.25Mb which is significantly better. However with DB table insertion I have to specify things like created_at and other auto generated stuff and I don't like to repeat this step every time.

How are you guys doing it? Is there some simplier solution?

0 likes
4 replies
JoeTheCoder's avatar

Hmm looks like I've misunderstand the forum structure. Sorry about that. Please if some mod could move it to the general section it would be great.

bashy's avatar

Oh asking for a tip on how to speed it up.

Okay well it depends on what process you're doing in the seeds. Foreach with Hash::make()? That would obviously slow things down since it has to do that function for each row.

JoeTheCoder's avatar

Thanks, yep that would be the problem. On my User model I have password mutator so it get automatically applied. I've tried to disable it and it took only 1.4 second.

Is there some way that I could disable it in the seed so I can create it once and use is as a pre generated variable? Something similar to Eloquent::unguard();

Please or to participate in this conversation.