-
Notifications
You must be signed in to change notification settings - Fork 369
/
buffer_spec.rb
280 lines (221 loc) · 7.72 KB
/
buffer_spec.rb
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
require 'spec_helper'
require 'ddtrace'
require 'ddtrace/buffer'
require 'concurrent'
RSpec.describe Datadog::TraceBuffer do
subject(:buffer_class) { described_class }
context 'with CRuby' do
before { skip unless PlatformHelpers.mri? }
it { is_expected.to be <= Datadog::CRubyTraceBuffer }
end
context 'with JRuby' do
before { skip unless PlatformHelpers.jruby? }
it { is_expected.to be <= Datadog::ThreadSafeBuffer }
end
end
RSpec.shared_examples 'trace buffer' do
include_context 'health metrics'
subject(:buffer) { described_class.new(max_size) }
let(:max_size) { 0 }
let(:max_size_leniency) { defined?(super) ? super() : 1 } # Multiplier to allowed max_size
def measure_traces_size(traces)
traces.inject(Datadog::Runtime::ObjectSpace.estimate_bytesize(traces)) do |sum, trace|
sum + measure_trace_size(trace)
end
end
def measure_trace_size(trace)
trace.inject(Datadog::Runtime::ObjectSpace.estimate_bytesize(trace)) do |sum, span|
sum + Datadog::Runtime::ObjectSpace.estimate_bytesize(span)
end
end
describe '#initialize' do
it { is_expected.to be_a_kind_of(described_class) }
end
describe '#push' do
let(:output) { buffer.pop }
context 'given no limit' do
let(:traces) { get_test_traces(4) }
let(:max_size) { 0 }
it 'retains all items' do
traces.each { |t| buffer.push(t) }
expect(output.length).to eq(4)
end
end
context 'given a max size' do
let(:traces) { get_test_traces(max_size + 1) }
let(:max_size) { 3 }
it 'does not exceed it' do
traces.each { |t| buffer.push(t) }
expect(output.length).to eq(max_size)
expect(output).to include(traces.last)
# A trace will be dropped at random, except the trace
# that triggered the overflow.
dropped_traces = traces.reject { |t| output.include?(t) }
expected_traces = traces - dropped_traces
expected_spans = expected_traces.inject(0) { |sum, t| sum + t.length }
# Calling #pop produces metrics:
# Accept events for every #push, and one drop event for overflow
expect(health_metrics).to have_received(:queue_accepted)
.with(traces.length)
expect(health_metrics).to have_received(:queue_accepted_lengths)
.with(expected_spans)
expect(health_metrics).to have_received(:queue_dropped)
.with(dropped_traces.length)
# Metrics for queue gauges.
expect(health_metrics).to have_received(:queue_max_length)
.with(max_size)
expect(health_metrics).to have_received(:queue_spans)
.with(expected_spans)
expect(health_metrics).to have_received(:queue_length)
.with(max_size)
end
end
context 'when closed' do
let(:max_size) { 0 }
let(:traces) { get_test_traces(6) }
let(:output) { buffer.pop }
it 'retains items up to close' do
traces.first(4).each { |t| buffer.push(t) }
buffer.close
traces.last(2).each { |t| buffer.push(t) }
expect(output.length).to eq(4)
expect(output).to_not include(*traces.last(2))
# Last 2 traces will be dropped, without triggering stats.
dropped_traces = traces.reject { |t| output.include?(t) }
expected_traces = traces - dropped_traces
expected_spans = expected_traces.inject(0) { |sum, t| sum + t.length }
# Calling #pop produces metrics:
# Metrics for accept events and no drop events
# When the buffer is closed, drops don't count. (Should they?)
expect(health_metrics).to have_received(:queue_accepted)
.with(4)
expect(health_metrics).to have_received(:queue_accepted_lengths)
.with(expected_spans)
expect(health_metrics).to have_received(:queue_dropped)
.with(0)
# Metrics for queue gauges.
expect(health_metrics).to have_received(:queue_max_length)
.with(max_size)
expect(health_metrics).to have_received(:queue_spans)
.with(expected_spans)
expect(health_metrics).to have_received(:queue_length)
.with(expected_traces.length)
end
end
context 'thread safety' do
subject(:push) { threads.each(&:join) }
let(:max_size) { 500 }
let(:thread_count) { 100 }
let(:threads) do
buffer
Array.new(thread_count) do |i|
Thread.new do
sleep(rand / 1000)
buffer.push([i])
end
end
end
let(:output) { buffer.pop }
it 'does not have collisions' do
push
expect(output).to_not be nil
expect(output.sort).to eq((0..thread_count - 1).map { |i| [i] })
end
context 'with items exceeding maximum size' do
let(:max_size) { 100 }
let(:thread_count) { 1000 }
let(:barrier) { Concurrent::CyclicBarrier.new(thread_count) }
let(:threads) do
buffer
barrier
Array.new(thread_count) do |i|
Thread.new do
barrier.wait
1000.times { buffer.push([i]) }
end
end
end
it 'does not exceed expected maximum size' do
push
expect(output).to have_at_most(max_size * max_size_leniency).items
end
context 'with #pop operations' do
let(:barrier) { Concurrent::CyclicBarrier.new(thread_count + 1) }
before do
allow(Datadog).to receive(:logger).and_return(double)
end
it 'executes without error' do
threads
barrier.wait
1000.times do
buffer.pop
# Yield control to threads to increase contention.
# Otherwise we might run #pop a few times in succession,
# which doesn't help us stress test this case.
sleep 0
end
threads.each(&:kill)
push
end
end
end
end
end
describe '#length' do
subject(:length) { buffer.length }
context 'given no traces' do
it { is_expected.to eq(0) }
end
context 'given a trace' do
before { buffer.push([1]) }
it { is_expected.to eq(1) }
end
end
describe '#empty?' do
subject(:empty?) { buffer.empty? }
context 'given no traces' do
it { is_expected.to be true }
end
context 'given a trace' do
before { buffer.push([1]) }
it { is_expected.to be false }
end
end
describe '#pop' do
subject(:pop) { buffer.pop }
let(:traces) { get_test_traces(2) }
before do
traces.each { |t| buffer.push(t) }
end
it do
expect(pop.length).to eq(traces.length)
expect(pop).to include(*traces)
expect(buffer.empty?).to be true
expected_spans = traces.inject(0) { |sum, t| sum + t.length }
# Calling #pop produces metrics:
# Metrics for accept events and one drop event
expect(health_metrics).to have_received(:queue_accepted)
.with(traces.length)
expect(health_metrics).to have_received(:queue_accepted_lengths)
.with(expected_spans)
expect(health_metrics).to have_received(:queue_dropped)
.with(0)
# Metrics for queue gauges.
expect(health_metrics).to have_received(:queue_max_length)
.with(max_size)
expect(health_metrics).to have_received(:queue_spans)
.with(expected_spans)
expect(health_metrics).to have_received(:queue_length)
.with(traces.length)
end
end
end
RSpec.describe Datadog::ThreadSafeBuffer do
it_behaves_like 'trace buffer'
end
RSpec.describe Datadog::CRubyTraceBuffer do
before { skip unless PlatformHelpers.mri? }
it_behaves_like 'trace buffer' do
let(:max_size_leniency) { 1.04 } # 4%
end
end