-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue_test.go
More file actions
167 lines (144 loc) · 3.28 KB
/
Copy pathqueue_test.go
File metadata and controls
167 lines (144 loc) · 3.28 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package queue_test
import (
"fmt"
"github.com/fhltang/queue"
"sync"
"testing"
)
const Bound int = 10000
type TestCase struct {
Name string
New func () queue.Queue
}
var TestCases = []TestCase{
TestCase{"CondListQueue", func() queue.Queue { return queue.NewCondListQueue() }},
TestCase{"CondSliceQueue", func() queue.Queue { return queue.NewCondSliceQueue() }} ,
TestCase{"ChanSliceQueue", func() queue.Queue { return queue.NewChanSliceQueue() }},
TestCase{"BoundedChanQueue", func() queue.Queue { return queue.NewBoundedChanQueue(Bound) }},
}
func TestPushPop(t *testing.T) {
for _, tc := range TestCases {
t.Run(tc.Name, func(t *testing.T) {
q := tc.New()
q.PushBack(1)
if item := q.PopFront().(int); 1 != item {
t.Errorf("Got %d", item)
}
})
}
}
func TestPushPushPopPop(t *testing.T) {
for _, tc := range TestCases {
t.Run(tc.Name, func(t *testing.T) {
q := tc.New()
q.PushBack(1)
q.PushBack(2)
if item := q.PopFront().(int); 1 != item {
t.Errorf("Got %d", item)
}
if item := q.PopFront().(int); 2 != item {
t.Errorf("Got %d", item)
}
})
}
}
func TestPush3Pop2Push2Pop3(t *testing.T) {
for _, tc := range TestCases {
t.Run(tc.Name, func(t *testing.T) {
q := tc.New()
q.PushBack(1)
q.PushBack(2)
q.PushBack(3)
if item := q.PopFront().(int); 1 != item {
t.Errorf("Got %d", item)
}
if item := q.PopFront().(int); 2 != item {
t.Errorf("Got %d", item)
}
q.PushBack(4)
q.PushBack(5)
if item := q.PopFront().(int); 3 != item {
t.Errorf("Got %d", item)
}
if item := q.PopFront().(int); 4 != item {
t.Errorf("Got %d", item)
}
if item := q.PopFront().(int); 5 != item {
t.Errorf("Got %d", item)
}
})
}
}
// Benchmark: serially push then serially pop.
func SerialPushSerialPop(q queue.Queue) {
N := 10000
for j := 0; j < N; j++ {
q.PushBack(j)
}
for j := 0; j < N; j++ {
q.PopFront()
}
}
// Benchmark: concurrently push items and read items.
func ConcurrentSerialPushSerialPop(q queue.Queue) {
N := 10000
go func() {
for j := 0; j < N; j++ {
q.PushBack(j)
}
}()
for j := 0; j < N; j++ {
q.PopFront()
}
}
// Benchmark: push items serially and read them concurrently.
func SerialPushConcurrentPop(q queue.Queue) {
N := 10000
w := sync.WaitGroup{}
w.Add(N)
for j := 0; j < N; j++ {
go func() {
q.PopFront()
w.Done()
}()
}
for j := 0; j < N; j++ {
q.PushBack(j)
}
w.Wait()
}
// Benchmark: alternately push and pop items in batches of 100.
func AlternatePushPop(q queue.Queue) {
M := 100
N := 100
for i := 0; i < N; i ++{
for j := 0; j < M; j++ {
q.PushBack(i*M + j)
}
for j := 0; j < M; j++ {
q.PopFront()
}
}
}
type Benchmark struct {
Name string
Run func (queue.Queue)
}
var Benchmarks = []Benchmark{
Benchmark{"New", func(q queue.Queue) { return } },
Benchmark{"SerialPushSerialPop", SerialPushSerialPop},
Benchmark{"ConcurrentPushSerialPop", ConcurrentSerialPushSerialPop},
Benchmark{"SerialPushConcurrentPop", SerialPushConcurrentPop},
Benchmark{"AlternatePushPop", AlternatePushPop},
}
func BenchmarkAll(b *testing.B) {
for _, bm := range Benchmarks {
for _, tc := range TestCases {
b.Run(fmt.Sprintf("%s_%s", bm.Name, tc.Name), func(b *testing.B) {
for i := 0; i < b.N; i++ {
bm.Run(tc.New())
}
})
}
}
}