debian-mirror-gitlab/spec/lib/gitlab/metrics/samplers/ruby_sampler_spec.rb

86 lines
2.6 KiB
Ruby
Raw Normal View History

2018-03-17 18:26:18 +05:30
require 'spec_helper'
describe Gitlab::Metrics::Samplers::RubySampler do
let(:sampler) { described_class.new(5) }
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
Timecop.freeze do
expect(sampler.metrics[:process_start_time_seconds].get).to eq(Time.now.to_i)
end
end
end
2018-03-17 18:26:18 +05:30
describe '#sample' do
it 'samples various statistics' do
2019-09-04 21:01:54 +05:30
expect(Gitlab::Metrics::System).to receive(:cpu_time)
2018-03-17 18:26:18 +05:30
expect(Gitlab::Metrics::System).to receive(:file_descriptor_count)
2019-09-04 21:01:54 +05:30
expect(Gitlab::Metrics::System).to receive(:memory_usage)
expect(Gitlab::Metrics::System).to receive(:max_open_file_descriptors)
2018-03-17 18:26:18 +05:30
expect(sampler).to receive(:sample_gc)
sampler.sample
end
2019-09-04 21:01:54 +05:30
it 'adds a metric containing the process resident memory bytes' do
2018-03-17 18:26:18 +05:30
expect(Gitlab::Metrics::System).to receive(:memory_usage).and_return(9000)
2019-09-04 21:01:54 +05:30
expect(sampler.metrics[:process_resident_memory_bytes]).to receive(:set).with({}, 9000)
2018-03-17 18:26:18 +05:30
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)
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
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)
2018-11-08 19:23:39 +05:30
expect(sampler.metrics[:total_time]).to receive(:increment).with({}, 0.24)
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