我读到 --runInBand flag 在 CI 服务器上将 Jest 测试持续时间加快了 50%。除了让测试在同一个线程中按顺序运行之外,我真的无法在网上找到关于该标志的作用的解释。
为什么在同一个线程中按顺序运行测试会使其更快?直觉上,这不应该让它变慢吗?
最佳答案
阅读您的链接页面和其他一些相关资源(如 this github issue)一些用户发现:
...using the
--runInBand
helps in an environment with limited resources.
和
...
--runInBand
took our tests from >1.5 hours (actually I don't know how long because Jenkins timed out at 1.5 hours) to around 4 minutes. (Note: we have really poor resources for our build server)
正如我们所见,这些用户在他们的机器上的性能得到了提高,即使他们的机器上的资源有限。如果我们从 docs 中读取 --runInBand
标志的作用它说:
Alias: -i. Run all tests serially in the current process, rather than creating a worker pool of child processes that run tests. This can be useful for debugging.
因此,考虑到这些评论和文档,我认为性能的提高是由于现在进程在单个线程中运行。这极大地帮助了有限资源计算机,因为它不必花费内存和时间来处理和处理线程池中的多个线程,而对于有限的资源来说,这项任务可能过于昂贵。
但是,我相信只有在您使用的机器资源有限的情况下才会出现这种情况。如果您使用更“强大”的机器(即:多个内核、体面的 RAM、SSD 等),使用多个线程可能比运行单个线程更好。
关于testing - 为什么 Jest --runInBand 会加速测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43864793/