From 68d4b1898d4dddf0b0d3e6c5e86c417da15c116d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=A5=A5=E8=B6=85?= Date: Mon, 22 Jan 2024 11:21:19 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=B5=8B=E8=AF=95=E7=94=A8?= =?UTF-8?q?=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/utils/byte_pool_test.go | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/internal/utils/byte_pool_test.go b/internal/utils/byte_pool_test.go index 04c1faf..ed13e7d 100644 --- a/internal/utils/byte_pool_test.go +++ b/internal/utils/byte_pool_test.go @@ -1,6 +1,8 @@ -package utils +package utils_test import ( + "bytes" + "github.com/TeaOSLab/EdgeNode/internal/utils" "runtime" "sync" "testing" @@ -10,7 +12,7 @@ func TestBytePool_Memory(t *testing.T) { var stat1 = &runtime.MemStats{} runtime.ReadMemStats(stat1) - var pool = NewBytePool(32 * 1024) + var pool = utils.NewBytePool(32 * 1024) for i := 0; i < 20480; i++ { pool.Put(make([]byte, 32*1024)) } @@ -29,7 +31,7 @@ func TestBytePool_Memory(t *testing.T) { func BenchmarkBytePool_Get(b *testing.B) { runtime.GOMAXPROCS(1) - var pool = NewBytePool(1) + var pool = utils.NewBytePool(1) b.ResetTimer() for i := 0; i < b.N; i++ { @@ -42,7 +44,7 @@ func BenchmarkBytePool_Get(b *testing.B) { func BenchmarkBytePool_Get_Parallel(b *testing.B) { runtime.GOMAXPROCS(1) - var pool = NewBytePool(1024) + var pool = utils.NewBytePool(1024) b.ResetTimer() b.RunParallel(func(pb *testing.PB) { @@ -72,3 +74,20 @@ func BenchmarkBytePool_Get_Sync(b *testing.B) { } }) } + +func BenchmarkBytePool_Copy(b *testing.B) { + var data = bytes.Repeat([]byte{'A'}, 8<<10) + + var pool = &sync.Pool{ + New: func() any { + return make([]byte, 8<<10) + }, + } + b.RunParallel(func(pb *testing.PB) { + for pb.Next() { + var buf = pool.Get().([]byte) + copy(buf, data) + pool.Put(buf) + } + }) +}