File: profiler_spec.rb

package info (click to toggle)
ruby-test-prof 1.6.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 15,448 kB
  • sloc: ruby: 13,093; sh: 4; makefile: 4
file content (121 lines) | stat: -rw-r--r-- 3,318 bytes parent folder | download
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# frozen_string_literal: true

describe TestProf::EventProf::Profiler do
  let(:rank_by) { :time }
  let(:top_count) { 5 }
  let(:per_example) { false }

  let(:options) do
    {
      rank_by: rank_by,
      top_count: top_count,
      per_example: per_example
    }
  end

  subject { described_class.new(event: "test.event", instrumenter: InstrumenterStub, **options) }

  describe "#result" do
    before(:each) do
      allow(subject).to receive(:take_time).and_return(500)
    end

    let(:results) do
      subject

      subject.group_started "A"

      subject.example_started "A1"
      InstrumenterStub.notify "test.event", 100
      subject.example_finished "A1"

      subject.group_finished "A"

      subject.group_started "B"

      subject.example_started "B1"
      InstrumenterStub.notify "test.event", 140
      InstrumenterStub.notify "test.event", 240
      subject.example_finished "B1"

      subject.example_started "B2"
      InstrumenterStub.notify "test.event", 40
      subject.example_finished "B2"

      subject.group_finished "B"

      subject.group_started "C"

      subject.example_started "C1"
      InstrumenterStub.notify "test.event", 400
      InstrumenterStub.notify "test.event", 40
      subject.example_finished "C1"

      subject.example_started "C2"
      subject.example_finished "C2"

      subject.group_finished "C"

      subject.results
    end

    it "returns top slow groups and totals" do
      expect(results).to eq(
        groups: [
          {id: "C", examples: 2, run_time: 500, time: 440, count: 2},
          {id: "B", examples: 2, run_time: 500, time: 420, count: 3},
          {id: "A", examples: 1, run_time: 500, time: 100, count: 1}
        ]
      )
      expect(subject.total_time).to eq 960
      expect(subject.total_count).to eq 6
    end

    context "when rank by count" do
      let(:rank_by) { :count }

      it "returns top groups by event occurances" do
        expect(results).to eq(
          groups: [
            {id: "B", examples: 2, run_time: 500, time: 420, count: 3},
            {id: "C", examples: 2, run_time: 500, time: 440, count: 2},
            {id: "A", examples: 1, run_time: 500, time: 100, count: 1}
          ]
        )
      end
    end

    context "when top_count is specified" do
      let(:top_count) { 2 }

      it "returns top groups by event occurances" do
        expect(results).to eq(
          groups: [
            {id: "C", examples: 2, run_time: 500, time: 440, count: 2},
            {id: "B", examples: 2, run_time: 500, time: 420, count: 3}
          ]
        )
      end
    end

    context "when per_example is true" do
      let(:per_example) { true }

      it "returns top groups and examples" do
        expect(results).to eq(
          groups: [
            {id: "C", examples: 2, run_time: 500, time: 440, count: 2},
            {id: "B", examples: 2, run_time: 500, time: 420, count: 3},
            {id: "A", examples: 1, run_time: 500, time: 100, count: 1}
          ],
          examples: [
            {id: "C1", run_time: 500, time: 440, count: 2},
            {id: "B1", run_time: 500, time: 380, count: 2},
            {id: "A1", run_time: 500, time: 100, count: 1},
            {id: "B2", run_time: 500, time: 40, count: 1}
          ]
        )
      end
    end
  end
end