aboutsummaryrefslogtreecommitdiff
path: root/pipeline/pipeline_bench_test.go
blob: f4f81a0f58ae03ef6baaf946ab59828a34a5f4c5 (plain)
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
package pipeline

import (
	"context"
	"testing"
)

func BenchmarkMap(b *testing.B) {
	b.ReportAllocs()

	input := make(chan int, 256)

	out := Run(b.Context(), input,
		Map(4, func(_ context.Context, v int) (int, bool) {
			return v * 2, true
		}),
	)

	go func() {
		for range out {
		}
	}()

	for b.Loop() {
		input <- 1
	}

	close(input)
}

func BenchmarkPipeline_Composition(b *testing.B) {
	b.ReportAllocs()

	input := make(chan int, 256)

	out := Run(b.Context(), input,
		Filter(func(v int) bool { return v > 0 }),
		Map(4, func(_ context.Context, v int) (int, bool) {
			return v * 2, true
		}),
		Buffer[int](64),
		Sink(2, func(_ context.Context, v int) {}),
	)

	go func() {
		for range out {
		}
	}()

	for b.Loop() {
		input <- 1
	}

	close(input)
}