diff options
author | Robert Speicher <robert@gitlab.com> | 2016-04-06 20:13:15 +0300 |
---|---|---|
committer | Robert Speicher <robert@gitlab.com> | 2016-04-06 20:13:15 +0300 |
commit | 755edd8004132fbbbaee3b8d64961bcfd588e507 (patch) | |
tree | bbcecb65f1065b817b4f60ef6956f947c9249f66 /spec | |
parent | 801e870ddbce07b9171c08d739ea40007895a0cd (diff) | |
parent | 352bc7d2b9a4ed5725e4f0e9f2e63d71678de9bc (diff) |
Merge branch 'metrics-measure-block' into 'master'
Support for measuring Ruby blocks using GitLab performance monitoring
This adds support for measuring timings of arbitrary Ruby blocks.
Fixes #14710
See merge request !3515
Diffstat (limited to 'spec')
-rw-r--r-- | spec/lib/gitlab/metrics_spec.rb | 47 |
1 files changed, 44 insertions, 3 deletions
diff --git a/spec/lib/gitlab/metrics_spec.rb b/spec/lib/gitlab/metrics_spec.rb index 0ec8a6dc5cb..8f63a5f2043 100644 --- a/spec/lib/gitlab/metrics_spec.rb +++ b/spec/lib/gitlab/metrics_spec.rb @@ -13,7 +13,7 @@ describe Gitlab::Metrics do end end - describe '#submit_metrics' do + describe '.submit_metrics' do it 'prepares and writes the metrics to InfluxDB' do connection = double(:connection) pool = double(:pool) @@ -26,7 +26,7 @@ describe Gitlab::Metrics do end end - describe '#prepare_metrics' do + describe '.prepare_metrics' do it 'returns a Hash with the keys as Symbols' do metrics = described_class. prepare_metrics([{ 'values' => {}, 'tags' => {} }]) @@ -51,7 +51,7 @@ describe Gitlab::Metrics do end end - describe '#escape_value' do + describe '.escape_value' do it 'escapes an equals sign' do expect(described_class.escape_value('foo=')).to eq('foo\\=') end @@ -60,4 +60,45 @@ describe Gitlab::Metrics do expect(described_class.escape_value(10)).to eq('10') end end + + describe '.measure' do + context 'without a transaction' do + it 'returns the return value of the block' do + val = Gitlab::Metrics.measure(:foo) { 10 } + + expect(val).to eq(10) + end + end + + context 'with a transaction' do + let(:transaction) { Gitlab::Metrics::Transaction.new } + + before do + allow(Gitlab::Metrics::Transaction).to receive(:current). + and_return(transaction) + end + + it 'adds a metric to the current transaction' do + expect(transaction).to receive(:add_metric). + with(:foo, { duration: a_kind_of(Numeric) }, { tag: 'value' }) + + Gitlab::Metrics.measure(:foo, {}, tag: 'value') { 10 } + end + + it 'supports adding of custom values' do + values = { duration: a_kind_of(Numeric), number: 10 } + + expect(transaction).to receive(:add_metric). + with(:foo, values, { tag: 'value' }) + + Gitlab::Metrics.measure(:foo, { number: 10 }, tag: 'value') { 10 } + end + + it 'returns the return value of the block' do + val = Gitlab::Metrics.measure(:foo) { 10 } + + expect(val).to eq(10) + end + end + end end |