-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathany_test.go
185 lines (179 loc) · 5.06 KB
/
any_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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// Copyright 2020 rateLimit Author(https://github.com/yudeguang/noGCMap). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/yudeguang/noGCMap.
package noGCMap
import (
"log"
"runtime"
"runtime/debug"
"strconv"
"sync"
"testing"
"time"
)
func TestAny(t *testing.T) {
log.SetFlags(log.Lshortfile | log.Ltime)
//构建值
var str string
var bigStr string
for i := 0; i <= 10; i++ {
str = str + "x"
}
str = ":" + str + ":"
for i := 0; i <= 100000; i++ {
bigStr = bigStr + "A"
}
//定义并发数及测试量
chanNum := 10
var max = 1000000
var maxDeleted = 500000
var mapOffical = make(map[string]string)
var mapAny = New()
log.Println("开始测试并发写")
//set
var wgSet sync.WaitGroup
wgSet.Add(chanNum)
mapAny.SetString("", "empty")
mapAny.SetString("empty", "")
mapAny.SetString("bigStr", bigStr)
//offical
for i := 0; i < max; i++ {
mapOffical[strconv.Itoa(i)] = "@" + strconv.Itoa(i) + str
}
//mapAny
for i := 0; i < chanNum; i++ {
go func(i int, wg *sync.WaitGroup) {
for i := 0; i < max; i++ {
mapAny.SetString(strconv.Itoa(i), "@"+strconv.Itoa(i)+str)
}
wgSet.Done()
}(i, &wgSet)
}
wgSet.Wait()
log.Println("开始测试并发读取")
//get
var wgGet sync.WaitGroup
wgGet.Add(chanNum)
for i := 0; i < chanNum; i++ {
go func(i int, wg *sync.WaitGroup) {
for i := 0; i < max+100; i++ {
valOffical, existOffical := mapOffical[strconv.Itoa(i)]
valAny, existAny := mapAny.GetString(strconv.Itoa(i))
if existOffical != existAny {
t.Fatalf("unexpected value obtained; got %v want %v", existAny, existOffical)
}
if valOffical != valAny {
t.Fatalf("unexpected value obtained; got %q want %q", valAny, valOffical)
}
}
wgGet.Done()
}(i, &wgGet)
}
wgGet.Wait()
//get value empty
valEmpty, exist := mapAny.GetString("empty")
if !exist {
t.Fatalf("unexpected value obtained; got %v want %v", exist, true)
}
if valEmpty != "" {
t.Fatalf("unexpected value obtained; got %q want %q", valEmpty, "")
}
//get key empty
valOFKeyEmpty, exist := mapAny.GetString("")
if !exist {
t.Fatalf("unexpected value obtained; got %v want %v", exist, true)
}
if valOFKeyEmpty != "empty" {
t.Fatalf("unexpected value obtained; got %q want %q", valOFKeyEmpty, "empty")
}
//算一下key个数
var keyNum, deleteItemNum int
for i := range mapAny.buckets {
keyNum = keyNum + len(mapAny.buckets[i].index)
deleteItemNum = deleteItemNum + mapAny.buckets[i].deleteItemNum
}
keyNum = keyNum + len(mapAny.mapForHashCollisionAndLongKVPair)
if keyNum != max+3 {
t.Fatalf("unexpected value obtained; got %v want %v", keyNum, max+3)
}
log.Println("开始测试并发删除")
//delete
var wgDelete sync.WaitGroup
wgDelete.Add(chanNum)
for i := 0; i < chanNum; i++ {
go func(i int, wg *sync.WaitGroup) {
for i := -100; i < maxDeleted; i++ {
mapAny.DeleteString(strconv.Itoa(i))
_, exist := mapAny.GetString(strconv.Itoa(i))
if exist {
t.Fatalf("unexpected value obtained; got %v want %v", exist, false)
}
}
wgDelete.Done()
}(i, &wgDelete)
}
wgDelete.Wait()
for i := -100; i < maxDeleted; i++ {
delete(mapOffical, strconv.Itoa(i))
}
log.Println("开始测试并发删除之后与官方结果对比")
//get 删除之后再用get测试
var wgGetAfterDeleted sync.WaitGroup
wgGetAfterDeleted.Add(chanNum)
for i := 0; i < chanNum; i++ {
go func(i int, wg *sync.WaitGroup) {
for i := 0; i < max+100; i++ {
valOffical, existOffical := mapOffical[strconv.Itoa(i)]
valAny, existAny := mapAny.GetString(strconv.Itoa(i))
if existOffical != existAny {
t.Fatalf("unexpected value obtained; got %v want %v", existAny, existOffical)
}
if valOffical != valAny {
t.Fatalf("unexpected value obtained; got %q want %q", valAny, valOffical)
}
}
wgGetAfterDeleted.Done()
}(i, &wgGetAfterDeleted)
}
wgGetAfterDeleted.Wait()
log.Println("清除官方map前GC耗时")
gcRunAndPrint()
mapOffical = nil
log.Println("清除官方map后GC耗时")
gcRunAndPrint()
log.Println("开始测试同时并发读写删,因为读写锁耗时会比较长")
//边写,边删
var wgSetGetDelete sync.WaitGroup
wgSetGetDelete.Add(chanNum)
for i := 0; i < chanNum; i++ {
go func(i int, wg *sync.WaitGroup) {
for i := 0; i < max+100; i++ {
mapAny.SetString(strconv.Itoa(i), strconv.Itoa(i))
mapAny.GetString(strconv.Itoa(i - 1))
mapAny.DeleteString(strconv.Itoa(i - 1))
}
wgSetGetDelete.Done()
}(i, &wgSetGetDelete)
}
wgSetGetDelete.Wait()
mapAny = nil
log.Println("清除所有数据")
gcRunAndPrint()
log.Println("开始测试2000W键值对 GC耗时")
mapAny2 := New()
for i := 0; i < 20000000; i++ {
mapAny2.SetString(strconv.Itoa(i), "@"+strconv.Itoa(i)+str)
}
gcRunAndPrint()
}
//一般是第二次执行才是真实耗时
func gcRunAndPrint() {
runtime.GC()
debug.FreeOSMemory()
begin_time := time.Now()
runtime.GC()
debug.FreeOSMemory()
log.Println("GC耗时:", "耗时:", time.Now().Sub(begin_time).String())
}