ruby_sampler_spec.rb 1.76 KB
Newer Older
Pawel Chojnacki's avatar
Pawel Chojnacki committed
1 2 3 4
require 'spec_helper'

describe Gitlab::Metrics::Samplers::RubySampler do
  let(:sampler) { described_class.new(5) }
Pawel Chojnacki's avatar
Pawel Chojnacki committed
5 6 7 8 9
  let(:null_metric) { double('null_metric', set: nil, observe: nil) }

  before do
    allow(Gitlab::Metrics::NullMetric).to receive(:instance).and_return(null_metric)
  end
Pawel Chojnacki's avatar
Pawel Chojnacki committed
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

  after do
    Allocations.stop if Gitlab::Metrics.mri?
  end

  describe '#sample' do
    it 'samples various statistics' do
      expect(Gitlab::Metrics::System).to receive(:memory_usage)
      expect(Gitlab::Metrics::System).to receive(:file_descriptor_count)
      expect(sampler).to receive(:sample_gc)

      sampler.sample
    end

    it 'adds a metric containing the memory usage' do
Pawel Chojnacki's avatar
Pawel Chojnacki committed
25
      expect(Gitlab::Metrics::System).to receive(:memory_usage).and_return(9000)
Pawel Chojnacki's avatar
Pawel Chojnacki committed
26

Pawel Chojnacki's avatar
Pawel Chojnacki committed
27
      expect(sampler.metrics[:memory_usage]).to receive(:set).with({}, 9000)
Pawel Chojnacki's avatar
Pawel Chojnacki committed
28 29 30 31 32 33 34 35

      sampler.sample
    end

    it 'adds a metric containing the amount of open file descriptors' do
      expect(Gitlab::Metrics::System).to receive(:file_descriptor_count)
                                           .and_return(4)

Pawel Chojnacki's avatar
Pawel Chojnacki committed
36
      expect(sampler.metrics[:file_descriptors]).to receive(:set).with({}, 4)
Pawel Chojnacki's avatar
Pawel Chojnacki committed
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51

      sampler.sample
    end

    it 'clears any GC profiles' do
      expect(GC::Profiler).to receive(:clear)

      sampler.sample
    end
  end

  describe '#sample_gc' do
    it 'adds a metric containing garbage collection time statistics' do
      expect(GC::Profiler).to receive(:total_time).and_return(0.24)

Pawel Chojnacki's avatar
Pawel Chojnacki committed
52
      expect(sampler.metrics[:total_time]).to receive(:set).with({}, 240)
Pawel Chojnacki's avatar
Pawel Chojnacki committed
53 54 55 56 57 58

      sampler.sample
    end

    it 'adds a metric containing garbage collection statistics' do
      GC.stat.keys.each do |key|
Pawel Chojnacki's avatar
Pawel Chojnacki committed
59
        expect(sampler.metrics[key]).to receive(:set).with({}, anything)
Pawel Chojnacki's avatar
Pawel Chojnacki committed
60 61 62 63 64 65
      end

      sampler.sample
    end
  end
end