-
Notifications
You must be signed in to change notification settings - Fork 304
/
Copy pathchannel_test.go
60 lines (55 loc) · 937 Bytes
/
channel_test.go
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
package benchmark
import (
"context"
"sync"
"testing"
)
func BenchmarkWaitGroup(b *testing.B) {
for n := 0; n < b.N; n++ {
var wg sync.WaitGroup
wg.Add(1)
go func() {
wg.Done()
}()
wg.Wait()
}
}
func BenchmarkChannel(b *testing.B) {
for n := 0; n < b.N; n++ {
done := make(chan bool)
go func() {
done <- true
}()
<-done
}
}
func BenchmarkSelectChannel(b *testing.B) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
for n := 0; n < b.N; n++ {
done := make(chan bool)
go func() {
select { case <-ctx.Done():
case done <- true:
}
}()
<-done
}
}
func BenchmarkDoubleSelectChannel(b *testing.B) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
for n := 0; n < b.N; n++ {
done := make(chan bool)
go func() {
select {
case <-ctx.Done():
case done <- true:
}
}()
select {
case <-ctx.Done():
case <-done:
}
}
}