-
Notifications
You must be signed in to change notification settings - Fork 0
/
wq_test.go
76 lines (63 loc) · 1.12 KB
/
wq_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
package wq
import (
"fmt"
"runtime"
"sync/atomic"
"testing"
"time"
)
func TestDrain(t *testing.T) {
workSize := 100
q := New(func(*int) { time.Sleep(time.Nanosecond) })
for i := 0; i < workSize; i++ {
i := i
q.EnQ(&i)
q.Drain()
}
q.Wait()
}
func BenchmarkQueue(b *testing.B) {
q := New(func(*int) { time.Sleep(time.Nanosecond) })
vals := make([]int, 0, b.N)
for i := 0; i < b.N; i++ {
vals = append(vals, i)
}
b.ResetTimer()
for i := range vals {
q.EnQ(&vals[i])
}
q.Wait()
}
func BenchmarkChans(b *testing.B) {
workerCount := runtime.NumCPU()
c := make(chan int, workerCount)
w := func() {
for _, ok := <-c; ok; _, ok = <-c {
time.Sleep(time.Nanosecond)
}
}
vals := make([]int, 0, b.N)
for i := 0; i < b.N; i++ {
vals = append(vals, i)
}
for i := 0; i < workerCount; i++ {
go w()
}
b.ResetTimer()
for i := range vals {
c <- vals[i]
}
}
func TestV(t *testing.T) {
const n = 150
c := uint32(0)
q := New(func(v *int) { atomic.AddUint32(&c, 1); fmt.Println(*v) })
for i := 0; i < n; i++ {
i := i
q.EnQ(&i)
}
q.Wait()
if c != n {
t.Errorf("expected %d, got %d", n, c)
}
}