-
Notifications
You must be signed in to change notification settings - Fork 0
/
subject_test.go
123 lines (109 loc) · 1.89 KB
/
subject_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
118
119
120
121
122
123
package observer
import (
"strconv"
"sync"
"testing"
)
func TestObserver(t *testing.T) {
var s Subject[int]
v := s.Set(1)
if v.Value() != 1 {
t.Fatal("required", 1)
}
v2, v2n := s.Set(2), v.Next()
if v2 != v2n {
t.Fatalf("%v != %v", v2, v2n)
}
var wg sync.WaitGroup
threes := make([]int, 8)
for i := range threes {
i := i
wg.Add(1)
go func() {
threes[i] = v2.Next().Value()
wg.Done()
}()
}
s.Set(3)
wg.Wait()
for i, three := range threes {
if three != 3 {
t.Fatalf("threes[%d] == %d, want 3", i, three)
}
}
for i := 4; i < 66; i++ {
s.Set(i)
}
v65 := s.View()
if v65.Value() != 65 {
t.Fatal("required", 65)
}
//t.Logf("%+v", v.frame)
//t.Logf("%+v", v65.frame)
// Check length matches.
l := v.Len()
if v65.Value() != l {
t.Fatalf("%v !=len(v) -> %v", v65, l)
}
for i := 0; i < 1000; i++ {
s.Set(66 + i)
l = v.Len()
if l != 66+i {
t.Fatalf("Got %v want %v for len", l, 66+i)
}
}
}
var cases = []int{1, 8, 32}
func BenchmarkObserver(b *testing.B) {
for _, i := range cases {
b.Run(strconv.Itoa(i), func(b *testing.B) {
s := &Subject[int]{}
s.Set(0)
var wg sync.WaitGroup
for w := 0; w < i; w++ {
wg.Add(1)
v := s.View()
go func() {
var sum int
for sum < b.N {
v = v.Next()
sum += v.Value()
}
wg.Done()
}()
}
b.ResetTimer()
for n := 0; n < b.N; n++ {
s.Set(1)
}
wg.Wait()
})
}
}
func BenchmarkChannel(b *testing.B) {
for _, i := range cases {
b.Run(strconv.Itoa(i), func(b *testing.B) {
var wg sync.WaitGroup
chs := make([]chan int, i)
for w := 0; w < i; w++ {
wg.Add(1)
ch := make(chan int, 8)
chs[w] = ch
go func() {
var sum int
for sum < b.N {
sum += <-ch
}
wg.Done()
}()
}
b.ResetTimer()
for n := 0; n < b.N; n++ {
for _, ch := range chs {
ch <- 1
}
}
wg.Wait()
})
}
}