-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark_test.go
More file actions
306 lines (264 loc) · 7.52 KB
/
Copy pathbenchmark_test.go
File metadata and controls
306 lines (264 loc) · 7.52 KB
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
package streams
import (
"testing"
)
// sink consumes a stream without allocating result slice.
// This measures the algorithm itself without collection overhead.
func sink[T any](s Stream[T]) int {
count := 0
for range s.seq {
count++
}
return count
}
// BenchmarkTakeLast benchmarks the TakeLast operation with ring buffer.
func BenchmarkTakeLast(b *testing.B) {
input := Range(1, 10001).Collect() // 10000 elements
b.Run("TakeLast_10_Collect", func(b *testing.B) {
b.ReportAllocs()
for b.Loop() {
_ = FromSlice(input).TakeLast(10).Collect()
}
})
b.Run("TakeLast_10_Count", func(b *testing.B) {
b.ReportAllocs()
for b.Loop() {
_ = sink(FromSlice(input).TakeLast(10))
}
})
b.Run("TakeLast_1000_Collect", func(b *testing.B) {
b.ReportAllocs()
for b.Loop() {
_ = FromSlice(input).TakeLast(1000).Collect()
}
})
b.Run("TakeLast_1000_Count", func(b *testing.B) {
b.ReportAllocs()
for b.Loop() {
_ = sink(FromSlice(input).TakeLast(1000))
}
})
}
// BenchmarkDropLast benchmarks the DropLast operation with ring buffer.
func BenchmarkDropLast(b *testing.B) {
input := Range(1, 10001).Collect() // 10000 elements
b.Run("DropLast_10_Collect", func(b *testing.B) {
b.ReportAllocs()
for b.Loop() {
_ = FromSlice(input).DropLast(10).Collect()
}
})
b.Run("DropLast_10_Count", func(b *testing.B) {
b.ReportAllocs()
for b.Loop() {
_ = sink(FromSlice(input).DropLast(10))
}
})
b.Run("DropLast_1000_Collect", func(b *testing.B) {
b.ReportAllocs()
for b.Loop() {
_ = FromSlice(input).DropLast(1000).Collect()
}
})
b.Run("DropLast_1000_Count", func(b *testing.B) {
b.ReportAllocs()
for b.Loop() {
_ = sink(FromSlice(input).DropLast(1000))
}
})
}
// BenchmarkWindowWithStep benchmarks the WindowWithStep operation.
func BenchmarkWindowWithStep(b *testing.B) {
input := Range(1, 10001).Collect() // 10000 elements
b.Run("WindowWithStep_Size3_Step1_Collect", func(b *testing.B) {
b.ReportAllocs()
for b.Loop() {
_ = WindowWithStep(FromSlice(input), 3, 1, false).Collect()
}
})
b.Run("WindowWithStep_Size3_Step1_Count", func(b *testing.B) {
b.ReportAllocs()
for b.Loop() {
_ = sink(WindowWithStep(FromSlice(input), 3, 1, false))
}
})
b.Run("WindowWithStep_Size100_Step100_Collect", func(b *testing.B) {
b.ReportAllocs()
for b.Loop() {
_ = WindowWithStep(FromSlice(input), 100, 100, false).Collect()
}
})
b.Run("WindowWithStep_Size100_Step100_Count", func(b *testing.B) {
b.ReportAllocs()
for b.Loop() {
_ = sink(WindowWithStep(FromSlice(input), 100, 100, false))
}
})
}
// BenchmarkScan benchmarks the Scan operation.
func BenchmarkScan(b *testing.B) {
input := Range(1, 10001).Collect() // 10000 elements
b.Run("Scan_Collect", func(b *testing.B) {
b.ReportAllocs()
for b.Loop() {
_ = Scan(FromSlice(input), 0, func(acc, v int) int { return acc + v }).Collect()
}
})
b.Run("Scan_Count", func(b *testing.B) {
b.ReportAllocs()
for b.Loop() {
_ = sink(Scan(FromSlice(input), 0, func(acc, v int) int { return acc + v }))
}
})
}
// BenchmarkDistinctUntilChanged benchmarks the DistinctUntilChanged operation.
func BenchmarkDistinctUntilChanged(b *testing.B) {
// Create input with many consecutive duplicates
input := make([]int, 10000)
for i := range input {
input[i] = i / 10 // Groups of 10 identical values
}
b.Run("DistinctUntilChanged_Collect", func(b *testing.B) {
b.ReportAllocs()
for b.Loop() {
_ = DistinctUntilChanged(FromSlice(input)).Collect()
}
})
b.Run("DistinctUntilChanged_Count", func(b *testing.B) {
b.ReportAllocs()
for b.Loop() {
_ = sink(DistinctUntilChanged(FromSlice(input)))
}
})
}
// BenchmarkStep benchmarks the Step operation.
func BenchmarkStep(b *testing.B) {
input := Range(1, 10001).Collect() // 10000 elements
b.Run("Step_10_Collect", func(b *testing.B) {
b.ReportAllocs()
for b.Loop() {
_ = FromSlice(input).Step(10).Collect()
}
})
b.Run("Step_10_Count", func(b *testing.B) {
b.ReportAllocs()
for b.Loop() {
_ = sink(FromSlice(input).Step(10))
}
})
}
// BenchmarkFlatten benchmarks the Flatten operation.
func BenchmarkFlatten(b *testing.B) {
// Create 100 slices of 100 elements each
input := make([][]int, 100)
for i := range input {
input[i] = make([]int, 100)
for j := range input[i] {
input[i][j] = i*100 + j
}
}
b.Run("Flatten_Collect", func(b *testing.B) {
b.ReportAllocs()
for b.Loop() {
_ = Flatten(FromSlice(input)).Collect()
}
})
b.Run("Flatten_Count", func(b *testing.B) {
b.ReportAllocs()
for b.Loop() {
_ = sink(Flatten(FromSlice(input)))
}
})
}
// BenchmarkZip3 benchmarks the Zip3 operation.
func BenchmarkZip3(b *testing.B) {
input1 := Range(1, 1001).Collect()
input2 := Range(1001, 2001).Collect()
input3 := Range(2001, 3001).Collect()
b.Run("Zip3_Collect", func(b *testing.B) {
b.ReportAllocs()
for b.Loop() {
_ = Zip3(FromSlice(input1), FromSlice(input2), FromSlice(input3)).Collect()
}
})
b.Run("Zip3_Count", func(b *testing.B) {
b.ReportAllocs()
for b.Loop() {
_ = sink(Zip3(FromSlice(input1), FromSlice(input2), FromSlice(input3)))
}
})
}
// BenchmarkReduceByKey benchmarks the ReduceByKey operation.
func BenchmarkReduceByKey(b *testing.B) {
// Create 10000 key-value pairs with 100 unique keys
pairs := make([]Pair[int, int], 10000)
for i := range pairs {
pairs[i] = NewPair(i%100, i)
}
b.Run("ReduceByKey_10000_100keys", func(b *testing.B) {
b.ReportAllocs()
for b.Loop() {
s := PairsOf(pairs...)
_ = ReduceByKey(s, func(a, b int) int { return a + b })
}
})
}
// BenchmarkParallelFlatMap compares streaming vs chunked modes.
// Use these benchmarks to tune ChunkSize based on your workload.
func BenchmarkParallelFlatMap(b *testing.B) {
// Simulate workload: 100 inputs, each producing sub-stream of 100 elements
input := Range(0, 100).Collect()
subStreamSize := 100
b.Run("Streaming_100x100", func(b *testing.B) {
b.ReportAllocs()
for b.Loop() {
_ = ParallelFlatMap(FromSlice(input), func(n int) Stream[int] {
return Range(0, subStreamSize)
}, WithConcurrency(4), WithOrdered(true)).Collect()
}
})
b.Run("Chunked_100x100_Chunk10", func(b *testing.B) {
b.ReportAllocs()
for b.Loop() {
_ = ParallelFlatMap(FromSlice(input), func(n int) Stream[int] {
return Range(0, subStreamSize)
}, WithConcurrency(4), WithOrdered(true), WithChunkSize(10)).Collect()
}
})
b.Run("Chunked_100x100_Chunk25", func(b *testing.B) {
b.ReportAllocs()
for b.Loop() {
_ = ParallelFlatMap(FromSlice(input), func(n int) Stream[int] {
return Range(0, subStreamSize)
}, WithConcurrency(4), WithOrdered(true), WithChunkSize(25)).Collect()
}
})
b.Run("Chunked_100x100_Chunk50", func(b *testing.B) {
b.ReportAllocs()
for b.Loop() {
_ = ParallelFlatMap(FromSlice(input), func(n int) Stream[int] {
return Range(0, subStreamSize)
}, WithConcurrency(4), WithOrdered(true), WithChunkSize(50)).Collect()
}
})
// Large sub-streams scenario
largeSubStreamSize := 1000
b.Run("Streaming_50x1000", func(b *testing.B) {
b.ReportAllocs()
smallInput := Range(0, 50).Collect()
for b.Loop() {
_ = ParallelFlatMap(FromSlice(smallInput), func(n int) Stream[int] {
return Range(0, largeSubStreamSize)
}, WithConcurrency(4), WithOrdered(true)).Collect()
}
})
b.Run("Chunked_50x1000_Chunk10", func(b *testing.B) {
b.ReportAllocs()
smallInput := Range(0, 50).Collect()
for b.Loop() {
_ = ParallelFlatMap(FromSlice(smallInput), func(n int) Stream[int] {
return Range(0, largeSubStreamSize)
}, WithConcurrency(4), WithOrdered(true), WithChunkSize(10)).Collect()
}
})
}