是什么让Rspec占用4GB的内存来运行500个规格?

时间:2018-04-25 07:46:18

标签: rspec rspec-rails circleci ruby-on-rails-5.1 rspec3

当我在本地计算机上运行这些规范并成功通过时,我正在处理一个具有500多个规范的应用程序。但是当这些规范在CircleCI上运行时,该过程就会被杀死。 我试图通过跟踪本地计算机上的内存来调查此问题。当我看到ruby进程占用4gb内存时,我感到很惊讶,这就是触发CircleCI终止进程的原因。

我不确定我的规格记录所有内存的原因 我已经在每个规范之后搜索了一个清理内存但没有用的配置。

这是我的rails_helper.rb

require "mongoid-rspec"

require "spec_helper"
ENV["RAILS_ENV"] ||= "test"
require File.expand_path("../../config/environment", __FILE__)
if Rails.env.production?
  abort("The Rails environment is running in production mode!")
end

require "database_cleaner"

require "rspec/rails"

#
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }

RSpec.configure do |config|
  config.infer_spec_type_from_file_location!

  # FactoryGirl
  config.include FactoryGirl::Syntax::Methods

  # Render
  config.render_views

  # Filter lines from Rails gems in backtraces.
  config.filter_rails_from_backtrace!
  # arbitrary gems may also be filtered via:
  # config.filter_gems_from_backtrace("gem name")
  config.include Mongoid::Matchers, type: :model

  config.include Requests::JSONHelpers, type: :request
  config.include Requests::AuthHelpers, type: :request

  config.include Requests::JSONHelpers, type: :controller
  config.include Requests::AuthHelpers, type: :controller

  config.before(:suite) do
    DatabaseCleaner.orm = "mongoid"
    DatabaseCleaner.strategy = :truncation, { except: %w[roles] }
    DatabaseCleaner.clean_with(:truncation)
    Rails.application.load_seed # loading seeds
  end

  config.around(:each) do |example|
    DatabaseCleaner.cleaning do
      example.run
    end
  end

  config.after(:suite) do
    DatabaseCleaner.strategy = :truncation
    DatabaseCleaner.clean
  end
end

这是我的spec_helper.rb

require "mongoid-rspec"
require "webmock/rspec"
require "pundit/rspec"
require "excon"

WebMock.disable_net_connect!(allow_localhost: true)

RSpec::Matchers.define :match_response_schema do |schema|
  match do
    schema_directory = "#{Dir.pwd}/spec/schemas"
    schema_path = "#{schema_directory}/#{schema}.json"
    JSON::Validator.validate!(schema_path, parsed_json, strict: true)
  end
end
RSpec.configure do |config|
  config.expect_with :rspec do |expectations|
    expectations.include_chain_clauses_in_custom_matcher_descriptions = true
  end

  config.mock_with :rspec do |mocks|
    mocks.verify_partial_doubles = true
  end

  config.shared_context_metadata_behavior = :apply_to_host_groups

  config.include Mongoid::Matchers

  config.before(:all) do
    Excon.defaults[:mock] = true
    Excon.stub({}, body: "{}", status: 200)
  end
end

更新: 我在每个例子后用启动垃圾收集器修复它

rails_helper.rb

中的

config.after(:each) do |example|
    GC.start
end

但是,我希望找到比这更好的解决方案

1 个答案:

答案 0 :(得分:0)

GC.start有帮助的事实表明这不是内存泄漏。也许您有一些古怪的GC设置。如果是这样,请尝试进行一些调整:https://blog.evanweaver.com/2009/04/09/ruby-gc-tuning/

[我在意识到您的问题可能不是那样之前写了内存泄漏搜寻部分,但我将其保留下来,因为它可能对某些人有用]

我要做的是使用ruby-prof运行分析,将其设置为收集内存统计信息

这里是示例

require 'ruby-prof'
RubyProf.measure_mode = RubyProf::ALLOCATIONS

result = RubyProf.profile(:track_allocations => true) do
   100.times { rand(1) ? "*" * rand(100) : 20* rand(100) }  
end  

printer = RubyProf::GraphHtmlPrinter.new(result)
printer.print(File.new("/tmp/report.html", "w+"))

该报告将看起来像这样 [![在此处输入图片描述] [1]] [1]

所以,您需要做的是这样的:

around(:suite) do |suite|
  require 'ruby-prof'
  RubyProf.measure_mode = RubyProf::ALLOCATIONS

  result = RubyProf.profile(:track_allocations => true) do
   suite.run 
  end

  printer = RubyProf::GraphHtmlPrinter.new(result)
  printer.print(File.new("/tmp/report.html", "w+"))
end

也许您将能够发现哪些对象被过度代表,并且从那里您可能会发现泄漏。试用不同的mearure_mode和打印机,以获得更好的视角。 [1]:https://i.stack.imgur.com/2ZFcO.png