File: tracker_spec.rb

package info (click to toggle)
ruby-test-prof 1.6.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 15,448 kB
  • sloc: ruby: 13,093; sh: 4; makefile: 4
file content (222 lines) | stat: -rw-r--r-- 5,612 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# frozen_string_literal: true

shared_examples "TestProf::MemoryProf::Tracker" do
  let(:list) { TestProf::MemoryProf::Tracker::LinkedList.new(100) }
  let(:example) { double("example") }
  let(:group) { double("group") }

  describe "#start" do
    it "initializes a linked list" do
      subject.start

      expect(subject.list).to be_kind_of(TestProf::MemoryProf::Tracker::LinkedList)
    end
  end

  describe "#finish" do
    before do
      allow(subject).to receive(:track).and_return(200)
      allow(subject).to receive(:list).and_return(list)
    end

    it "sets total_memory" do
      subject.finish

      expect(subject.total_memory).to eq(100)
    end
  end

  describe "#example_started" do
    let(:example_started) { subject.example_started(example, {name: :example}) }

    before do
      allow(subject).to receive(:track).and_return(200)
      allow(subject).to receive(:list).and_return(list)
    end

    it "tracks memory at the start of an example" do
      example_started

      expect(subject.list.head).to have_attributes(id: example, item: {name: :example}, memory_at_start: 200)
    end
  end

  describe "#example_finished" do
    let(:example_finished) { subject.example_finished(example) }

    before do
      list.add_node(example, {name: :example}, 200)

      allow(subject).to receive(:track).and_return(350)
      allow(subject).to receive(:list).and_return(list)
    end

    context "when the example memory > the memory of the top examples" do
      before do
        [75, 100, 125, 175, 200].each do |memory|
          subject.examples << {memory: memory}
        end
      end

      it "adds the example to the top examples" do
        example_finished

        expect(subject.examples).to match_array([
          {memory: 200},
          {memory: 175},
          {name: :example, memory: 150},
          {memory: 125},
          {memory: 100}
        ])
      end
    end

    context "when the example memory <= the memory of the top examples" do
      before do
        [175, 200, 225, 250, 275].each do |memory|
          subject.examples << {memory: memory}
        end
      end

      it "adds the example to the top examples" do
        example_finished

        expect(subject.examples).to match_array([
          {memory: 275},
          {memory: 250},
          {memory: 225},
          {memory: 200},
          {memory: 175}
        ])
      end
    end
  end

  describe "#group_started" do
    let(:group_started) { subject.group_started(group, {name: :group}) }

    before do
      allow(subject).to receive(:track).and_return(200)
      allow(subject).to receive(:list).and_return(list)
    end

    it "tracks memory at the start of a group" do
      group_started

      expect(subject.list.head).to have_attributes(id: group, item: {name: :group}, memory_at_start: 200)
    end
  end

  describe "#group_finished" do
    let(:group_finished) { subject.group_finished(group) }

    before do
      list.add_node(group, {name: :group}, 200)

      allow(subject).to receive(:track).and_return(350)
      allow(subject).to receive(:list).and_return(list)
    end

    context "when the group memory > the memory of the top groups" do
      before do
        [75, 100, 125, 175, 200].each do |memory|
          subject.groups << {memory: memory}
        end
      end

      it "adds the group to the top groups" do
        group_finished

        expect(subject.groups).to match_array([
          {memory: 200},
          {memory: 175},
          {name: :group, memory: 150},
          {memory: 125},
          {memory: 100}
        ])
      end
    end

    context "when the group memory <= the memory of the top groups" do
      before do
        [175, 200, 225, 250, 275].each do |memory|
          subject.groups << {memory: memory}
        end
      end

      it "adds the group to the top groups" do
        group_finished

        expect(subject.groups).to match_array([
          {memory: 275},
          {memory: 250},
          {memory: 225},
          {memory: 200},
          {memory: 175}
        ])
      end
    end
  end
end

describe TestProf::MemoryProf::AllocTracker do
  subject { described_class.new(5) }

  if RUBY_ENGINE == "jruby"
    it "raises an error" do
      expect { subject }.to raise_error("Your Ruby Engine or OS is not supported")
    end
  else
    it_behaves_like "TestProf::MemoryProf::Tracker"

    describe "#track" do
      before do
        allow(GC).to receive(:stat).and_return({total_allocated_objects: 100})
      end

      it "returns the current number of allocations" do
        expect(subject.track).to eq(100)
      end
    end

    describe "#supported?" do
      it "returns true" do
        expect(subject.supported?).to be_truthy
      end
    end
  end
end

describe TestProf::MemoryProf::RssTracker do
  subject { described_class.new(5) }

  let(:tool) { instance_double(TestProf::MemoryProf::Tracker::RssTool::PS, track: 100) }

  before do
    allow(TestProf::MemoryProf::Tracker::RssTool).to receive(:tool).and_return(tool)
  end

  it_behaves_like "TestProf::MemoryProf::Tracker"

  describe "#track" do
    it "returns the current rss" do
      expect(subject.track).to eq(100)
    end
  end

  describe "#supported?" do
    context "when the host OS is supported" do
      it "returns true" do
        expect(subject.supported?).to be_truthy
      end
    end

    context "when the host OS is not supported" do
      let(:tool) { nil }

      it "raises an error" do
        expect { subject }.to raise_error("Your Ruby Engine or OS is not supported")
      end
    end
  end
end