-
Notifications
You must be signed in to change notification settings - Fork 0
/
strmap_test.go
137 lines (120 loc) · 2.8 KB
/
strmap_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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package imstrmap
import (
"fmt"
"github.com/onsi/gomega"
"runtime"
"runtime/debug"
"testing"
)
func newSrcMap() map[string]string {
m := map[string]string{
"locality": "vlocality",
}
for _, k := range []string{"a", "b", "c", "das", "huhqw", "uyoqw", "y9qw", "juioq", "qqeq", "vqrqasas", "hqw", "asdqw", "asqwqwe"} {
m[k] = k
}
return m
}
var (
indexerFactory = NewIndexerFactory([]string{"locality"})
)
func TestStrMap(t *testing.T) {
g := gomega.NewGomegaWithT(t)
im := FromMap(newSrcMap(), indexerFactory)
v, ok := im.Get("locality")
g.Expect(v).To(gomega.Equal("vlocality"))
g.Expect(ok).To(gomega.Equal(true))
v, ok = im.Get("b")
g.Expect(v).To(gomega.Equal("b"))
g.Expect(ok).To(gomega.Equal(true))
v, ok = im.Get("z")
g.Expect(ok).To(gomega.Equal(false))
}
func BenchmarkNewImmutableMap(b *testing.B) {
srcMap := newSrcMap()
for i := 0; i < b.N; i++ {
FromMap(srcMap, indexerFactory)
}
}
func BenchmarkImmutabeStringMapIndexGet(b *testing.B) {
srcMap := newSrcMap()
m := FromMap(srcMap, indexerFactory)
for i := 0; i < b.N; i++ {
m.Get("locality")
m.Get("locality")
m.Get("locality")
}
}
func BenchmarkImmutabeStringMapNoIndexGet(b *testing.B) {
srcMap := newSrcMap()
m := FromMap(srcMap, indexerFactory)
for i := 0; i < b.N; i++ {
m.Get("a")
m.Get("b")
m.Get("d")
}
}
func BenchmarkStringMapGet(b *testing.B) {
srcMap := newSrcMap()
for i := 0; i < b.N; i++ {
_ = srcMap["a"]
_ = srcMap["b"]
_ = srcMap["d"]
}
}
func BenchmarkImmutabeStringMap_Range(b *testing.B) {
srcMap := newSrcMap()
m := FromMap(srcMap, indexerFactory)
for i := 0; i < b.N; i++ {
m.Range(func(s string, s2 string) bool {
return false
})
}
}
func BenchmarkStringMap_Range(b *testing.B) {
srcMap := newSrcMap()
for i := 0; i < b.N; i++ {
for k, v := range srcMap {
_, _ = k, v
}
}
}
func BenchmarkImmutabeStringMap_Map(b *testing.B) {
srcMap := newSrcMap()
m := FromMap(srcMap, indexerFactory)
for i := 0; i < b.N; i++ {
m.Map()
}
}
func TestImmutabeStringMap_Memory(t *testing.T) {
runtime.GC()
debug.FreeOSMemory()
before := &runtime.MemStats{}
runtime.ReadMemStats(before)
a := make([]*ImmutabeStringMap, 0)
for i := 0; i < 10000; i++ {
a = append(a, FromMap(newSrcMap(), indexerFactory))
}
runtime.GC()
debug.FreeOSMemory()
after := &runtime.MemStats{}
runtime.ReadMemStats(after)
fmt.Println("heap", after.HeapInuse-before.HeapInuse)
fmt.Printf("%p\n", a)
}
func TestStringMap_Memory(t *testing.T) {
runtime.GC()
debug.FreeOSMemory()
before := &runtime.MemStats{}
runtime.ReadMemStats(before)
a := make([]map[string]string, 0)
for i := 0; i < 10000; i++ {
a = append(a, newSrcMap())
}
runtime.GC()
debug.FreeOSMemory()
after := &runtime.MemStats{}
runtime.ReadMemStats(after)
fmt.Println("heap", after.HeapInuse-before.HeapInuse)
fmt.Printf("%p\n", a)
}