Module: RailsMemoryProfiler::TestHelper

Extended by:
TestHelper
Included in:
TestHelper
Defined in:
lib/rails_memory_profiler/test_helper.rb

Instance Method Summary collapse

Instance Method Details

#assert_allocations_below(threshold, &block) ⇒ Object

Raises:

  • (error_class)


14
15
16
17
18
19
20
21
# File 'lib/rails_memory_profiler/test_helper.rb', line 14

def assert_allocations_below(threshold, &block)
  count = capture_allocations(&block)
  return if count < threshold

  message = "Expected fewer than #{threshold} allocated objects but got #{count}"
  error_class = Object.const_defined?(:Minitest) ? Minitest::Assertion : RuntimeError
  raise error_class, message
end

#capture_allocationsObject



7
8
9
10
11
12
# File 'lib/rails_memory_profiler/test_helper.rb', line 7

def capture_allocations
  GC.start
  before = GC.stat(:total_allocated_objects)
  yield
  GC.stat(:total_allocated_objects) - before
end