2019-12-04 20:38:33 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
RSpec.describe Gitlab::Metrics::Samplers::RubySampler do
|
2020-06-23 00:09:42 +05:30
|
|
|
let(:sampler) { described_class.new }
|
2018-03-17 18:26:18 +05:30
|
|
|
let(:null_metric) { double('null_metric', set: nil, observe: nil) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(Gitlab::Metrics::NullMetric).to receive(:instance).and_return(null_metric)
|
|
|
|
end
|
|
|
|
|
2019-09-30 21:07:59 +05:30
|
|
|
describe '#initialize' do
|
|
|
|
it 'sets process_start_time_seconds' do
|
2020-11-24 15:15:51 +05:30
|
|
|
freeze_time do
|
2019-09-30 21:07:59 +05:30
|
|
|
expect(sampler.metrics[:process_start_time_seconds].get).to eq(Time.now.to_i)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
describe '#interval' do
|
|
|
|
it 'samples every sixty seconds by default' do
|
|
|
|
expect(subject.interval).to eq(60)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'samples at other intervals if requested' do
|
|
|
|
expect(described_class.new(11).interval).to eq(11)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
describe '#sample' do
|
2020-05-24 23:13:21 +05:30
|
|
|
it 'adds a metric containing the process resident memory bytes' do
|
|
|
|
expect(Gitlab::Metrics::System).to receive(:memory_usage_rss).and_return(9000)
|
|
|
|
|
|
|
|
expect(sampler.metrics[:process_resident_memory_bytes]).to receive(:set).with({}, 9000)
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
sampler.sample
|
|
|
|
end
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
it 'adds a metric containing the process unique and proportional memory bytes' do
|
|
|
|
expect(Gitlab::Metrics::System).to receive(:memory_usage_uss_pss).and_return(uss: 9000, pss: 10_000)
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
expect(sampler.metrics[:process_unique_memory_bytes]).to receive(:set).with({}, 9000)
|
|
|
|
expect(sampler.metrics[:process_proportional_memory_bytes]).to receive(:set).with({}, 10_000)
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
sampler.sample
|
|
|
|
end
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
context 'when USS+PSS sampling is disabled via environment' do
|
|
|
|
before do
|
|
|
|
stub_env('enable_memory_uss_pss', "0")
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not sample USS or PSS' do
|
|
|
|
expect(Gitlab::Metrics::System).not_to receive(:memory_usage_uss_pss)
|
|
|
|
|
|
|
|
expect(sampler.metrics[:process_unique_memory_bytes]).not_to receive(:set)
|
|
|
|
expect(sampler.metrics[:process_proportional_memory_bytes]).not_to receive(:set)
|
|
|
|
|
|
|
|
sampler.sample
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
it 'adds a metric containing the amount of open file descriptors' do
|
|
|
|
expect(Gitlab::Metrics::System).to receive(:file_descriptor_count)
|
|
|
|
.and_return(4)
|
|
|
|
|
|
|
|
expect(sampler.metrics[:file_descriptors]).to receive(:set).with({}, 4)
|
|
|
|
|
|
|
|
sampler.sample
|
|
|
|
end
|
|
|
|
|
2019-09-04 21:01:54 +05:30
|
|
|
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
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
it 'clears any GC profiles' do
|
2019-10-12 21:52:04 +05:30
|
|
|
expect(GC::Profiler).to receive(:clear).at_least(:once)
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
sampler.sample
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#sample_gc' do
|
2019-10-12 21:52:04 +05:30
|
|
|
let!(:sampler) { described_class.new(5) }
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
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
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
sampler.sample
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'adds a metric containing garbage collection statistics' do
|
|
|
|
GC.stat.keys.each do |key|
|
|
|
|
expect(sampler.metrics[key]).to receive(:set).with({}, anything)
|
|
|
|
end
|
|
|
|
|
|
|
|
sampler.sample
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|