2019-08-22 10:57:44 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-06-17 15:45:37 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 18:09:03 +00:00
|
|
|
RSpec.describe Gitlab::Metrics::MethodCall do
|
2020-07-28 12:09:49 +00:00
|
|
|
let(:transaction) { Gitlab::Metrics::WebTransaction.new({}) }
|
2017-09-06 18:36:25 +00:00
|
|
|
let(:method_call) { described_class.new('Foo#bar', :Foo, '#bar', transaction) }
|
2016-06-17 15:45:37 +00:00
|
|
|
|
|
|
|
describe '#measure' do
|
2018-01-22 15:44:29 +00:00
|
|
|
after do
|
2020-07-28 12:09:49 +00:00
|
|
|
::Gitlab::Metrics::Transaction.reload_metric!(:gitlab_method_call_duration_seconds)
|
2018-01-15 21:06:40 +00:00
|
|
|
end
|
|
|
|
|
2016-06-17 15:45:37 +00:00
|
|
|
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
|
2017-09-06 18:36:25 +00:00
|
|
|
|
2017-11-23 00:25:45 +00:00
|
|
|
context 'when measurement is above threshold' do
|
|
|
|
before do
|
|
|
|
allow(method_call).to receive(:above_threshold?).and_return(true)
|
|
|
|
end
|
2017-09-06 18:36:25 +00:00
|
|
|
|
2017-11-23 00:25:45 +00:00
|
|
|
context 'prometheus instrumentation is enabled' do
|
|
|
|
before do
|
2020-06-02 12:08:33 +00:00
|
|
|
stub_feature_flags(prometheus_metrics_method_instrumentation: true)
|
2017-11-23 00:25:45 +00:00
|
|
|
end
|
2017-09-06 18:36:25 +00:00
|
|
|
|
2017-12-08 18:36:30 +00:00
|
|
|
around do |example|
|
2020-08-31 15:10:41 +00:00
|
|
|
freeze_time do
|
2017-12-08 18:36:30 +00:00
|
|
|
example.run
|
|
|
|
end
|
|
|
|
end
|
2017-12-07 18:49:44 +00:00
|
|
|
|
2018-01-15 21:06:40 +00:00
|
|
|
it 'metric is not a NullMetric' do
|
2020-07-28 12:09:49 +00:00
|
|
|
method_call.measure { 'foo' }
|
|
|
|
expect(::Gitlab::Metrics::Transaction.prometheus_metric(:gitlab_method_call_duration_seconds, :histogram)).not_to be_instance_of(Gitlab::Metrics::NullMetric)
|
2017-12-07 18:49:44 +00:00
|
|
|
end
|
|
|
|
|
2017-11-23 00:25:45 +00:00
|
|
|
it 'observes the performance of the supplied block' do
|
2020-07-28 12:09:49 +00:00
|
|
|
expect(transaction)
|
|
|
|
.to receive(:observe).with(:gitlab_method_call_duration_seconds, be_a_kind_of(Numeric), { method: "#bar", module: :Foo })
|
2017-11-23 00:25:45 +00:00
|
|
|
|
|
|
|
method_call.measure { 'foo' }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'prometheus instrumentation is disabled' do
|
|
|
|
before do
|
2020-06-02 12:08:33 +00:00
|
|
|
stub_feature_flags(prometheus_metrics_method_instrumentation: false)
|
2017-11-23 00:25:45 +00:00
|
|
|
end
|
|
|
|
|
2020-07-28 12:09:49 +00:00
|
|
|
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
|
2017-11-23 00:25:45 +00:00
|
|
|
|
2020-07-28 12:09:49 +00:00
|
|
|
it 'observes using NullMetric' do
|
2017-11-23 00:25:45 +00:00
|
|
|
method_call.measure { 'foo' }
|
2020-07-28 12:09:49 +00:00
|
|
|
|
|
|
|
expect(::Gitlab::Metrics::Transaction.prometheus_metric(:gitlab_method_call_duration_seconds, :histogram)).to be_instance_of(Gitlab::Metrics::NullMetric)
|
2017-11-23 00:25:45 +00:00
|
|
|
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-07-28 12:09:49 +00:00
|
|
|
expect(transaction)
|
2017-11-23 00:25:45 +00:00
|
|
|
.not_to receive(:observe)
|
2020-07-28 12:09:49 +00:00
|
|
|
.with(:gitlab_method_call_duration_seconds, be_a_kind_of(Numeric))
|
2017-11-23 00:25:45 +00:00
|
|
|
|
|
|
|
method_call.measure { 'foo' }
|
|
|
|
end
|
2017-09-06 18:36:25 +00:00
|
|
|
end
|
2016-06-17 15:45:37 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '#above_threshold?' do
|
2017-11-23 14:28:37 +00:00
|
|
|
before do
|
|
|
|
allow(Gitlab::Metrics).to receive(:method_call_threshold).and_return(100)
|
|
|
|
end
|
|
|
|
|
2016-06-17 15:45:37 +00:00
|
|
|
it 'returns false when the total call time is not above the threshold' do
|
2017-12-20 18:30:58 +00:00
|
|
|
expect(method_call).to receive(:real_time).and_return(0.009)
|
2017-11-23 14:28:37 +00:00
|
|
|
|
2016-06-17 15:45:37 +00:00
|
|
|
expect(method_call.above_threshold?).to eq(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns true when the total call time is above the threshold' do
|
2017-12-20 18:30:58 +00:00
|
|
|
expect(method_call).to receive(:real_time).and_return(9)
|
2016-06-17 15:45:37 +00:00
|
|
|
|
|
|
|
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
|