diff options
Diffstat (limited to 'fanout/fanout_bench_test.go')
| -rw-r--r-- | fanout/fanout_bench_test.go | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/fanout/fanout_bench_test.go b/fanout/fanout_bench_test.go new file mode 100644 index 0000000..0b88d87 --- /dev/null +++ b/fanout/fanout_bench_test.go | |||
| @@ -0,0 +1,65 @@ | |||
| 1 | package fanout | ||
| 2 | |||
| 3 | import ( | ||
| 4 | "context" | ||
| 5 | "testing" | ||
| 6 | ) | ||
| 7 | |||
| 8 | func BenchmarkFanOut_RoundRobin(b *testing.B) { | ||
| 9 | b.ReportAllocs() | ||
| 10 | |||
| 11 | fo, _ := NewFanOut[int]( | ||
| 12 | WithWorkerCount(4), | ||
| 13 | WithBufferSize(64), | ||
| 14 | WithStrategy(RoundRobin), | ||
| 15 | ) | ||
| 16 | |||
| 17 | ctx, cancel := context.WithCancel(context.Background()) | ||
| 18 | defer cancel() | ||
| 19 | |||
| 20 | input := make(chan int, 64) | ||
| 21 | fo.Run(ctx, input) | ||
| 22 | |||
| 23 | // Drain outputs | ||
| 24 | for _, ch := range fo.Outputs() { | ||
| 25 | go func() { | ||
| 26 | for range ch { | ||
| 27 | } | ||
| 28 | }() | ||
| 29 | } | ||
| 30 | |||
| 31 | for b.Loop() { | ||
| 32 | input <- 1 | ||
| 33 | } | ||
| 34 | |||
| 35 | close(input) | ||
| 36 | } | ||
| 37 | |||
| 38 | func BenchmarkFanOut_Broadcast(b *testing.B) { | ||
| 39 | b.ReportAllocs() | ||
| 40 | |||
| 41 | fo, _ := NewFanOut[int]( | ||
| 42 | WithWorkerCount(4), | ||
| 43 | WithBufferSize(64), | ||
| 44 | WithStrategy(Broadcast), | ||
| 45 | ) | ||
| 46 | |||
| 47 | ctx, cancel := context.WithCancel(context.Background()) | ||
| 48 | defer cancel() | ||
| 49 | |||
| 50 | input := make(chan int, 64) | ||
| 51 | fo.Run(ctx, input) | ||
| 52 | |||
| 53 | for _, ch := range fo.Outputs() { | ||
| 54 | go func() { | ||
| 55 | for range ch { | ||
| 56 | } | ||
| 57 | }() | ||
| 58 | } | ||
| 59 | |||
| 60 | for b.Loop() { | ||
| 61 | input <- 1 | ||
| 62 | } | ||
| 63 | |||
| 64 | close(input) | ||
| 65 | } | ||
