diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-06-18 11:18:50 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-06-18 11:18:50 +0000 |
commit | 8c7f4e9d5f36cff46365a7f8c4b9c21578c1e781 (patch) | |
tree | a77e7fe7a93de11213032ed4ab1f33a3db51b738 /spec/lib/gitlab/instrumentation | |
parent | 00b35af3db1abfe813a778f643dad221aad51fca (diff) | |
download | gitlab-ce-8c7f4e9d5f36cff46365a7f8c4b9c21578c1e781.tar.gz |
Add latest changes from gitlab-org/gitlab@13-1-stable-ee
Diffstat (limited to 'spec/lib/gitlab/instrumentation')
-rw-r--r-- | spec/lib/gitlab/instrumentation/redis_base_spec.rb | 144 | ||||
-rw-r--r-- | spec/lib/gitlab/instrumentation/redis_interceptor_spec.rb | 45 | ||||
-rw-r--r-- | spec/lib/gitlab/instrumentation/redis_spec.rb | 114 |
3 files changed, 303 insertions, 0 deletions
diff --git a/spec/lib/gitlab/instrumentation/redis_base_spec.rb b/spec/lib/gitlab/instrumentation/redis_base_spec.rb new file mode 100644 index 00000000000..5ea8f00114e --- /dev/null +++ b/spec/lib/gitlab/instrumentation/redis_base_spec.rb @@ -0,0 +1,144 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe Gitlab::Instrumentation::RedisBase, :request_store do + let(:instrumentation_class_a) do + stub_const('InstanceA', Class.new(described_class)) + end + + let(:instrumentation_class_b) do + stub_const('InstanceB', Class.new(described_class)) + end + + describe '.storage_key' do + it 'returns the class name with underscore' do + expect(instrumentation_class_a.storage_key).to eq('instance_a') + expect(instrumentation_class_b.storage_key).to eq('instance_b') + end + end + + describe '.known_payload_keys' do + it 'returns generated payload keys' do + expect(instrumentation_class_a.known_payload_keys).to eq([:redis_instance_a_calls, + :redis_instance_a_duration_s, + :redis_instance_a_read_bytes, + :redis_instance_a_write_bytes]) + end + + it 'does not call calculation methods' do + expect(instrumentation_class_a).not_to receive(:get_request_count) + expect(instrumentation_class_a).not_to receive(:query_time) + expect(instrumentation_class_a).not_to receive(:read_bytes) + expect(instrumentation_class_a).not_to receive(:write_bytes) + + instrumentation_class_a.known_payload_keys + end + end + + describe '.payload' do + it 'returns values that are higher than 0' do + allow(instrumentation_class_a).to receive(:get_request_count) { 1 } + allow(instrumentation_class_a).to receive(:query_time) { 0.1 } + allow(instrumentation_class_a).to receive(:read_bytes) { 0.0 } + allow(instrumentation_class_a).to receive(:write_bytes) { 123 } + + expected_payload = { + redis_instance_a_calls: 1, + redis_instance_a_write_bytes: 123, + redis_instance_a_duration_s: 0.1 + } + + expect(instrumentation_class_a.payload).to eq(expected_payload) + end + end + + describe '.add_duration' do + it 'does not lose precision while adding' do + precision = 1.0 / (10**::Gitlab::InstrumentationHelper::DURATION_PRECISION) + 2.times { instrumentation_class_a.add_duration(0.4 * precision) } + + # 2 * 0.4 should be 0.8 and get rounded to 1 + expect(instrumentation_class_a.query_time).to eq(1 * precision) + end + + context 'storage key overlapping' do + it 'keys do not overlap across storages' do + instrumentation_class_a.add_duration(0.4) + instrumentation_class_b.add_duration(0.5) + + expect(instrumentation_class_a.query_time).to eq(0.4) + expect(instrumentation_class_b.query_time).to eq(0.5) + end + end + end + + describe '.increment_request_count' do + context 'storage key overlapping' do + it 'keys do not overlap across storages' do + 3.times { instrumentation_class_a.increment_request_count } + 2.times { instrumentation_class_b.increment_request_count } + + expect(instrumentation_class_a.get_request_count).to eq(3) + expect(instrumentation_class_b.get_request_count).to eq(2) + end + end + end + + describe '.increment_write_bytes' do + context 'storage key overlapping' do + it 'keys do not overlap across storages' do + 2.times do + instrumentation_class_a.increment_write_bytes(42) + instrumentation_class_b.increment_write_bytes(77) + end + + expect(instrumentation_class_a.write_bytes).to eq(42 * 2) + expect(instrumentation_class_b.write_bytes).to eq(77 * 2) + end + end + end + + describe '.increment_read_bytes' do + context 'storage key overlapping' do + it 'keys do not overlap across storages' do + 2.times do + instrumentation_class_a.increment_read_bytes(42) + instrumentation_class_b.increment_read_bytes(77) + end + + expect(instrumentation_class_a.read_bytes).to eq(42 * 2) + expect(instrumentation_class_b.read_bytes).to eq(77 * 2) + end + end + end + + describe '.add_call_details' do + before do + allow(Gitlab::PerformanceBar).to receive(:enabled_for_request?) { true } + end + + context 'storage key overlapping' do + it 'keys do not overlap across storages' do + 2.times do + instrumentation_class_a.add_call_details(0.3, [:set]) + instrumentation_class_b.add_call_details(0.4, [:set]) + end + + expect(instrumentation_class_a.detail_store).to match( + [ + a_hash_including(cmd: :set, duration: 0.3, backtrace: an_instance_of(Array)), + a_hash_including(cmd: :set, duration: 0.3, backtrace: an_instance_of(Array)) + ] + ) + + expect(instrumentation_class_b.detail_store).to match( + [ + a_hash_including(cmd: :set, duration: 0.4, backtrace: an_instance_of(Array)), + a_hash_including(cmd: :set, duration: 0.4, backtrace: an_instance_of(Array)) + ] + ) + end + end + end +end diff --git a/spec/lib/gitlab/instrumentation/redis_interceptor_spec.rb b/spec/lib/gitlab/instrumentation/redis_interceptor_spec.rb new file mode 100644 index 00000000000..25506d63091 --- /dev/null +++ b/spec/lib/gitlab/instrumentation/redis_interceptor_spec.rb @@ -0,0 +1,45 @@ +# frozen_string_literal: true + +require 'spec_helper' +require 'rspec-parameterized' + +describe Gitlab::Instrumentation::RedisInterceptor, :clean_gitlab_redis_shared_state, :request_store do + using RSpec::Parameterized::TableSyntax + + describe 'read and write' do + where(:setup, :command, :expect_write, :expect_read) do + # The response is 'OK', the request size is the combined size of array + # elements. Exercise counting of a status reply. + [] | [:set, 'foo', 'bar'] | 3 + 3 + 3 | 2 + + # The response is 1001, so 4 bytes. Exercise counting an integer reply. + [[:set, 'foobar', 1000]] | [:incr, 'foobar'] | 4 + 6 | 4 + + # Exercise counting empty multi bulk reply + [] | [:hgetall, 'foobar'] | 7 + 6 | 0 + + # Hgetall response length is combined length of keys and values in the + # hash. Exercises counting of a multi bulk reply + [[:hset, 'myhash', 'field', 'hello world']] | [:hgetall, 'myhash'] | 7 + 6 | 5 + 11 + + # Exercise counting of a bulk reply + [[:set, 'foo', 'bar' * 100]] | [:get, 'foo'] | 3 + 3 | 3 * 100 + + # Nested array response: ['123456-89', ['foo', 'bar']] + [[:xadd, 'mystream', '123456-89', 'foo', 'bar']] | [:xrange, 'mystream', '-', '+'] | 6 + 8 + 1 + 1 | 9 + 3 + 3 + end + + with_them do + it 'counts bytes read and written' do + Gitlab::Redis::SharedState.with do |redis| + setup.each { |cmd| redis.call(cmd) } + RequestStore.clear! + redis.call(command) + end + + expect(Gitlab::Instrumentation::Redis.read_bytes).to eq(expect_read) + expect(Gitlab::Instrumentation::Redis.write_bytes).to eq(expect_write) + end + end + end +end diff --git a/spec/lib/gitlab/instrumentation/redis_spec.rb b/spec/lib/gitlab/instrumentation/redis_spec.rb new file mode 100644 index 00000000000..8311c4f5bbb --- /dev/null +++ b/spec/lib/gitlab/instrumentation/redis_spec.rb @@ -0,0 +1,114 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe Gitlab::Instrumentation::Redis do + def stub_storages(method, value) + described_class::STORAGES.each do |storage| + allow(storage).to receive(method) { value } + end + end + + shared_examples 'aggregation of redis storage data' do |method| + describe "#{method} sum" do + it "sums data from all Redis storages" do + amount = 0.3 + + stub_storages(method, amount) + + expect(described_class.public_send(method)).to eq(described_class::STORAGES.size * amount) + end + end + end + + it_behaves_like 'aggregation of redis storage data', :get_request_count + it_behaves_like 'aggregation of redis storage data', :query_time + it_behaves_like 'aggregation of redis storage data', :read_bytes + it_behaves_like 'aggregation of redis storage data', :write_bytes + + describe '.known_payload_keys' do + it 'returns all known payload keys' do + expected_keys = [ + :redis_calls, + :redis_duration_s, + :redis_read_bytes, + :redis_write_bytes, + :redis_action_cable_calls, + :redis_action_cable_duration_s, + :redis_action_cable_read_bytes, + :redis_action_cable_write_bytes, + :redis_cache_calls, + :redis_cache_duration_s, + :redis_cache_read_bytes, + :redis_cache_write_bytes, + :redis_queues_calls, + :redis_queues_duration_s, + :redis_queues_read_bytes, + :redis_queues_write_bytes, + :redis_shared_state_calls, + :redis_shared_state_duration_s, + :redis_shared_state_read_bytes, + :redis_shared_state_write_bytes + ] + + expect(described_class.known_payload_keys).to eq(expected_keys) + end + + it 'does not call storage calculation methods' do + described_class::STORAGES.each do |storage| + expect(storage).not_to receive(:get_request_count) + expect(storage).not_to receive(:query_time) + expect(storage).not_to receive(:read_bytes) + expect(storage).not_to receive(:write_bytes) + end + + described_class.known_payload_keys + end + end + + describe '.payload', :request_store do + before do + Gitlab::Redis::Cache.with { |redis| redis.set('cache-test', 321) } + Gitlab::Redis::SharedState.with { |redis| redis.set('shared-state-test', 123) } + end + + it 'returns payload filtering out zeroed values' do + expected_payload = { + # Aggregated results + redis_calls: 2, + redis_duration_s: be >= 0, + redis_read_bytes: be >= 0, + redis_write_bytes: be >= 0, + + # Cache results + redis_cache_calls: 1, + redis_cache_duration_s: be >= 0, + redis_cache_read_bytes: be >= 0, + redis_cache_write_bytes: be >= 0, + + # Shared state results + redis_shared_state_calls: 1, + redis_shared_state_duration_s: be >= 0, + redis_shared_state_read_bytes: be >= 0, + redis_shared_state_write_bytes: be >= 0 + } + + expect(described_class.payload).to include(expected_payload) + expect(described_class.payload.keys).to match_array(expected_payload.keys) + end + end + + describe '.detail_store' do + it 'returns a flat array of detail stores with the storage name added to each item' do + details_row = { cmd: 'GET foo', duration: 1 } + + stub_storages(:detail_store, [details_row]) + + expect(described_class.detail_store) + .to contain_exactly(details_row.merge(storage: 'ActionCable'), + details_row.merge(storage: 'Cache'), + details_row.merge(storage: 'Queues'), + details_row.merge(storage: 'SharedState')) + end + end +end |