-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweightrand_test.go
70 lines (59 loc) · 1.11 KB
/
weightrand_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
package rand
import (
"fmt"
"sync"
"testing"
)
func TestWeightRand(t *testing.T) {
r := NewWeightRand[int]()
r.AddWeight(14, 0)
l := 10
highWeightItem := 0
m := map[int]int{}
for i := 0; i < l; i++ {
if i == highWeightItem {
r.AddWeight(i, 2)
} else {
r.Add(i)
}
}
count := 1000000
for i := 0; i < count; i++ {
m[r.Get()]++
}
if _, ok := m[14]; ok || m[highWeightItem] <= count/l {
t.Errorf("test failed")
}
}
// go test -bench . -benchmem -run none -count 10 -memprofile mem.out
// go tool pprof -svg mem.out > mem.svg
func BenchmarkTestWeightGet(b *testing.B) {
r := NewWeightRand[string]()
wg := sync.WaitGroup{}
for i := 0; i < 80000; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
r.Add(fmt.Sprintf("rand-it-%d", i))
}(i)
}
for i := 0; i < 10000; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
r.AddWeight(fmt.Sprintf("rand-it-%d", i), 100)
}(i)
}
for i := 0; i < 10000; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
r.AddWeight(fmt.Sprintf("rand-it-%d", i), 30)
}(i)
}
wg.Wait()
b.ResetTimer()
for i := 0; i < b.N; i++ {
r.Get()
}
}