github.com/TeaOSLab/EdgeNode@v1.3.8/internal/caches/writer_memory_test.go (about) 1 // Copyright 2024 GoEdge CDN goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn . 2 3 package caches_test 4 5 import ( 6 "bytes" 7 "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs" 8 "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared" 9 "github.com/TeaOSLab/EdgeNode/internal/caches" 10 "math/rand" 11 "strconv" 12 "testing" 13 "time" 14 ) 15 16 func TestNewMemoryWriter(t *testing.T) { 17 var storage = caches.NewMemoryStorage(&serverconfigs.HTTPCachePolicy{ 18 Id: 0, 19 IsOn: false, 20 Name: "", 21 Description: "", 22 Capacity: &shared.SizeCapacity{ 23 Count: 8, 24 Unit: shared.SizeCapacityUnitGB, 25 }, 26 }, nil) 27 err := storage.Init() 28 if err != nil { 29 t.Fatal(err) 30 } 31 32 const size = 1 << 20 33 const chunkSize = 16 << 10 34 var data = bytes.Repeat([]byte{'A'}, chunkSize) 35 36 var before = time.Now() 37 38 var writer = caches.NewMemoryWriter(storage, "a", time.Now().Unix()+3600, 200, false, size, 1<<30, func(valueItem *caches.MemoryItem) { 39 t.Log(len(valueItem.BodyValue), "bytes") 40 }) 41 42 for i := 0; i < size/chunkSize; i++ { 43 _, err = writer.Write(data) 44 if err != nil { 45 t.Fatal(err) 46 } 47 } 48 49 err = writer.Close() 50 if err != nil { 51 t.Fatal(err) 52 } 53 54 t.Log("cost:", time.Since(before).Seconds()*1000, "ms") 55 } 56 57 func BenchmarkMemoryWriter_Capacity(b *testing.B) { 58 b.ReportAllocs() 59 60 var storage = caches.NewMemoryStorage(&serverconfigs.HTTPCachePolicy{ 61 Id: 0, 62 IsOn: false, 63 Name: "", 64 Description: "", 65 Capacity: &shared.SizeCapacity{ 66 Count: 8, 67 Unit: shared.SizeCapacityUnitGB, 68 }, 69 }, nil) 70 initErr := storage.Init() 71 if initErr != nil { 72 b.Fatal(initErr) 73 } 74 75 const size = 1 << 20 76 const chunkSize = 16 << 10 77 var data = bytes.Repeat([]byte{'A'}, chunkSize) 78 79 b.ResetTimer() 80 81 b.RunParallel(func(pb *testing.PB) { 82 for pb.Next() { 83 var writer = caches.NewMemoryWriter(storage, "a"+strconv.Itoa(rand.Int()), time.Now().Unix()+3600, 200, false, size, 1<<30, func(valueItem *caches.MemoryItem) { 84 }) 85 86 for i := 0; i < size/chunkSize; i++ { 87 _, err := writer.Write(data) 88 if err != nil { 89 b.Fatal(err) 90 } 91 } 92 93 err := writer.Close() 94 if err != nil { 95 b.Fatal(err) 96 } 97 } 98 }) 99 } 100 101 func BenchmarkMemoryWriter_Capacity_Disabled(b *testing.B) { 102 b.ReportAllocs() 103 104 var storage = caches.NewMemoryStorage(&serverconfigs.HTTPCachePolicy{ 105 Id: 0, 106 IsOn: false, 107 Name: "", 108 Description: "", 109 Capacity: &shared.SizeCapacity{ 110 Count: 8, 111 Unit: shared.SizeCapacityUnitGB, 112 }, 113 }, nil) 114 initErr := storage.Init() 115 if initErr != nil { 116 b.Fatal(initErr) 117 } 118 119 const size = 1 << 20 120 const chunkSize = 16 << 10 121 var data = bytes.Repeat([]byte{'A'}, chunkSize) 122 123 b.ResetTimer() 124 125 b.RunParallel(func(pb *testing.PB) { 126 for pb.Next() { 127 var writer = caches.NewMemoryWriter(storage, "a"+strconv.Itoa(rand.Int()), time.Now().Unix()+3600, 200, false, 0, 1<<30, func(valueItem *caches.MemoryItem) { 128 }) 129 130 for i := 0; i < size/chunkSize; i++ { 131 _, err := writer.Write(data) 132 if err != nil { 133 b.Fatal(err) 134 } 135 } 136 137 err := writer.Close() 138 if err != nil { 139 b.Fatal(err) 140 } 141 } 142 }) 143 }