forked from mna/gocostmodel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
goro_test.go
62 lines (53 loc) · 1010 Bytes
/
goro_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
61
62
package gocostmodel
import (
"math"
"sync"
"testing"
)
// this one doesn't work as intended, it presumably overwhelms the scheduler
// with tons of goros to spawn, so it ends up looking much more costly than
// it actually is.
func xBenchmarkGoroFireForget(b *testing.B) {
for i := 0; i < b.N; i++ {
go func() {
// avoid inlining
math.Pow(1, 1)
}()
}
}
func BenchmarkGoroWait(b *testing.B) {
for i := 0; i < b.N; i++ {
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
wg.Done()
}()
wg.Wait()
}
}
func BenchmarkGoroSend(b *testing.B) {
benchmarkGoroSend(b, 0)
}
func BenchmarkGoroSendBuf1(b *testing.B) {
benchmarkGoroSend(b, 1)
}
func BenchmarkGoroSendBuf100(b *testing.B) {
benchmarkGoroSend(b, 100)
}
func benchmarkGoroSend(b *testing.B, bufSize int) {
ch := make(chan int, bufSize)
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
for _ = range ch {
}
wg.Done()
}()
b.ResetTimer()
for i := 0; i < b.N; i++ {
ch <- i
}
b.StopTimer()
close(ch)
wg.Wait()
}