为什么我的功能测试使用 Rails、Selenium 和 Spring 启动缓慢?

Why do my feature tests start slowly with Rails, Selenium and Spring?

我有一个 Rails 5.0.5 应用程序,其中包含使用 Capybara 和 Selenium (chromedriver) 编写的功能测试。我也在使用 spring。当我 运行 我的功能测试时,第一次请求到我的应用程序大约需要 45 秒,然后请求时间就正常了。

我想了解为什么第一个请求很慢并使其更快 - 这是一个严重的问题。

我已经将 selenium 的日志级别设置为调试:Selenium::WebDriver.logger.level = :debug

然后我看到这个:

017-10-06 10:40:53 INFO Selenium    >>> http://127.0.0.1:9516/session/d61353b6a9b514302b0146b398250ac6/url | {"url":"http://lvh.me:65483/logout"}
2017-10-06 10:40:53 DEBUG Selenium      > {"Accept"=>"application/json", "Content-Type"=>"application/json; charset=utf-8", "Content-Length"=>"36"}
DEPRECATION WARNING: You didn't set `secret_key_base`. Read the upgrade documentation to learn more about this new config option. (called from env_config at /Users/johnnaegle/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/railties-5.0.5/lib/rails/application.rb:246)

此时,它在我的测试中达到第一个 URL (/logout) 并且浏览器已启动(但挂起)。 rails 进程正在 运行ning,当它完成时,这会显示在服务器日志中:

[e97dc298-2ac5-4f29-b07c-58b0cc4e8827] Started GET "/logout" for 127.0.0.1 at 2017-10-06 10:45:16 -0500
[e97dc298-2ac5-4f29-b07c-58b0cc4e8827] Processing by UserSessionsController#destroy as HTML
[e97dc298-2ac5-4f29-b07c-58b0cc4e8827] Redirected to http://lvh.me:53531/login
[e97dc298-2ac5-4f29-b07c-58b0cc4e8827] Completed 302 Found in 834ms (ActiveRecord: 0.0ms)
[9a296768-7b0a-41f0-9eea-a95414f8a81d] Started GET "/login" for 127.0.0.1 at 2017-10-06 10:45:17 -0500
...
[9a296768-7b0a-41f0-9eea-a95414f8a81d] Completed 200 OK in 43621ms (Views: 43571.7ms | ActiveRecord: 21.8ms)

我在 osx。

如果我捆绑 exec 一个 rails 控制台(绕过 spring),急切加载我的应用程序并退出,这需要 22 秒。

我如何弄清楚为什么我的第一个功能测试需要 45 秒?我认为 spring 的目的是使开始时间和首次请求时间更快,但事实并非如此。

出于测试目的,Capybara 需要在单独的线程中启动自己的应用程序副本。您所看到的启动时间,加上可能触发资产编译的第一个请求(资产是在测试模式下编译的,而不是在开发模式下编译的)。