ruby_sampler_spec.rb 3.03 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

Ryan Cobb's avatar
Ryan Cobb committed
11 12 13 14 15 16 17 18
  describe '#initialize' do
    it 'sets process_start_time_seconds' do
      Timecop.freeze do
        expect(sampler.metrics[:process_start_time_seconds].get).to eq(Time.now.to_i)
      end
    end
  end

Pawel Chojnacki's avatar
Pawel Chojnacki committed
19 20
  describe '#sample' do
    it 'samples various statistics' do
21
      expect(Gitlab::Metrics::System).to receive(:cpu_time)
22
      expect(Gitlab::Metrics::System).to receive(:file_descriptor_count)
23 24
      expect(Gitlab::Metrics::System).to receive(:memory_usage)
      expect(Gitlab::Metrics::System).to receive(:max_open_file_descriptors)
Pawel Chojnacki's avatar
Pawel Chojnacki committed
25 26 27 28 29
      expect(sampler).to receive(:sample_gc)

      sampler.sample
    end

30
    it 'adds a metric containing the process resident memory bytes' do
Pawel Chojnacki's avatar
Pawel Chojnacki committed
31
      expect(Gitlab::Metrics::System).to receive(:memory_usage).and_return(9000)
Pawel Chojnacki's avatar
Pawel Chojnacki committed
32

33
      expect(sampler.metrics[:process_resident_memory_bytes]).to receive(:set).with({}, 9000)
Pawel Chojnacki's avatar
Pawel Chojnacki committed
34 35 36 37 38 39 40 41

      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
42
      expect(sampler.metrics[:file_descriptors]).to receive(:set).with({}, 4)
Pawel Chojnacki's avatar
Pawel Chojnacki committed
43 44 45 46

      sampler.sample
    end

47 48 49 50 51 52 53 54 55 56 57 58 59 60
    it 'adds a metric containing the process total cpu time' do
      expect(Gitlab::Metrics::System).to receive(:cpu_time).and_return(0.51)
      expect(sampler.metrics[:process_cpu_seconds_total]).to receive(:set).with({}, 0.51)

      sampler.sample
    end

    it 'adds a metric containing the process max file descriptors' do
      expect(Gitlab::Metrics::System).to receive(:max_open_file_descriptors).and_return(1024)
      expect(sampler.metrics[:process_max_fds]).to receive(:set).with({}, 1024)

      sampler.sample
    end

Pawel Chojnacki's avatar
Pawel Chojnacki committed
61
    it 'clears any GC profiles' do
62
      expect(GC::Profiler).to receive(:clear).at_least(:once)
Pawel Chojnacki's avatar
Pawel Chojnacki committed
63 64 65 66 67 68

      sampler.sample
    end
  end

  describe '#sample_gc' do
69
    let!(:sampler) { described_class.new(5) }
Pawel Chojnacki's avatar
Pawel Chojnacki committed
70

71 72 73 74 75 76 77 78 79 80 81 82 83 84
    let(:gc_reports) { [{ GC_TIME: 0.1 }, { GC_TIME: 0.2 }, { GC_TIME: 0.3 }] }

    it 're-enables GC::Profiler if needed' do
      expect(GC::Profiler).to receive(:enable)

      sampler.sample
    end

    it 'observes GC cycles time' do
      expect(sampler).to receive(:sample_gc_reports).and_return(gc_reports)

      expect(sampler.metrics[:gc_duration_seconds]).to receive(:observe).with({}, 0.1).ordered
      expect(sampler.metrics[:gc_duration_seconds]).to receive(:observe).with({}, 0.2).ordered
      expect(sampler.metrics[:gc_duration_seconds]).to receive(:observe).with({}, 0.3).ordered
Pawel Chojnacki's avatar
Pawel Chojnacki committed
85 86 87 88 89 90

      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
91
        expect(sampler.metrics[key]).to receive(:set).with({}, anything)
Pawel Chojnacki's avatar
Pawel Chojnacki committed
92 93 94 95 96 97
      end

      sampler.sample
    end
  end
end