为什么当机器与internet断开连接时PHPUnit测试运行得更快?

时间:2022-10-15 22:48:06

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!

我注意到,当我的笔记本电脑连接到互联网上时,我的PHPUnit测试需要大约90到200秒才能完成。但是当我把它从互联网上断开时,它会在20秒内运行!这让我既高兴又难过!

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

在这两种情况下,所有的测试都在通过,我确信我正在模拟对外部API的每个请求。

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.

我使用Laravel和MySQL进行真正的数据存储,在内存中使用sqlite进行测试。我的开发环境也都在Docker上运行。

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

这与PHPUnit或我的代码有关吗? !任何人都知道发生了什么事。谢谢

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.

我正在使用的域名是something.dev,我的API使用了API。每个测试至少对每个API端点进行一次调用。

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!

DNS !如果您认为这是由于DNS查找:我将所有域和子域都更改为127.0.0.1来测试它,这并没有帮助测试仍然很慢。这是否应该消除DNS查找的可能性!

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.

此外,我尝试使用PHPUnit Bridge与PHPUnit连接来模拟DNS,但由于缺乏文档,我猜我无法使它正常工作,因此我不知道从我的setUp()函数调用它之后,要将什么作为参数传递给DnsMock:::withMockedHosts([here!!])。

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.

我认为这个问题与数据存储有关,因为延迟发生在查询数据库之前和之后,主要是存储数据。

1 个解决方案

#1


4  

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

哇,没有预期。由于PHP Faker包$ Faker ->image()提供的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/.

我在我的一个工厂中使用它来为DB准备一个假的图像,我不知道它实际上是下载图像并将它们存储在这样的文件夹中/private/var/folders/51/ 5ybn3kjn8f332jfrs7n00000gn /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 :)

通过监视PHP进程正在运行的过程,我可以发现它在该目录中有一个open .jpg文件,所以我查看了我的代码中与图像相关的任何东西,并在经过大约6个小时的调试之后发现了这一点。编码快乐:)

#1


4  

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

哇,没有预期。由于PHP Faker包$ Faker ->image()提供的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/.

我在我的一个工厂中使用它来为DB准备一个假的图像,我不知道它实际上是下载图像并将它们存储在这样的文件夹中/private/var/folders/51/ 5ybn3kjn8f332jfrs7n00000gn /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 :)

通过监视PHP进程正在运行的过程,我可以发现它在该目录中有一个open .jpg文件,所以我查看了我的代码中与图像相关的任何东西,并在经过大约6个小时的调试之后发现了这一点。编码快乐:)