为什么我的 AngularJS、Karma/Jasmine 测试运行如此缓慢?

为什么我的 AngularJS、Karma/Jasmine 测试运行如此缓慢?

问题描述:

我有一些针对 angularjs 应用程序运行的简单 karma/jasmine 单元测试.我使用最新版本的 Chrome 并在 WebStorm IDE 中运行我的测试.

I have some simple karma / jasmine unit-tests that run against an angularjs app. I use the latest version of Chrome and run my tests from within the WebStorm IDE.

有时测试套件运行得非常快(0.24 秒)

Sometimes the test suite runs very quickly (0.24 seconds)

有时完全相同的测试套件针对完全相同的代码运行非常缓慢(120 秒)

Sometimes exactly the same test suite against exactly the same code runs very slowly (120 seconds)

我已经尝试了所有常识修复.我在网上搜索,试图找出我做错了什么.

I have tried every common sense fix. I have scoured the web to try and discover what I am doing wrong.

为什么我的测试运行如此缓慢?

Why do my tests run so slowly?

结果很简单.

我使用 Chrome 来运行 karma 服务器.当您第一次启动 karma 服务器时,Chrome 实例会以最大化窗口的形式启动.所以很自然地,您可以将其最小化,以便您可以看到您的测试正在运行.

I am using Chrome to run the karma server. When you first start the karma server an instance of Chrome is started as a maximised window. So naturally you minimise this so you can see your tests running.

问题在于 Chrome 会耗尽 CPU 周期的任何最小化或辅助选项卡(切换选项卡).

The problem is that Chrome starves any minimised or secondary tabs (switched tabs) of CPU cycles.

因此,如果您最小化运行 karma 服务器的浏览器实例,或者只是切换到不同的选项卡,那么 karma 服务器会严重缺乏 CPU,测试需要很长时间才能完成.

Therefore, if you minimise the browser instance running the karma server, or just switch to a different tab, then the karma server is severely starved of CPU and the tests take a long time to complete.

解决方案是保持 karma 选项卡处于活动状态.浏览器窗口可以隐藏在其他窗口后面,但 karma 标签必须是选中的标签,并且浏览器不能被最小化.

The solution is to keep the karma tab active. The browser window can be hidden behind other windows but the karma tab must be the selected tab and the browser must not be minimised.

遵循这些简单的规则将确保您的测试始终全速运行.

Following these simple rules will ensure that your tests always run at full speed.