2019-12-04 20:38:33 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-06-22 15:30:34 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
RSpec.describe Gitlab::Metrics::MethodCall do
|
2020-10-24 23:57:45 +05:30
|
|
|
let(:transaction) { Gitlab::Metrics::WebTransaction.new({}) }
|
2018-03-17 18:26:18 +05:30
|
|
|
let(:method_call) { described_class.new('Foo#bar', :Foo, '#bar', transaction) }
|
2016-06-22 15:30:34 +05:30
|
|
|
|
|
|
|
describe '#measure' do
|
2018-03-17 18:26:18 +05:30
|
|
|
after do
|
2020-10-24 23:57:45 +05:30
|
|
|
::Gitlab::Metrics::Transaction.reload_metric!(:gitlab_method_call_duration_seconds)
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
2016-06-22 15:30:34 +05:30
|
|
|
it 'measures the performance of the supplied block' do
|
|
|
|
method_call.measure { 'foo' }
|
|
|
|
|
|
|
|
expect(method_call.real_time).to be_a_kind_of(Numeric)
|
|
|
|
expect(method_call.cpu_time).to be_a_kind_of(Numeric)
|
|
|
|
expect(method_call.call_count).to eq(1)
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
context 'when measurement is above threshold' do
|
|
|
|
before do
|
|
|
|
allow(method_call).to receive(:above_threshold?).and_return(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'prometheus instrumentation is enabled' do
|
|
|
|
before do
|
2020-06-23 00:09:42 +05:30
|
|
|
stub_feature_flags(prometheus_metrics_method_instrumentation: true)
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
around do |example|
|
2020-11-24 15:15:51 +05:30
|
|
|
freeze_time do
|
2018-03-17 18:26:18 +05:30
|
|
|
example.run
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'metric is not a NullMetric' do
|
2020-10-24 23:57:45 +05:30
|
|
|
method_call.measure { 'foo' }
|
|
|
|
expect(::Gitlab::Metrics::Transaction.prometheus_metric(:gitlab_method_call_duration_seconds, :histogram)).not_to be_instance_of(Gitlab::Metrics::NullMetric)
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'observes the performance of the supplied block' do
|
2020-10-24 23:57:45 +05:30
|
|
|
expect(transaction)
|
|
|
|
.to receive(:observe).with(:gitlab_method_call_duration_seconds, be_a_kind_of(Numeric), { method: "#bar", module: :Foo })
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
method_call.measure { 'foo' }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'prometheus instrumentation is disabled' do
|
|
|
|
before do
|
2020-06-23 00:09:42 +05:30
|
|
|
stub_feature_flags(prometheus_metrics_method_instrumentation: false)
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
it 'observes the performance of the supplied block' do
|
|
|
|
expect(transaction)
|
|
|
|
.to receive(:observe).with(:gitlab_method_call_duration_seconds, be_a_kind_of(Numeric), { method: "#bar", module: :Foo })
|
|
|
|
|
|
|
|
method_call.measure { 'foo' }
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
it 'observes using NullMetric' do
|
2018-03-17 18:26:18 +05:30
|
|
|
method_call.measure { 'foo' }
|
2020-10-24 23:57:45 +05:30
|
|
|
|
|
|
|
expect(::Gitlab::Metrics::Transaction.prometheus_metric(:gitlab_method_call_duration_seconds, :histogram)).to be_instance_of(Gitlab::Metrics::NullMetric)
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when measurement is below threshold' do
|
|
|
|
before do
|
|
|
|
allow(method_call).to receive(:above_threshold?).and_return(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not observe the performance' do
|
2020-10-24 23:57:45 +05:30
|
|
|
expect(transaction)
|
2018-03-17 18:26:18 +05:30
|
|
|
.not_to receive(:observe)
|
2020-10-24 23:57:45 +05:30
|
|
|
.with(:gitlab_method_call_duration_seconds, be_a_kind_of(Numeric))
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
method_call.measure { 'foo' }
|
|
|
|
end
|
|
|
|
end
|
2016-06-22 15:30:34 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
describe '#above_threshold?' do
|
2018-03-17 18:26:18 +05:30
|
|
|
before do
|
|
|
|
allow(Gitlab::Metrics).to receive(:method_call_threshold).and_return(100)
|
|
|
|
end
|
|
|
|
|
2016-06-22 15:30:34 +05:30
|
|
|
it 'returns false when the total call time is not above the threshold' do
|
2018-03-17 18:26:18 +05:30
|
|
|
expect(method_call).to receive(:real_time).and_return(0.009)
|
|
|
|
|
2016-06-22 15:30:34 +05:30
|
|
|
expect(method_call.above_threshold?).to eq(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns true when the total call time is above the threshold' do
|
2018-03-17 18:26:18 +05:30
|
|
|
expect(method_call).to receive(:real_time).and_return(9)
|
2016-06-22 15:30:34 +05:30
|
|
|
|
|
|
|
expect(method_call.above_threshold?).to eq(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#call_count' do
|
|
|
|
context 'without any method calls' do
|
|
|
|
it 'returns 0' do
|
|
|
|
expect(method_call.call_count).to eq(0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with method calls' do
|
|
|
|
it 'returns the number of method calls' do
|
|
|
|
method_call.measure { 'foo' }
|
|
|
|
|
|
|
|
expect(method_call.call_count).to eq(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#cpu_time' do
|
|
|
|
context 'without timings' do
|
|
|
|
it 'returns 0.0' do
|
|
|
|
expect(method_call.cpu_time).to eq(0.0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with timings' do
|
|
|
|
it 'returns the total CPU time' do
|
|
|
|
method_call.measure { 'foo' }
|
|
|
|
|
|
|
|
expect(method_call.cpu_time >= 0.0).to be(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#real_time' do
|
|
|
|
context 'without timings' do
|
|
|
|
it 'returns 0.0' do
|
|
|
|
expect(method_call.real_time).to eq(0.0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with timings' do
|
|
|
|
it 'returns the total real time' do
|
|
|
|
method_call.measure { 'foo' }
|
|
|
|
|
|
|
|
expect(method_call.real_time >= 0.0).to be(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|