github.com/TeaOSLab/EdgeNode@v1.3.8/internal/caches/storage_memory_test.go (about) 1 package caches 2 3 import ( 4 "bytes" 5 "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs" 6 "github.com/TeaOSLab/EdgeNode/internal/utils/testutils" 7 "github.com/iwind/TeaGo/logs" 8 "github.com/iwind/TeaGo/rands" 9 "math/rand" 10 "runtime" 11 "runtime/debug" 12 "strconv" 13 "sync" 14 "testing" 15 "time" 16 ) 17 18 func TestMemoryStorage_OpenWriter(t *testing.T) { 19 var storage = NewMemoryStorage(&serverconfigs.HTTPCachePolicy{}, nil) 20 21 writer, err := storage.OpenWriter("abc", time.Now().Unix()+60, 200, -1, -1, -1, false) 22 if err != nil { 23 t.Fatal(err) 24 } 25 if err != nil { 26 t.Fatal(err) 27 } 28 _, _ = writer.WriteHeader([]byte("Header")) 29 _, _ = writer.Write([]byte("Hello")) 30 _, _ = writer.Write([]byte(", World")) 31 err = writer.Close() 32 if err != nil { 33 t.Fatal(err) 34 } 35 t.Log(storage.valuesMap) 36 37 { 38 reader, err := storage.OpenReader("abc", false, false) 39 if err != nil { 40 if err == ErrNotFound { 41 t.Log("not found: abc") 42 return 43 } else { 44 t.Fatal(err) 45 } 46 } 47 buf := make([]byte, 1024) 48 t.Log("status:", reader.Status()) 49 err = reader.ReadHeader(buf, func(n int) (goNext bool, err error) { 50 t.Log("header:", string(buf[:n])) 51 return true, nil 52 }) 53 if err != nil { 54 t.Fatal(err) 55 } 56 err = reader.ReadBody(buf, func(n int) (goNext bool, err error) { 57 t.Log("body:", string(buf[:n])) 58 return true, nil 59 }) 60 if err != nil { 61 t.Fatal(err) 62 } 63 } 64 65 { 66 _, err := storage.OpenReader("abc 2", false, false) 67 if err != nil { 68 if err == ErrNotFound { 69 t.Log("not found: abc2") 70 } else { 71 t.Fatal(err) 72 } 73 } 74 } 75 76 writer, err = storage.OpenWriter("abc", time.Now().Unix()+60, 200, -1, -1, -1, false) 77 if err != nil { 78 t.Fatal(err) 79 } 80 _, _ = writer.Write([]byte("Hello123")) 81 { 82 reader, err := storage.OpenReader("abc", false, false) 83 if err != nil { 84 if err == ErrNotFound { 85 t.Log("not found: abc") 86 } else { 87 t.Fatal(err) 88 } 89 } 90 buf := make([]byte, 1024) 91 err = reader.ReadBody(buf, func(n int) (goNext bool, err error) { 92 t.Log("abc:", string(buf[:n])) 93 return true, nil 94 }) 95 if err != nil { 96 t.Fatal(err) 97 } 98 } 99 } 100 101 func TestMemoryStorage_OpenReaderLock(t *testing.T) { 102 storage := NewMemoryStorage(&serverconfigs.HTTPCachePolicy{}, nil) 103 _ = storage.Init() 104 105 var h = storage.hash("test") 106 storage.valuesMap = map[uint64]*MemoryItem{ 107 h: { 108 IsDone: true, 109 }, 110 } 111 _, _ = storage.OpenReader("test", false, false) 112 } 113 114 func TestMemoryStorage_Delete(t *testing.T) { 115 var storage = NewMemoryStorage(&serverconfigs.HTTPCachePolicy{}, nil) 116 { 117 writer, err := storage.OpenWriter("abc", time.Now().Unix()+60, 200, -1, -1, -1, false) 118 if err != nil { 119 t.Fatal(err) 120 } 121 _, _ = writer.Write([]byte("Hello")) 122 err = writer.Close() 123 if err != nil { 124 t.Fatal(err) 125 } 126 t.Log(len(storage.valuesMap)) 127 } 128 { 129 writer, err := storage.OpenWriter("abc1", time.Now().Unix()+60, 200, -1, -1, -1, false) 130 if err != nil { 131 t.Fatal(err) 132 } 133 _, _ = writer.Write([]byte("Hello")) 134 err = writer.Close() 135 if err != nil { 136 t.Fatal(err) 137 } 138 t.Log(len(storage.valuesMap)) 139 } 140 _ = storage.Delete("abc1") 141 t.Log(len(storage.valuesMap)) 142 } 143 144 func TestMemoryStorage_Stat(t *testing.T) { 145 var storage = NewMemoryStorage(&serverconfigs.HTTPCachePolicy{}, nil) 146 expiredAt := time.Now().Unix() + 60 147 { 148 writer, err := storage.OpenWriter("abc", expiredAt, 200, -1, -1, -1, false) 149 if err != nil { 150 t.Fatal(err) 151 } 152 _, _ = writer.Write([]byte("Hello")) 153 err = writer.Close() 154 if err != nil { 155 t.Fatal(err) 156 } 157 t.Log(len(storage.valuesMap)) 158 storage.AddToList(&Item{ 159 Key: "abc", 160 BodySize: 5, 161 ExpiresAt: expiredAt, 162 }) 163 } 164 { 165 writer, err := storage.OpenWriter("abc1", expiredAt, 200, -1, -1, -1, false) 166 if err != nil { 167 t.Fatal(err) 168 } 169 _, _ = writer.Write([]byte("Hello")) 170 err = writer.Close() 171 if err != nil { 172 t.Fatal(err) 173 } 174 t.Log(len(storage.valuesMap)) 175 storage.AddToList(&Item{ 176 Key: "abc1", 177 BodySize: 5, 178 ExpiresAt: expiredAt, 179 }) 180 } 181 stat, err := storage.Stat() 182 if err != nil { 183 t.Fatal(err) 184 } 185 t.Log("===stat===") 186 logs.PrintAsJSON(stat, t) 187 } 188 189 func TestMemoryStorage_CleanAll(t *testing.T) { 190 var storage = NewMemoryStorage(&serverconfigs.HTTPCachePolicy{}, nil) 191 var expiredAt = time.Now().Unix() + 60 192 { 193 writer, err := storage.OpenWriter("abc", expiredAt, 200, -1, -1, -1, false) 194 if err != nil { 195 t.Fatal(err) 196 } 197 _, _ = writer.Write([]byte("Hello")) 198 err = writer.Close() 199 if err != nil { 200 t.Fatal(err) 201 } 202 storage.AddToList(&Item{ 203 Key: "abc", 204 BodySize: 5, 205 ExpiresAt: expiredAt, 206 }) 207 } 208 { 209 writer, err := storage.OpenWriter("abc1", expiredAt, 200, -1, -1, -1, false) 210 if err != nil { 211 t.Fatal(err) 212 } 213 _, _ = writer.Write([]byte("Hello")) 214 err = writer.Close() 215 if err != nil { 216 t.Fatal(err) 217 } 218 storage.AddToList(&Item{ 219 Key: "abc1", 220 BodySize: 5, 221 ExpiresAt: expiredAt, 222 }) 223 } 224 err := storage.CleanAll() 225 if err != nil { 226 t.Fatal(err) 227 } 228 total, _ := storage.list.Count() 229 t.Log(total, len(storage.valuesMap)) 230 } 231 232 func TestMemoryStorage_Purge(t *testing.T) { 233 storage := NewMemoryStorage(&serverconfigs.HTTPCachePolicy{}, nil) 234 expiredAt := time.Now().Unix() + 60 235 { 236 writer, err := storage.OpenWriter("abc", expiredAt, 200, -1, -1, -1, false) 237 if err != nil { 238 t.Fatal(err) 239 } 240 _, _ = writer.Write([]byte("Hello")) 241 err = writer.Close() 242 if err != nil { 243 t.Fatal(err) 244 } 245 storage.AddToList(&Item{ 246 Key: "abc", 247 BodySize: 5, 248 ExpiresAt: expiredAt, 249 }) 250 } 251 { 252 writer, err := storage.OpenWriter("abc1", expiredAt, 200, -1, -1, -1, false) 253 if err != nil { 254 t.Fatal(err) 255 } 256 _, _ = writer.Write([]byte("Hello")) 257 err = writer.Close() 258 if err != nil { 259 t.Fatal(err) 260 } 261 storage.AddToList(&Item{ 262 Key: "abc1", 263 BodySize: 5, 264 ExpiresAt: expiredAt, 265 }) 266 } 267 err := storage.Purge([]string{"abc", "abc1"}, "") 268 if err != nil { 269 t.Fatal(err) 270 } 271 total, _ := storage.list.Count() 272 t.Log(total, len(storage.valuesMap)) 273 } 274 275 func TestMemoryStorage_Expire(t *testing.T) { 276 if !testutils.IsSingleTesting() { 277 return 278 } 279 280 var storage = NewMemoryStorage(&serverconfigs.HTTPCachePolicy{ 281 MemoryAutoPurgeInterval: 5, 282 }, nil) 283 err := storage.Init() 284 if err != nil { 285 t.Fatal(err) 286 } 287 288 for i := 0; i < 1000; i++ { 289 expiredAt := time.Now().Unix() + int64(rands.Int(0, 60)) 290 key := "abc" + strconv.Itoa(i) 291 writer, err := storage.OpenWriter(key, expiredAt, 200, -1, -1, -1, false) 292 if err != nil { 293 t.Fatal(err) 294 } 295 _, _ = writer.Write([]byte("Hello")) 296 err = writer.Close() 297 if err != nil { 298 t.Fatal(err) 299 } 300 storage.AddToList(&Item{ 301 Key: key, 302 BodySize: 5, 303 ExpiresAt: expiredAt, 304 }) 305 } 306 time.Sleep(70 * time.Second) 307 } 308 309 func TestMemoryStorage_Locker(t *testing.T) { 310 var storage = NewMemoryStorage(&serverconfigs.HTTPCachePolicy{}, nil) 311 err := storage.Init() 312 if err != nil { 313 t.Fatal(err) 314 } 315 storage.locker.Lock() 316 err = storage.deleteWithoutLocker("a") 317 storage.locker.Unlock() 318 if err != nil { 319 t.Fatal(err) 320 } 321 t.Log("ok") 322 } 323 324 func TestMemoryStorage_Stop(t *testing.T) { 325 var stat1 = &runtime.MemStats{} 326 runtime.ReadMemStats(stat1) 327 328 var m = map[uint64]*MemoryItem{} 329 for i := 0; i < 1_000_000; i++ { 330 m[uint64(i)] = &MemoryItem{ 331 HeaderValue: []byte("Hello, World"), 332 BodyValue: bytes.Repeat([]byte("Hello"), 1024), 333 } 334 } 335 336 m = map[uint64]*MemoryItem{} 337 338 var before = time.Now() 339 //runtime.GC() 340 debug.FreeOSMemory() 341 /**go func() { 342 time.Sleep(10 * time.Second) 343 runtime.GC() 344 }()**/ 345 t.Log(time.Since(before).Seconds()*1000, "ms") 346 347 var stat2 = &runtime.MemStats{} 348 runtime.ReadMemStats(stat2) 349 350 if stat2.HeapInuse > stat1.HeapInuse { 351 t.Log(stat2.HeapInuse, stat1.HeapInuse, (stat2.HeapInuse-stat1.HeapInuse)/1024/1024, "MB") 352 } else { 353 t.Log("0 MB") 354 } 355 356 t.Log(len(m)) 357 } 358 359 func BenchmarkValuesMap(b *testing.B) { 360 var m = map[uint64]*MemoryItem{} 361 var count = 1_000_000 362 for i := 0; i < count; i++ { 363 m[uint64(i)] = &MemoryItem{ 364 ExpiresAt: time.Now().Unix(), 365 } 366 } 367 b.Log(len(m)) 368 369 var locker = sync.Mutex{} 370 b.ResetTimer() 371 372 b.RunParallel(func(pb *testing.PB) { 373 for pb.Next() { 374 locker.Lock() 375 _, ok := m[uint64(rands.Int(0, 1_000_000))] 376 _ = ok 377 locker.Unlock() 378 379 locker.Lock() 380 delete(m, uint64(rands.Int(2, 1000000))) 381 locker.Unlock() 382 } 383 }) 384 } 385 386 func BenchmarkNewMemoryStorage(b *testing.B) { 387 var storage = NewMemoryStorage(&serverconfigs.HTTPCachePolicy{}, nil) 388 389 var data = bytes.Repeat([]byte{'A'}, 1024) 390 391 b.ResetTimer() 392 393 b.RunParallel(func(pb *testing.PB) { 394 for pb.Next() { 395 func() { 396 writer, err := storage.OpenWriter("abc"+strconv.Itoa(rand.Int()), time.Now().Unix()+60, 200, -1, -1, -1, false) 397 if err != nil { 398 b.Fatal(err) 399 } 400 if err != nil { 401 b.Fatal(err) 402 } 403 _, _ = writer.WriteHeader([]byte("Header")) 404 _, _ = writer.Write(data) 405 err = writer.Close() 406 if err != nil { 407 b.Fatal(err) 408 } 409 }() 410 } 411 }) 412 }