debian-mirror-gitlab/spec/lib/gitlab/metrics/subscribers/rails_cache_spec.rb

221 lines
6.6 KiB
Ruby
Raw Normal View History

2019-12-04 20:38:33 +05:30
# frozen_string_literal: true
2016-06-02 11:05:42 +05:30
require 'spec_helper'
describe Gitlab::Metrics::Subscribers::RailsCache do
2018-03-17 18:26:18 +05:30
let(:env) { {} }
let(:transaction) { Gitlab::Metrics::WebTransaction.new(env) }
2016-06-02 11:05:42 +05:30
let(:subscriber) { described_class.new }
let(:event) { double(:event, duration: 15.2) }
describe '#cache_read' do
it 'increments the cache_read duration' do
2018-03-17 18:26:18 +05:30
expect(subscriber).to receive(:observe)
.with(:read, event.duration)
2016-06-02 11:05:42 +05:30
subscriber.cache_read(event)
end
2016-08-24 12:49:21 +05:30
context 'with a transaction' do
before do
2017-09-10 17:25:29 +05:30
allow(subscriber).to receive(:current_transaction)
2018-03-17 18:26:18 +05:30
.and_return(transaction)
2016-08-24 12:49:21 +05:30
end
context 'with hit event' do
let(:event) { double(:event, duration: 15.2, payload: { hit: true }) }
it 'increments the cache_read_hit count' do
2017-09-10 17:25:29 +05:30
expect(transaction).to receive(:increment)
2018-03-17 18:26:18 +05:30
.with(:cache_read_hit_count, 1, false)
2017-09-10 17:25:29 +05:30
expect(transaction).to receive(:increment)
2018-03-17 18:26:18 +05:30
.with(any_args).at_least(1) # Other calls
2016-08-24 12:49:21 +05:30
subscriber.cache_read(event)
end
context 'when super operation is fetch' do
let(:event) { double(:event, duration: 15.2, payload: { hit: true, super_operation: :fetch }) }
it 'does not increment cache read miss' do
2017-09-10 17:25:29 +05:30
expect(transaction).not_to receive(:increment)
2018-03-17 18:26:18 +05:30
.with(:cache_read_hit_count, 1)
2016-08-24 12:49:21 +05:30
subscriber.cache_read(event)
end
end
end
context 'with miss event' do
let(:event) { double(:event, duration: 15.2, payload: { hit: false }) }
it 'increments the cache_read_miss count' do
2017-09-10 17:25:29 +05:30
expect(transaction).to receive(:increment)
2018-03-17 18:26:18 +05:30
.with(:cache_read_miss_count, 1, false)
2017-09-10 17:25:29 +05:30
expect(transaction).to receive(:increment)
2018-03-17 18:26:18 +05:30
.with(any_args).at_least(1) # Other calls
subscriber.cache_read(event)
end
it 'increments the cache_read_miss total' do
expect(subscriber.send(:metric_cache_misses_total)).to receive(:increment).with({})
2016-08-24 12:49:21 +05:30
subscriber.cache_read(event)
end
context 'when super operation is fetch' do
let(:event) { double(:event, duration: 15.2, payload: { hit: false, super_operation: :fetch }) }
it 'does not increment cache read miss' do
2017-09-10 17:25:29 +05:30
expect(transaction).not_to receive(:increment)
2018-03-17 18:26:18 +05:30
.with(:cache_read_miss_count, 1)
subscriber.cache_read(event)
end
it 'does not increment cache_read_miss total' do
expect(subscriber.send(:metric_cache_misses_total)).not_to receive(:increment).with({})
2016-08-24 12:49:21 +05:30
subscriber.cache_read(event)
end
end
end
end
2016-06-02 11:05:42 +05:30
end
describe '#cache_write' do
2018-03-17 18:26:18 +05:30
it 'observes write duration' do
expect(subscriber).to receive(:observe)
.with(:write, event.duration)
2016-06-02 11:05:42 +05:30
subscriber.cache_write(event)
end
end
describe '#cache_delete' do
2018-03-17 18:26:18 +05:30
it 'observes delete duration' do
expect(subscriber).to receive(:observe)
.with(:delete, event.duration)
2016-06-02 11:05:42 +05:30
subscriber.cache_delete(event)
end
end
describe '#cache_exist?' do
2018-03-17 18:26:18 +05:30
it 'observes the exists duration' do
expect(subscriber).to receive(:observe)
.with(:exists, event.duration)
2016-06-02 11:05:42 +05:30
subscriber.cache_exist?(event)
end
end
2016-08-24 12:49:21 +05:30
describe '#cache_fetch_hit' do
context 'without a transaction' do
it 'returns' do
expect(transaction).not_to receive(:increment)
subscriber.cache_fetch_hit(event)
end
end
context 'with a transaction' do
before do
2017-09-10 17:25:29 +05:30
allow(subscriber).to receive(:current_transaction)
2018-03-17 18:26:18 +05:30
.and_return(transaction)
2016-08-24 12:49:21 +05:30
end
it 'increments the cache_read_hit count' do
2017-09-10 17:25:29 +05:30
expect(transaction).to receive(:increment)
2018-03-17 18:26:18 +05:30
.with(:cache_read_hit_count, 1)
2016-08-24 12:49:21 +05:30
subscriber.cache_fetch_hit(event)
end
end
end
describe '#cache_generate' do
context 'without a transaction' do
it 'returns' do
expect(transaction).not_to receive(:increment)
subscriber.cache_generate(event)
end
end
context 'with a transaction' do
2018-03-17 18:26:18 +05:30
let(:metric_cache_misses_total) { double('metric_cache_misses_total', increment: nil) }
2016-08-24 12:49:21 +05:30
before do
2018-03-17 18:26:18 +05:30
allow(subscriber).to receive(:metric_cache_misses_total).and_return(metric_cache_misses_total)
2017-09-10 17:25:29 +05:30
allow(subscriber).to receive(:current_transaction)
2018-03-17 18:26:18 +05:30
.and_return(transaction)
2016-08-24 12:49:21 +05:30
end
it 'increments the cache_fetch_miss count' do
2017-09-10 17:25:29 +05:30
expect(transaction).to receive(:increment)
2018-03-17 18:26:18 +05:30
.with(:cache_read_miss_count, 1)
subscriber.cache_generate(event)
end
2016-08-24 12:49:21 +05:30
2018-03-17 18:26:18 +05:30
it 'increments the cache_read_miss total' do
2016-08-24 12:49:21 +05:30
subscriber.cache_generate(event)
2018-03-17 18:26:18 +05:30
expect(metric_cache_misses_total).to have_received(:increment).with({})
2016-08-24 12:49:21 +05:30
end
end
end
2018-03-17 18:26:18 +05:30
describe '#observe' do
2016-06-02 11:05:42 +05:30
context 'without a transaction' do
it 'returns' do
expect(transaction).not_to receive(:increment)
2018-03-17 18:26:18 +05:30
subscriber.observe(:foo, 15.2)
2016-06-02 11:05:42 +05:30
end
end
context 'with a transaction' do
before do
2017-09-10 17:25:29 +05:30
allow(subscriber).to receive(:current_transaction)
2018-03-17 18:26:18 +05:30
.and_return(transaction)
2016-06-02 11:05:42 +05:30
end
it 'increments the total and specific cache duration' do
2017-09-10 17:25:29 +05:30
expect(transaction).to receive(:increment)
2018-03-17 18:26:18 +05:30
.with(:cache_duration, event.duration, false)
2016-06-02 11:05:42 +05:30
2017-09-10 17:25:29 +05:30
expect(transaction).to receive(:increment)
2018-03-17 18:26:18 +05:30
.with(:cache_count, 1, false)
2016-06-02 11:05:42 +05:30
2017-09-10 17:25:29 +05:30
expect(transaction).to receive(:increment)
2018-03-17 18:26:18 +05:30
.with(:cache_delete_duration, event.duration, false)
2016-06-02 11:05:42 +05:30
2017-09-10 17:25:29 +05:30
expect(transaction).to receive(:increment)
2018-03-17 18:26:18 +05:30
.with(:cache_delete_count, 1, false)
subscriber.observe(:delete, event.duration)
end
it 'observes cache metric' do
expect(subscriber.send(:metric_cache_operation_duration_seconds))
.to receive(:observe)
2019-10-12 21:52:04 +05:30
.with({ operation: :delete }, event.duration / 1000.0)
subscriber.observe(:delete, event.duration)
end
it 'increments the operations total' do
expect(subscriber.send(:metric_cache_operations_total))
.to receive(:increment)
.with(transaction.labels.merge(operation: :delete))
2016-06-02 11:05:42 +05:30
2018-03-17 18:26:18 +05:30
subscriber.observe(:delete, event.duration)
2016-06-02 11:05:42 +05:30
end
end
end
end