-
Notifications
You must be signed in to change notification settings - Fork 8
/
utils_test.go
103 lines (93 loc) · 2.26 KB
/
utils_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
package matching
import (
"math/rand"
"strconv"
"sync"
"testing"
"github.com/stretchr/testify/assert"
)
func assertEqual(assert *assert.Assertions, expected, actual []Subscriber) {
assert.Len(actual, len(expected))
for _, sub := range expected {
assert.Contains(actual, sub)
}
}
func populateMatcher(m Matcher, num, topicSize int) {
for i := 0; i < num; i++ {
prefix := ""
topic := ""
for j := 0; j < topicSize; j++ {
topic += prefix + strconv.Itoa(rand.Int())
prefix = "."
}
m.Subscribe(topic, Subscriber(topic))
}
}
func benchmark5050(b *testing.B, numItems, numThreads int, factory func([][]string) Matcher) {
itemsToInsert := make([][]string, 0, numThreads)
for i := 0; i < numThreads; i++ {
items := make([]string, 0, numItems)
for j := 0; j < numItems; j++ {
topic := strconv.Itoa(j%10) + "." + strconv.Itoa(j%50) + "." + strconv.Itoa(j)
items = append(items, topic)
}
itemsToInsert = append(itemsToInsert, items)
}
var wg sync.WaitGroup
sub := Subscriber("abc")
m := factory(itemsToInsert)
populateMatcher(m, 1000, 5)
b.ResetTimer()
for i := 0; i < b.N; i++ {
wg.Add(numThreads)
for j := 0; j < numThreads; j++ {
go func(j int) {
if j%2 != 0 {
for _, key := range itemsToInsert[j] {
m.Subscribe(key, sub)
}
} else {
for _, key := range itemsToInsert[j] {
m.Lookup(key)
}
}
wg.Done()
}(j)
}
wg.Wait()
}
}
func benchmark9010(b *testing.B, numItems, numThreads int, factory func([][]string) Matcher) {
itemsToInsert := make([][]string, 0, numThreads)
for i := 0; i < numThreads; i++ {
items := make([]string, 0, numItems)
for j := 0; j < numItems; j++ {
topic := strconv.Itoa(j%10) + "." + strconv.Itoa(j%50) + "." + strconv.Itoa(j)
items = append(items, topic)
}
itemsToInsert = append(itemsToInsert, items)
}
var wg sync.WaitGroup
sub := Subscriber("abc")
m := factory(itemsToInsert)
populateMatcher(m, 1000, 5)
b.ResetTimer()
for i := 0; i < b.N; i++ {
wg.Add(numThreads)
for j := 0; j < numThreads; j++ {
go func(j int) {
if j%10 == 0 {
for _, key := range itemsToInsert[j] {
m.Subscribe(key, sub)
}
} else {
for _, key := range itemsToInsert[j] {
m.Lookup(key)
}
}
wg.Done()
}(j)
}
wg.Wait()
}
}