mirror of
https://github.com/wahyd4/gitlabhq.git
synced 2026-08-11 05:36:07 +10:00
Track method call times/counts as a single metric This changes method call tracking so only a single metric is emitted regardless of the number of calls. This allows us to more accurately measure the total execution time of a method as well as the number of times a method is called. See 851e3ff7578973c2206628424eac3b951a3c656d for more details. Method call tracking tracked calls individually meaning the end statistics may not always be accurate enough to get a good understanding of where time is spent. See merge request !4754
92 lines
2.3 KiB
Ruby
92 lines
2.3 KiB
Ruby
require 'spec_helper'
|
|
|
|
describe Gitlab::Metrics::MethodCall do
|
|
let(:method_call) { described_class.new('Foo#bar', 'foo') }
|
|
|
|
describe '#measure' do
|
|
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
|
|
end
|
|
|
|
describe '#to_metric' do
|
|
it 'returns a Metric instance' do
|
|
method_call.measure { 'foo' }
|
|
metric = method_call.to_metric
|
|
|
|
expect(metric).to be_an_instance_of(Gitlab::Metrics::Metric)
|
|
expect(metric.series).to eq('foo')
|
|
|
|
expect(metric.values[:duration]).to be_a_kind_of(Numeric)
|
|
expect(metric.values[:cpu_duration]).to be_a_kind_of(Numeric)
|
|
expect(metric.values[:call_count]).to an_instance_of(Fixnum)
|
|
|
|
expect(metric.tags).to eq({ method: 'Foo#bar' })
|
|
end
|
|
end
|
|
|
|
describe '#above_threshold?' do
|
|
it 'returns false when the total call time is not above the threshold' do
|
|
expect(method_call.above_threshold?).to eq(false)
|
|
end
|
|
|
|
it 'returns true when the total call time is above the threshold' do
|
|
expect(method_call).to receive(:real_time).and_return(9000)
|
|
|
|
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
|