diff options
Diffstat (limited to 'spec/lib/gitlab/metrics/memory_spec.rb')
-rw-r--r-- | spec/lib/gitlab/metrics/memory_spec.rb | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/spec/lib/gitlab/metrics/memory_spec.rb b/spec/lib/gitlab/metrics/memory_spec.rb new file mode 100644 index 00000000000..fd8ca3b37c6 --- /dev/null +++ b/spec/lib/gitlab/metrics/memory_spec.rb @@ -0,0 +1,40 @@ +# frozen_string_literal: true + +require 'fast_spec_helper' + +RSpec.describe Gitlab::Metrics::Memory do + describe '.gc_heap_fragmentation' do + subject(:call) do + described_class.gc_heap_fragmentation( + heap_live_slots: gc_stat_heap_live_slots, + heap_eden_pages: gc_stat_heap_eden_pages + ) + end + + context 'when the Ruby heap is perfectly utilized' do + # All objects are located in a single heap page. + let(:gc_stat_heap_live_slots) { described_class::HEAP_SLOTS_PER_PAGE } + let(:gc_stat_heap_eden_pages) { 1 } + + it { is_expected.to eq(0) } + end + + context 'when the Ruby heap is greatly fragmented' do + # There is one object per heap page. + let(:gc_stat_heap_live_slots) { described_class::HEAP_SLOTS_PER_PAGE } + let(:gc_stat_heap_eden_pages) { described_class::HEAP_SLOTS_PER_PAGE } + + # The heap can never be "perfectly fragmented" because that would require + # zero objects per page. + it { is_expected.to be > 0.99 } + end + + context 'when the Ruby heap is semi-fragmented' do + # All objects are spread over two pages i.e. each page is 50% utilized. + let(:gc_stat_heap_live_slots) { described_class::HEAP_SLOTS_PER_PAGE } + let(:gc_stat_heap_eden_pages) { 2 } + + it { is_expected.to eq(0.5) } + end + end +end |