@@ -8,7 +8,6 @@ package internal
8
8
import (
9
9
"context"
10
10
"errors"
11
- "fmt"
12
11
"strconv"
13
12
"sync"
14
13
"testing"
@@ -23,7 +22,7 @@ import (
23
22
// We want to test the overflow behavior, so we block the consumer
24
23
// by holding a startLock before submitting items to the queue.
25
24
func TestBoundedQueue (t * testing.T ) {
26
- q := NewBoundedMemoryQueue [string ](1 )
25
+ q := NewBoundedMemoryQueue [string ](& RequestSizer [ string ]{}, 1 )
27
26
28
27
assert .NoError (t , q .Offer (context .Background (), "a" ))
29
28
@@ -73,7 +72,7 @@ func TestBoundedQueue(t *testing.T) {
73
72
// only after Stop will mean the consumers are still locked while
74
73
// trying to perform the final consumptions.
75
74
func TestShutdownWhileNotEmpty (t * testing.T ) {
76
- q := NewBoundedMemoryQueue [string ](1000 )
75
+ q := NewBoundedMemoryQueue [string ](& RequestSizer [ string ]{}, 1000 )
77
76
78
77
assert .NoError (t , q .Start (context .Background (), componenttest .NewNopHost ()))
79
78
for i := 0 ; i < 10 ; i ++ {
@@ -98,75 +97,70 @@ func TestShutdownWhileNotEmpty(t *testing.T) {
98
97
}))
99
98
}
100
99
101
- func Benchmark_QueueUsage_10000_1_50000 (b * testing.B ) {
102
- benchmarkQueueUsage (b , 10000 , 1 , 50000 )
100
+ func Benchmark_QueueUsage_10000_requests_1_50000 (b * testing.B ) {
101
+ benchmarkQueueUsage (b , & RequestSizer [ fakeReq ]{}, 10000 , 1 , 50000 )
103
102
}
104
103
105
- func Benchmark_QueueUsage_10000_2_50000 (b * testing.B ) {
106
- benchmarkQueueUsage (b , 10000 , 2 , 50000 )
107
- }
108
- func Benchmark_QueueUsage_10000_5_50000 (b * testing.B ) {
109
- benchmarkQueueUsage (b , 10000 , 5 , 50000 )
110
- }
111
- func Benchmark_QueueUsage_10000_10_50000 (b * testing.B ) {
112
- benchmarkQueueUsage (b , 10000 , 10 , 50000 )
104
+ func Benchmark_QueueUsage_10000_requests_10_50000 (b * testing.B ) {
105
+ benchmarkQueueUsage (b , & RequestSizer [fakeReq ]{}, 10000 , 10 , 50000 )
113
106
}
114
107
115
- func Benchmark_QueueUsage_50000_1_50000 (b * testing.B ) {
116
- benchmarkQueueUsage (b , 50000 , 1 , 50000 )
108
+ func Benchmark_QueueUsage_50000_requests_1_50000 (b * testing.B ) {
109
+ benchmarkQueueUsage (b , & RequestSizer [ fakeReq ]{}, 50000 , 1 , 50000 )
117
110
}
118
111
119
- func Benchmark_QueueUsage_50000_2_50000 (b * testing.B ) {
120
- benchmarkQueueUsage (b , 50000 , 2 , 50000 )
121
- }
122
- func Benchmark_QueueUsage_50000_5_50000 (b * testing.B ) {
123
- benchmarkQueueUsage (b , 50000 , 5 , 50000 )
112
+ func Benchmark_QueueUsage_50000_requests_10_50000 (b * testing.B ) {
113
+ benchmarkQueueUsage (b , & RequestSizer [fakeReq ]{}, 50000 , 10 , 50000 )
124
114
}
125
- func Benchmark_QueueUsage_50000_10_50000 (b * testing.B ) {
126
- benchmarkQueueUsage (b , 50000 , 10 , 50000 )
115
+
116
+ func Benchmark_QueueUsage_10000_requests_1_250000 (b * testing.B ) {
117
+ benchmarkQueueUsage (b , & RequestSizer [fakeReq ]{}, 10000 , 1 , 250000 )
127
118
}
128
119
129
- func Benchmark_QueueUsage_10000_1_250000 (b * testing.B ) {
130
- benchmarkQueueUsage (b , 10000 , 1 , 250000 )
120
+ func Benchmark_QueueUsage_10000_requests_10_250000 (b * testing.B ) {
121
+ benchmarkQueueUsage (b , & RequestSizer [ fakeReq ]{}, 10000 , 10 , 250000 )
131
122
}
132
123
133
- func Benchmark_QueueUsage_10000_2_250000 (b * testing.B ) {
134
- benchmarkQueueUsage (b , 10000 , 2 , 250000 )
124
+ func Benchmark_QueueUsage_1M_items_10_250k (b * testing.B ) {
125
+ benchmarkQueueUsage (b , & ItemsSizer [ fakeReq ]{}, 1000000 , 10 , 250000 )
135
126
}
136
- func Benchmark_QueueUsage_10000_5_250000 (b * testing.B ) {
137
- benchmarkQueueUsage (b , 10000 , 5 , 250000 )
127
+
128
+ func Benchmark_QueueUsage_1M_items_10_1M (b * testing.B ) {
129
+ benchmarkQueueUsage (b , & ItemsSizer [fakeReq ]{}, 1000000 , 10 , 1000000 )
138
130
}
139
- func Benchmark_QueueUsage_10000_10_250000 (b * testing.B ) {
140
- benchmarkQueueUsage (b , 10000 , 10 , 250000 )
131
+
132
+ func Benchmark_QueueUsage_100M_items_10_10M (b * testing.B ) {
133
+ benchmarkQueueUsage (b , & ItemsSizer [fakeReq ]{}, 100000000 , 10 , 10000000 )
141
134
}
142
135
143
136
func TestQueueUsage (t * testing.T ) {
144
137
t .Run ("with enough workers" , func (t * testing.T ) {
145
- queueUsage (t , 10000 , 5 , 1000 )
138
+ queueUsage (t , & RequestSizer [ fakeReq ]{}, 10000 , 5 , 1000 )
146
139
})
147
140
t .Run ("past capacity" , func (t * testing.T ) {
148
- queueUsage (t , 10000 , 2 , 50000 )
141
+ queueUsage (t , & RequestSizer [ fakeReq ]{}, 10000 , 2 , 50000 )
149
142
})
150
143
}
151
144
152
- func benchmarkQueueUsage (b * testing.B , capacity int , numConsumers int , numberOfItems int ) {
145
+ func benchmarkQueueUsage (b * testing.B , sizer Sizer [fakeReq ], capacity int , numConsumers int ,
146
+ numberOfItems int ) {
153
147
b .ReportAllocs ()
154
148
for i := 0 ; i < b .N ; i ++ {
155
- queueUsage (b , capacity , numConsumers , numberOfItems )
149
+ queueUsage (b , sizer , capacity , numConsumers , numberOfItems )
156
150
}
157
151
}
158
152
159
- func queueUsage (tb testing.TB , capacity int , numConsumers int , numberOfItems int ) {
153
+ func queueUsage (tb testing.TB , sizer Sizer [ fakeReq ], capacity int , numConsumers int , numberOfItems int ) {
160
154
var wg sync.WaitGroup
161
155
wg .Add (numberOfItems )
162
- q := NewBoundedMemoryQueue [string ]( capacity )
163
- consumers := NewQueueConsumers (q , numConsumers , func (context.Context , string ) error {
156
+ q := NewBoundedMemoryQueue [fakeReq ]( sizer , capacity )
157
+ consumers := NewQueueConsumers (q , numConsumers , func (context.Context , fakeReq ) error {
164
158
wg .Done ()
165
159
return nil
166
160
})
167
161
require .NoError (tb , consumers .Start (context .Background (), componenttest .NewNopHost ()))
168
162
for j := 0 ; j < numberOfItems ; j ++ {
169
- if err := q .Offer (context .Background (), fmt . Sprintf ( "%d" , j ) ); errors .Is (err , ErrQueueIsFull ) {
163
+ if err := q .Offer (context .Background (), fakeReq { 10 } ); errors .Is (err , ErrQueueIsFull ) {
170
164
wg .Done ()
171
165
}
172
166
}
@@ -176,7 +170,7 @@ func queueUsage(tb testing.TB, capacity int, numConsumers int, numberOfItems int
176
170
}
177
171
178
172
func TestZeroSizeNoConsumers (t * testing.T ) {
179
- q := NewBoundedMemoryQueue [string ](0 )
173
+ q := NewBoundedMemoryQueue [string ](& RequestSizer [ string ]{}, 0 )
180
174
181
175
err := q .Start (context .Background (), componenttest .NewNopHost ())
182
176
assert .NoError (t , err )
0 commit comments