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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

210 lines
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'
2020-07-28 23:09:34 +05:30
RSpec.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 }
2023-03-04 22:38:38 +05:30
let(:event) { double(:event, duration: 15.2, payload: { key: %w[a b c] }) }
2016-06-02 11:05:42 +05:30
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 }) }
context 'when super operation is fetch' do
let(:event) { double(:event, duration: 15.2, payload: { hit: true, super_operation: :fetch }) }
2020-10-24 23:57:45 +05:30
it 'does not increment cache read miss total' do
2017-09-10 17:25:29 +05:30
expect(transaction).not_to receive(:increment)
2020-10-24 23:57:45 +05:30
.with(:gitlab_cache_misses_total, 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 }) }
2020-10-24 23:57:45 +05:30
it 'increments the cache_read_miss total' do
2017-09-10 17:25:29 +05:30
expect(transaction).to receive(:increment)
2020-10-24 23:57:45 +05:30
.with(:gitlab_cache_misses_total, 1)
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
2016-08-24 12:49:21 +05:30
context 'when super operation is fetch' do
let(:event) { double(:event, duration: 15.2, payload: { hit: false, super_operation: :fetch }) }
2020-10-24 23:57:45 +05:30
it 'does not increment cache read miss total' do
2017-09-10 17:25:29 +05:30
expect(transaction).not_to receive(:increment)
2020-10-24 23:57:45 +05:30
.with(:gitlab_cache_misses_total, 1)
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
2023-03-04 22:38:38 +05:30
describe '#cache_read_multi' do
subject { subscriber.cache_read_multi(event) }
context 'with a transaction' do
before do
allow(subscriber).to receive(:current_transaction)
.and_return(transaction)
end
it 'observes multi-key count' do
expect(transaction).to receive(:observe)
.with(:gitlab_cache_read_multikey_count, event.payload[:key].size)
subject
end
end
context 'with no transaction' do
it 'does not observes multi-key count' do
expect(transaction).not_to receive(:observe)
.with(:gitlab_cache_read_multikey_count, event.payload[:key].size)
subject
end
end
it 'observes read_multi duration' do
expect(subscriber).to receive(:observe)
.with(:read_multi, event.duration)
subject
end
end
2016-06-02 11:05:42 +05:30
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)
2020-10-24 23:57:45 +05:30
.with(:gitlab_transaction_cache_read_hit_count_total, 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
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
2020-10-24 23:57:45 +05:30
it 'increments the cache_fetch_miss count and cache_read_miss total' do
expect(transaction).to receive(:increment).with(:gitlab_cache_misses_total, 1)
2017-09-10 17:25:29 +05:30
expect(transaction).to receive(:increment)
2020-10-24 23:57:45 +05:30
.with(:gitlab_transaction_cache_read_miss_count_total, 1)
2016-08-24 12:49:21 +05:30
subscriber.cache_generate(event)
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
2018-03-17 18:26:18 +05:30
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
2020-10-24 23:57:45 +05:30
expect(transaction)
2019-10-12 21:52:04 +05:30
.to receive(:increment)
2020-10-24 23:57:45 +05:30
.with(:gitlab_cache_operations_total, 1, { 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