github.com/TeaOSLab/EdgeNode@v1.3.8/internal/utils/cachehits/stat_test.go (about) 1 // Copyright 2023 GoEdge CDN goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn . 2 3 package cachehits_test 4 5 import ( 6 "github.com/TeaOSLab/EdgeNode/internal/utils/cachehits" 7 "github.com/TeaOSLab/EdgeNode/internal/utils/testutils" 8 "github.com/iwind/TeaGo/assert" 9 "github.com/iwind/TeaGo/rands" 10 "github.com/iwind/TeaGo/types" 11 "runtime" 12 "strconv" 13 "testing" 14 "time" 15 ) 16 17 func TestNewStat(t *testing.T) { 18 var a = assert.NewAssertion(t) 19 20 { 21 var stat = cachehits.NewStat(20) 22 for i := 0; i < 1000; i++ { 23 stat.IncreaseCached("a") 24 } 25 26 a.IsTrue(stat.IsGood("a")) 27 } 28 29 { 30 var stat = cachehits.NewStat(5) 31 for i := 0; i < 10000; i++ { 32 stat.IncreaseCached("a") 33 } 34 for i := 0; i < 500; i++ { 35 stat.IncreaseHit("a") 36 } 37 38 stat.IncreaseHit("b") // empty 39 40 a.IsTrue(stat.IsGood("a")) 41 a.IsTrue(stat.IsGood("b")) 42 } 43 44 { 45 var stat = cachehits.NewStat(10) 46 for i := 0; i < 10000; i++ { 47 stat.IncreaseCached("a") 48 } 49 for i := 0; i < 1000; i++ { 50 stat.IncreaseHit("a") 51 } 52 53 stat.IncreaseHit("b") // empty 54 55 a.IsTrue(stat.IsGood("a")) 56 a.IsTrue(stat.IsGood("b")) 57 } 58 59 { 60 var stat = cachehits.NewStat(5) 61 for i := 0; i < 100001; i++ { 62 stat.IncreaseCached("a") 63 } 64 for i := 0; i < 4999; i++ { 65 stat.IncreaseHit("a") 66 } 67 68 a.IsFalse(stat.IsGood("a")) 69 } 70 } 71 72 func TestNewStat_Memory(t *testing.T) { 73 if !testutils.IsSingleTesting() { 74 return 75 } 76 77 var stat = cachehits.NewStat(20) 78 for i := 0; i < 10_000_000; i++ { 79 stat.IncreaseCached("a" + types.String(i)) 80 } 81 82 time.Sleep(60 * time.Second) 83 84 t.Log(stat.Len()) 85 } 86 87 func BenchmarkStat(b *testing.B) { 88 runtime.GOMAXPROCS(4) 89 90 var stat = cachehits.NewStat(5) 91 for i := 0; i < 1_000_000; i++ { 92 stat.IncreaseCached("a" + types.String(i)) 93 } 94 95 b.ResetTimer() 96 97 b.RunParallel(func(pb *testing.PB) { 98 for pb.Next() { 99 var key = strconv.Itoa(rands.Int(0, 100_000)) 100 stat.IncreaseCached(key) 101 if rands.Int(0, 3) == 0 { 102 stat.IncreaseHit(key) 103 } 104 _ = stat.IsGood(key) 105 } 106 }) 107 }