github.com/TeaOSLab/EdgeNode@v1.3.8/internal/utils/byte_pool_test.go (about) 1 package utils_test 2 3 import ( 4 "bytes" 5 "github.com/TeaOSLab/EdgeNode/internal/utils" 6 "runtime" 7 "sync" 8 "testing" 9 ) 10 11 func TestBytePool_Memory(t *testing.T) { 12 var stat1 = &runtime.MemStats{} 13 runtime.ReadMemStats(stat1) 14 15 var pool = utils.NewBytePool(32 * 1024) 16 for i := 0; i < 20480; i++ { 17 pool.Put(&utils.BytesBuf{ 18 Bytes: make([]byte, 32*1024), 19 }) 20 } 21 22 //pool.Purge() 23 24 //time.Sleep(60 * time.Second) 25 26 runtime.GC() 27 28 var stat2 = &runtime.MemStats{} 29 runtime.ReadMemStats(stat2) 30 t.Log((stat2.HeapInuse-stat1.HeapInuse)/1024/1024, "MB,") 31 } 32 33 func BenchmarkBytePool_Get(b *testing.B) { 34 runtime.GOMAXPROCS(1) 35 36 var pool = utils.NewBytePool(1) 37 b.ResetTimer() 38 39 for i := 0; i < b.N; i++ { 40 var buf = pool.Get() 41 _ = buf 42 pool.Put(buf) 43 } 44 } 45 46 func BenchmarkBytePool_Get_Parallel(b *testing.B) { 47 runtime.GOMAXPROCS(1) 48 49 var pool = utils.NewBytePool(1024) 50 b.ResetTimer() 51 52 b.RunParallel(func(pb *testing.PB) { 53 for pb.Next() { 54 var buf = pool.Get() 55 pool.Put(buf) 56 } 57 }) 58 } 59 60 func BenchmarkBytePool_Get_Sync(b *testing.B) { 61 runtime.GOMAXPROCS(1) 62 63 var pool = &sync.Pool{ 64 New: func() any { 65 return make([]byte, 1024) 66 }, 67 } 68 69 b.ReportAllocs() 70 b.ResetTimer() 71 72 b.RunParallel(func(pb *testing.PB) { 73 for pb.Next() { 74 var buf = pool.Get() 75 pool.Put(buf) 76 } 77 }) 78 } 79 80 func BenchmarkBytePool_Get_Sync2(b *testing.B) { 81 runtime.GOMAXPROCS(1) 82 83 var pool = &sync.Pool{ 84 New: func() any { 85 return &utils.BytesBuf{ 86 Bytes: make([]byte, 1024), 87 } 88 }, 89 } 90 91 b.ReportAllocs() 92 b.ResetTimer() 93 94 b.RunParallel(func(pb *testing.PB) { 95 for pb.Next() { 96 var buf = pool.Get() 97 pool.Put(buf) 98 } 99 }) 100 } 101 102 func BenchmarkBytePool_Copy_Bytes_4(b *testing.B) { 103 const size = 4 << 10 104 105 var data = bytes.Repeat([]byte{'A'}, size) 106 107 var pool = &sync.Pool{ 108 New: func() any { 109 return make([]byte, size) 110 }, 111 } 112 b.ResetTimer() 113 114 b.RunParallel(func(pb *testing.PB) { 115 for pb.Next() { 116 var buf = pool.Get().([]byte) 117 copy(buf, data) 118 pool.Put(buf) 119 } 120 }) 121 } 122 123 func BenchmarkBytePool_Copy_Wrapper_4(b *testing.B) { 124 const size = 4 << 10 125 126 var data = bytes.Repeat([]byte{'A'}, size) 127 128 var pool = &sync.Pool{ 129 New: func() any { 130 return &utils.BytesBuf{ 131 Bytes: make([]byte, size), 132 } 133 }, 134 } 135 b.ResetTimer() 136 137 b.RunParallel(func(pb *testing.PB) { 138 for pb.Next() { 139 var buf = pool.Get().(*utils.BytesBuf) 140 copy(buf.Bytes, data) 141 pool.Put(buf) 142 } 143 }) 144 } 145 146 func BenchmarkBytePool_Copy_Bytes_16(b *testing.B) { 147 const size = 16 << 10 148 149 var data = bytes.Repeat([]byte{'A'}, size) 150 151 var pool = &sync.Pool{ 152 New: func() any { 153 return make([]byte, size) 154 }, 155 } 156 b.ResetTimer() 157 158 b.RunParallel(func(pb *testing.PB) { 159 for pb.Next() { 160 var buf = pool.Get().([]byte) 161 copy(buf, data) 162 pool.Put(buf) 163 } 164 }) 165 } 166 167 func BenchmarkBytePool_Copy_Wrapper_16(b *testing.B) { 168 const size = 16 << 10 169 170 var data = bytes.Repeat([]byte{'A'}, size) 171 172 var pool = &sync.Pool{ 173 New: func() any { 174 return &utils.BytesBuf{ 175 Bytes: make([]byte, size), 176 } 177 }, 178 } 179 b.ResetTimer() 180 181 b.RunParallel(func(pb *testing.PB) { 182 for pb.Next() { 183 var buf = pool.Get().(*utils.BytesBuf) 184 copy(buf.Bytes, data) 185 pool.Put(buf) 186 } 187 }) 188 } 189 190 func BenchmarkBytePool_Copy_Wrapper_Buf_16(b *testing.B) { 191 const size = 16 << 10 192 193 var data = bytes.Repeat([]byte{'A'}, size) 194 195 var pool = &sync.Pool{ 196 New: func() any { 197 return &utils.BytesBuf{ 198 Bytes: make([]byte, size), 199 } 200 }, 201 } 202 b.ResetTimer() 203 204 b.RunParallel(func(pb *testing.PB) { 205 for pb.Next() { 206 var bytesPtr = pool.Get().(*utils.BytesBuf) 207 var buf = bytesPtr.Bytes 208 copy(buf, data) 209 pool.Put(bytesPtr) 210 } 211 }) 212 } 213 214 func BenchmarkBytePool_Copy_Wrapper_BytePool_16(b *testing.B) { 215 const size = 16 << 10 216 217 var data = bytes.Repeat([]byte{'A'}, size) 218 219 b.ResetTimer() 220 221 b.RunParallel(func(pb *testing.PB) { 222 for pb.Next() { 223 var bytesPtr = utils.BytePool16k.Get() 224 copy(bytesPtr.Bytes, data) 225 utils.BytePool16k.Put(bytesPtr) 226 } 227 }) 228 } 229 230 func BenchmarkBytePool_Copy_Bytes_32(b *testing.B) { 231 const size = 32 << 10 232 233 var data = bytes.Repeat([]byte{'A'}, size) 234 235 var pool = &sync.Pool{ 236 New: func() any { 237 return make([]byte, size) 238 }, 239 } 240 b.ResetTimer() 241 242 b.RunParallel(func(pb *testing.PB) { 243 for pb.Next() { 244 var buf = pool.Get().([]byte) 245 copy(buf, data) 246 pool.Put(buf) 247 } 248 }) 249 } 250 251 func BenchmarkBytePool_Copy_Wrapper_32(b *testing.B) { 252 const size = 32 << 10 253 254 var data = bytes.Repeat([]byte{'A'}, size) 255 256 var pool = &sync.Pool{ 257 New: func() any { 258 return &utils.BytesBuf{ 259 Bytes: make([]byte, size), 260 } 261 }, 262 } 263 b.ResetTimer() 264 265 b.RunParallel(func(pb *testing.PB) { 266 for pb.Next() { 267 var buf = pool.Get().(*utils.BytesBuf) 268 copy(buf.Bytes, data) 269 pool.Put(buf) 270 } 271 }) 272 }