github.com/benchplus/gocache@v0.0.0-20220303084804-56d5996c6777/go-cache/gocache_test.go (about) 1 package benchplus 2 3 import ( 4 "fmt" 5 "os" 6 "runtime/debug" 7 "sync" 8 "testing" 9 "time" 10 11 "github.com/benchplus/gocache" 12 "github.com/golang/protobuf/proto" 13 "github.com/patrickmn/go-cache" 14 ) 15 16 func TestMain(m *testing.M) { 17 setup() 18 code := m.Run() 19 shutdown() 20 os.Exit(code) 21 } 22 23 func setup() { 24 debug.SetGCPercent(10) 25 } 26 27 func shutdown() { 28 gocache.PrintGCPause() 29 gocache.PrintMem() 30 gocache.PrintRate() 31 } 32 33 func BenchmarkHeavyMixedInt_gocache(b *testing.B) { 34 c := cache.New(10*time.Second, 0) 35 var wg sync.WaitGroup 36 for index := 0; index < 10000; index++ { 37 wg.Add(1) 38 go func() { 39 for i := 0; i < 8192; i++ { 40 c.Set(gocache.Int64Key(int64(i)), i+1, cache.DefaultExpiration) 41 } 42 wg.Done() 43 }() 44 wg.Add(1) 45 go func() { 46 for i := 0; i < 8192; i++ { 47 c.Get(gocache.Int64Key(int64(i))) 48 } 49 wg.Done() 50 }() 51 } 52 wg.Wait() 53 54 gocache.AddMem() 55 } 56 57 func BenchmarkPutInt_gocache(b *testing.B) { 58 c := cache.New(10*time.Second, 0) 59 for i := 0; i < b.N; i++ { 60 c.Set(gocache.Int64Key(int64(i)), i+1, cache.DefaultExpiration) 61 } 62 } 63 64 func BenchmarkGetInt_gocache(b *testing.B) { 65 c := cache.New(10*time.Second, 0) 66 c.Set("0", "0", cache.DefaultExpiration) 67 for i := 0; i < b.N; i++ { 68 c.Get("0") 69 } 70 } 71 72 func BenchmarkPut1K_gocache(b *testing.B) { 73 c := cache.New(10*time.Second, 0) 74 for i := 0; i < b.N; i++ { 75 c.Set(gocache.Int64Key(int64(i)), gocache.Data1K, cache.DefaultExpiration) 76 } 77 } 78 79 func BenchmarkPut1M_gocache(b *testing.B) { 80 c := cache.New(10*time.Second, 0) 81 for i := 0; i < b.N; i++ { 82 c.Set(gocache.Int64Key(int64(i)), gocache.Data1M, cache.DefaultExpiration) 83 } 84 } 85 86 func BenchmarkPutTinyObject_gocache(b *testing.B) { 87 c := cache.New(10*time.Second, 0) 88 for i := 0; i < b.N; i++ { 89 data, _ := proto.Marshal(&gocache.UserInfo{}) 90 c.Set(gocache.Int64Key(int64(i)), data, cache.DefaultExpiration) 91 } 92 } 93 94 func BenchmarkChangeOutAllInt_gocache(b *testing.B) { 95 c := cache.New(10*time.Second, 0) 96 for i := 0; i < b.N*1024; i++ { 97 c.Set(gocache.Int64Key(int64(i)), i+1, cache.DefaultExpiration) 98 } 99 } 100 101 func BenchmarkHeavyReadInt_gocache(b *testing.B) { 102 gocache.GCPause() 103 104 c := cache.New(10*time.Second, 0) 105 for i := 0; i < 1024; i++ { 106 c.Set(gocache.Int64Key(int64(i)), i+1, cache.DefaultExpiration) 107 } 108 var wg sync.WaitGroup 109 for index := 0; index < 10000; index++ { 110 wg.Add(1) 111 go func() { 112 for i := 0; i < 1024; i++ { 113 c.Get(gocache.Int64Key(int64(i))) 114 } 115 wg.Done() 116 }() 117 } 118 wg.Wait() 119 120 gocache.AddGCPause() 121 } 122 123 func BenchmarkHeavyWriteInt_gocache(b *testing.B) { 124 gocache.GCPause() 125 126 c := cache.New(10*time.Second, 0) 127 var wg sync.WaitGroup 128 for index := 0; index < 10000; index++ { 129 start := index 130 wg.Add(1) 131 go func() { 132 for i := 0; i < 8192; i++ { 133 c.Set(gocache.Int64Key(int64(i+start)), i+1, cache.DefaultExpiration) 134 } 135 wg.Done() 136 }() 137 } 138 wg.Wait() 139 140 gocache.AddGCPause() 141 } 142 143 func BenchmarkHeavyWrite1K_gocache(b *testing.B) { 144 gocache.GCPause() 145 146 c := cache.New(10*time.Second, 0) 147 var wg sync.WaitGroup 148 for index := 0; index < 10000; index++ { 149 start := index 150 wg.Add(1) 151 go func() { 152 for i := 0; i < 8192; i++ { 153 c.Set(gocache.Int64Key(int64(i+start)), gocache.Data1K, cache.DefaultExpiration) 154 } 155 wg.Done() 156 }() 157 } 158 wg.Wait() 159 160 gocache.AddGCPause() 161 } 162 163 func BenchmarkCacheHitRate_gocache(b *testing.B) { 164 fmt.Println("no print") 165 gocache.AddRate(0.0) 166 }