如果编译时间超过可接受的水平,我可以创建一个失败的单元测试吗?

Can I create a unit test that fails if compile time is above an acceptable level?

有时代码会进入我的团队开发分支,但编译速度非常慢。当这达到几分钟时长的地步时,我们别无选择,只能放弃我们的任务并搜索导致此问题的原因,否则我们会浪费很多时间,直到我们解决它。

为了我们的应用程序性能,我们有单元测试来阻止我们的用户遇到缓慢的时间,我想知道是否可以在缓慢的编译时间会导致我们的测试失败的情况下进行测试,因此导致编译时间缓慢的更改可以在他们浪费整个团队的时间之前立即识别和删除。

也许它需要一些额外的努力,但一个可能的解决方案:

I use 'xcodebuild clean build OTHER_SWIFT_FLAGS="-Xfrontend -debug-time-function-bodies" | grep "[0-9][0-9].[0-9]*ms" | sort -nr > culprits.txt' to get a text file with the time it took to compile individual methods. Then you know where the compiler gets stuck and can optimize until the method that takes the longest to compile is <100ms.

source

您可以在项目的 Build Settings -> Other Swift Flags 中添加以下标志:-Xfrontend -warn-long-function-bodies=<time><time> 中指定毫秒数。然后,您将能够看到任何需要更多时间的功能的警告并修复它们。

它不会让您的测试失败,但当他们编写的代码编译时间过长时,整个团队都会意识到这一点。