summaryrefslogtreecommitdiff
path: root/lib/gitlab/database/background_migration/batch_metrics.rb
blob: 14fe0c14c24a3467a656c9f4305c70be5517a9c0 (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
# frozen_string_literal: true

module Gitlab
  module Database
    module BackgroundMigration
      class BatchMetrics
        attr_reader :timings
        attr_reader :affected_rows

        def initialize
          @timings = {}
          @affected_rows = {}
        end

        def time_operation(label, &blk)
          instrument_operation(label, instrument_affected_rows: false, &blk)
        end

        def instrument_operation(label, instrument_affected_rows: true)
          start_time = monotonic_time

          count = yield

          timings_for_label(label) << monotonic_time - start_time
          affected_rows_for_label(label) << count if instrument_affected_rows && count.is_a?(Integer)
        end

        private

        def timings_for_label(label)
          timings[label] ||= []
        end

        def affected_rows_for_label(label)
          affected_rows[label] ||= []
        end

        def monotonic_time
          Gitlab::Metrics::System.monotonic_time
        end
      end
    end
  end
end