Skip to content
This repository was archived by the owner on Nov 5, 2024. It is now read-only.

Commit f5e3562

Browse files
committed
对内存缓存增加多个测试
1 parent db77644 commit f5e3562

3 files changed

Lines changed: 51 additions & 0 deletions

File tree

teamemory/cell.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,11 @@ func (this *Cell) Range(f func(item *Item)) {
160160

161161
func (this *Cell) Recycle() {
162162
this.locker.Lock()
163+
if len(this.mapping) == 0 {
164+
this.locker.Unlock()
165+
return
166+
}
167+
163168
timestamp := time.Now().Unix()
164169
for key, item := range this.mapping {
165170
if item.ExpireAt < timestamp {
@@ -168,6 +173,7 @@ func (this *Cell) Recycle() {
168173
this.totalBytes -= item.Size()
169174
}
170175
}
176+
171177
this.locker.Unlock()
172178
}
173179

teamemory/grid_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,3 +179,26 @@ func TestMemoryGrid_Destroy(t *testing.T) {
179179
t.Fatal("looper != nil")
180180
}
181181
}
182+
183+
func TestMemoryGrid_Recycle(t *testing.T) {
184+
cell := NewCell()
185+
timestamp := time.Now().Unix()
186+
for i := 0; i < 300_0000; i++ {
187+
cell.Write(uint64(i), &Item{
188+
ExpireAt: timestamp - 30,
189+
})
190+
}
191+
before := time.Now()
192+
cell.Recycle()
193+
t.Log(time.Since(before).Seconds()*1000, "ms")
194+
t.Log(len(cell.mapping))
195+
196+
runtime.GC()
197+
printMem(t)
198+
}
199+
200+
func printMem(t *testing.T) {
201+
mem := &runtime.MemStats{}
202+
runtime.ReadMemStats(mem)
203+
t.Log(mem.Alloc/1024/1024, "M")
204+
}

teawaf/utils/utils_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package utils
22

33
import (
4+
"github.com/TeaWeb/code/teatesting"
5+
"net/http"
46
"regexp"
7+
"strconv"
58
"strings"
69
"testing"
710
)
@@ -20,6 +23,25 @@ func TestMatchBytesCache(t *testing.T) {
2023
t.Log(MatchBytesCache(regex, []byte("123")))
2124
}
2225

26+
func TestMatchRemoteCache(t *testing.T) {
27+
if teatesting.IsGlobal() {
28+
return
29+
}
30+
client := http.Client{}
31+
for i := 0; i < 200_0000; i++ {
32+
req, err := http.NewRequest(http.MethodGet, "http://192.168.2.30:8882/?arg="+strconv.Itoa(i), nil)
33+
if err != nil {
34+
t.Fatal(err)
35+
}
36+
req.Header.Set("User-Agent", "GoTest/"+strconv.Itoa(i))
37+
resp, err := client.Do(req)
38+
if err != nil {
39+
t.Fatal(err)
40+
}
41+
_ = resp.Body.Close()
42+
}
43+
}
44+
2345
func BenchmarkMatchStringCache(b *testing.B) {
2446
data := strings.Repeat("HELLO", 512)
2547
regex := regexp.MustCompile(`(?iU)\b(eval|system|exec|execute|passthru|shell_exec|phpinfo)\b`)

0 commit comments

Comments
 (0)