Welcome to mirror list, hosted at ThFree Co, Russian Federation.

batch_metrics_spec.rb « background_migration « database « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6db3081ca7e874afc4870b7ef73e700fa0551d47 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Database::BackgroundMigration::BatchMetrics do
  let(:batch_metrics) { described_class.new }

  describe '#time_operation' do
    it 'tracks the duration of the operation using monotonic time' do
      expect(batch_metrics.timings).to be_empty

      expect(Gitlab::Metrics::System).to receive(:monotonic_time)
        .and_return(0.0, 111.0, 200.0, 290.0, 300.0, 410.0)

      batch_metrics.time_operation(:my_label) do
        # some operation
      end

      batch_metrics.time_operation(:my_other_label) do
        # some operation
      end

      batch_metrics.time_operation(:my_label) do
        # some operation
      end

      expect(batch_metrics.timings).to eq(my_label: [111.0, 110.0], my_other_label: [90.0])
    end
  end

  describe '#instrument_operation' do
    it 'tracks duration and affected rows' do
      expect(batch_metrics.timings).to be_empty
      expect(batch_metrics.affected_rows).to be_empty

      expect(Gitlab::Metrics::System).to receive(:monotonic_time)
        .and_return(0.0, 111.0, 200.0, 290.0, 300.0, 410.0, 420.0, 450.0)

      batch_metrics.instrument_operation(:my_label) do
        3
      end

      batch_metrics.instrument_operation(:my_other_label) do
        42
      end

      batch_metrics.instrument_operation(:my_label) do
        2
      end

      batch_metrics.instrument_operation(:my_other_label) do
        :not_an_integer
      end

      expect(batch_metrics.timings).to eq(my_label: [111.0, 110.0], my_other_label: [90.0, 30.0])
      expect(batch_metrics.affected_rows).to eq(my_label: [3, 2], my_other_label: [42])
    end
  end
end