Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why PHPUnit tests run faster when the machine is disconnected from the internet?

I have noticed that when my laptop is connected to the internet my PHPUnit tests takes between ~90 sec ~200 sec to finish. But when I disconnect it from the internet it runs in less than 20 sec!! that makes me happy and sad at the same time!

In both cases all the tests are passing, I'm sure I'm mocking every request to external API's.

I'm using Laravel and MySQL for real data storage and in-memory sqlite for the tests environment. Also my development environment is all on running on Docker.

Is this something related to PHPUnit or to my code!! any one has an idea on what's going on. Thanks

More Info

The domain I'm using is something.dev and my API's uses api.something.dev. Every test makes at least one call to each API endpoint.

DNS! If you think this is due to DNS lookup: I changed all the domain and subdomains to 127.0.0.1 just to test it, and it didn't helped the tests are still slow. Should this eliminate the possibility of DNS lookup!

In addition I tried mocking the DNS using The PHPUnit Bridge with PHPUnit but I guess I couldn't make it work due to the lack of documentation, so I didn't knew what to pass as parameter to DnsMock::withMockedHosts([here!!]) after calling it from my setUp() function.

Something else I think the problem is related to the data storage because the delay happens before and after querying the database, mostly to store data.

like image 740
Mahmoud Zalt Avatar asked Dec 21 '16 17:12

Mahmoud Zalt


1 Answers

Wow that wasn't expected. Turns out my tests are slow because of the image() function provided by the PHP Faker package $faker->image().

I was using it in one of my factories to prepare a fake Image for the DB, I didn't know it's literally downloading images and storing them in folder like this /private/var/folders/51/5ybn3kjn8f332jfrsx7nmam00000gn/T/.

I was able to find that by monitoring what the PHP process is doing while the test is running, to find out it has an open .jpg file in that directory, so I looked in my code anything related to images and discovered that, after about 6 hours of debugging. Happy coding :)

like image 157
Mahmoud Zalt Avatar answered Nov 15 '22 13:11

Mahmoud Zalt