summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/sherlock/line_profiler_spec.rb
blob: c19976068394022493cd300f00df28ad13d18afa (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# frozen_string_literal: true

require 'spec_helper'

describe Gitlab::Sherlock::LineProfiler do
  let(:profiler) { described_class.new }

  describe '#profile' do
    it 'runs the profiler when using MRI' do
      allow(profiler).to receive(:mri?).and_return(true)
      allow(profiler).to receive(:profile_mri)

      profiler.profile { 'cats' }
    end

    it 'raises NotImplementedError when profiling an unsupported platform' do
      allow(profiler).to receive(:mri?).and_return(false)

      expect { profiler.profile { 'cats' } }.to raise_error(NotImplementedError)
    end
  end

  describe '#profile_mri' do
    it 'returns an Array containing the return value and profiling samples' do
      allow(profiler).to receive(:lineprof)
        .and_yield
        .and_return({ __FILE__ => [[0, 0, 0, 0]] })

      retval, samples = profiler.profile_mri { 42 }

      expect(retval).to eq(42)
      expect(samples).to eq([])
    end
  end

  describe '#aggregate_rblineprof' do
    let(:raw_samples) do
      { __FILE__ => [[30000, 30000, 5, 0], [15000, 15000, 4, 0]] }
    end

    it 'returns an Array of FileSample objects' do
      samples = profiler.aggregate_rblineprof(raw_samples)

      expect(samples).to be_an_instance_of(Array)
      expect(samples[0]).to be_an_instance_of(Gitlab::Sherlock::FileSample)
    end

    describe 'the first FileSample object' do
      let(:file_sample) do
        profiler.aggregate_rblineprof(raw_samples)[0]
      end

      it 'uses the correct file path' do
        expect(file_sample.file).to eq(__FILE__)
      end

      it 'contains a list of line samples' do
        line_sample = file_sample.line_samples[0]

        expect(line_sample).to be_an_instance_of(Gitlab::Sherlock::LineSample)

        expect(line_sample.duration).to eq(15.0)
        expect(line_sample.events).to eq(4)
      end

      it 'contains the total file execution time' do
        expect(file_sample.duration).to eq(30.0)
      end

      it 'contains the total amount of file events' do
        expect(file_sample.events).to eq(5)
      end
    end
  end
end