github.com/TeaOSLab/EdgeNode@v1.3.8/internal/caches/item_test.go (about) 1 // Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved. 2 3 package caches_test 4 5 import ( 6 "encoding/json" 7 "github.com/TeaOSLab/EdgeNode/internal/caches" 8 "github.com/TeaOSLab/EdgeNode/internal/utils/fasttime" 9 "github.com/TeaOSLab/EdgeNode/internal/utils/testutils" 10 "github.com/TeaOSLab/EdgeNode/internal/zero" 11 "github.com/iwind/TeaGo/rands" 12 "github.com/iwind/TeaGo/types" 13 "runtime" 14 "testing" 15 "time" 16 ) 17 18 func TestItem_Marshal(t *testing.T) { 19 { 20 var item = &caches.Item{} 21 data, err := json.Marshal(item) 22 if err != nil { 23 t.Fatal(err) 24 } 25 t.Log(string(data)) 26 } 27 28 { 29 var item = &caches.Item{ 30 Type: caches.ItemTypeFile, 31 Key: "https://example.com/index.html", 32 ExpiresAt: fasttime.Now().Unix(), 33 HeaderSize: 1 << 10, 34 BodySize: 1 << 20, 35 MetaSize: 256, 36 } 37 data, err := json.Marshal(item) 38 if err != nil { 39 t.Fatal(err) 40 } 41 t.Log(string(data)) 42 } 43 } 44 45 func TestItems_Memory(t *testing.T) { 46 var stat = &runtime.MemStats{} 47 runtime.ReadMemStats(stat) 48 var memory1 = stat.HeapInuse 49 50 var items = []*caches.Item{} 51 var count = 100 52 if testutils.IsSingleTesting() { 53 count = 10_000_000 54 } 55 for i := 0; i < count; i++ { 56 items = append(items, &caches.Item{ 57 Key: types.String(i), 58 }) 59 } 60 61 runtime.ReadMemStats(stat) 62 var memory2 = stat.HeapInuse 63 64 t.Log(memory1, memory2, (memory2-memory1)/1024/1024, "M") 65 66 runtime.ReadMemStats(stat) 67 var memory3 = stat.HeapInuse 68 t.Log(memory2, memory3, (memory3-memory2)/1024/1024, "M") 69 70 if testutils.IsSingleTesting() { 71 time.Sleep(1 * time.Second) 72 } 73 } 74 75 func TestItems_Memory2(t *testing.T) { 76 var stat = &runtime.MemStats{} 77 runtime.ReadMemStats(stat) 78 var memory1 = stat.HeapInuse 79 80 var items = map[int32]map[string]zero.Zero{} 81 var count = 100 82 if testutils.IsSingleTesting() { 83 count = 10_000_000 84 } 85 86 for i := 0; i < count; i++ { 87 var week = int32((time.Now().Unix() - int64(86400*rands.Int(0, 300))) / (86400 * 7)) 88 m, ok := items[week] 89 if !ok { 90 m = map[string]zero.Zero{} 91 items[week] = m 92 } 93 m[types.String(int64(i)*1_000_000)] = zero.New() 94 } 95 96 runtime.ReadMemStats(stat) 97 var memory2 = stat.HeapInuse 98 99 t.Log(memory1, memory2, (memory2-memory1)/1024/1024, "M") 100 101 if testutils.IsSingleTesting() { 102 time.Sleep(1 * time.Second) 103 } 104 for w, i := range items { 105 t.Log(w, len(i)) 106 } 107 } 108 109 func TestItem_RequestURI(t *testing.T) { 110 for _, u := range []string{ 111 "https://goedge.cn/hello/world", 112 "https://goedge.cn:8080/hello/world", 113 "https://goedge.cn/hello/world?v=1&t=123", 114 } { 115 var item = &caches.Item{Key: u} 116 t.Log(u, "=>", item.RequestURI()) 117 } 118 }