-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathblockingQueue_test.go
117 lines (100 loc) · 1.95 KB
/
blockingQueue_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
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
package blockingQueues
import (
. "gopkg.in/check.v1"
"sync"
"testing"
)
type SimpleQueue interface {
Put(item interface{}) (bool, error)
Get() (interface{}, error)
}
// Hook up gocheck into the "go test" runner.
func Test(t *testing.T) { TestingT(t) }
func benchmarkPut(c *C, writers int, readers int, q SimpleQueue) {
wg := sync.WaitGroup{}
for writer := 0; writer < writers; writer++ {
wg.Add(1)
go func(wg *sync.WaitGroup) {
for i := 0; i < c.N; i++ {
q.Put(i)
}
wg.Done()
}(&wg)
}
for reader := 0; reader < readers; reader++ {
wg.Add(1)
go func(wg *sync.WaitGroup) {
for i := 0; i < c.N; i++ {
q.Get()
}
wg.Done()
}(&wg)
}
wg.Wait()
}
func benchmarkPutMoreWriters(c *C, writers int, readers int, q SimpleQueue) {
wg := sync.WaitGroup{}
rest := writers - readers
for writer := 0; writer < writers; writer++ {
wg.Add(1)
go func(wg *sync.WaitGroup) {
for i := 0; i < c.N; i++ {
q.Put(i)
}
wg.Done()
}(&wg)
}
for reader := 0; reader < readers; reader++ {
wg.Add(1)
go func(wg *sync.WaitGroup) {
for i := 0; i < c.N; i++ {
q.Get()
}
wg.Done()
}(&wg)
}
c.ResetTimer()
for reader := 0; reader < rest; reader++ {
wg.Add(1)
go func(wg *sync.WaitGroup) {
for i := 0; i < c.N; i++ {
q.Get()
}
wg.Done()
}(&wg)
}
wg.Wait()
}
func benchmarkPutMoreReaders(c *C, writers int, readers int, q SimpleQueue) {
wg := sync.WaitGroup{}
rest := readers - writers
for writer := 0; writer < writers; writer++ {
wg.Add(1)
go func(wg *sync.WaitGroup) {
for i := 0; i < c.N; i++ {
q.Put(i)
}
wg.Done()
}(&wg)
}
for reader := 0; reader < readers; reader++ {
wg.Add(1)
go func(wg *sync.WaitGroup) {
for i := 0; i < c.N; i++ {
q.Get()
}
wg.Done()
}(&wg)
}
c.ResetTimer()
for writer := 0; writer < rest; writer++ {
wg.Add(1)
go func(wg *sync.WaitGroup) {
for i := 0; i < c.N; i++ {
q.Put(i)
}
wg.Done()
}(&wg)
}
wg.Wait()
}