Files
EdgeNode/internal/utils/bytepool/byte_pool_test.go

273 lines
4.5 KiB
Go
Raw Normal View History

2024-05-03 15:31:40 +08:00
package bytepool_test
2020-09-26 08:07:07 +08:00
import (
2024-01-22 11:21:19 +08:00
"bytes"
2024-05-03 15:31:40 +08:00
"github.com/TeaOSLab/EdgeNode/internal/utils/bytepool"
2020-09-26 08:07:07 +08:00
"runtime"
2022-08-07 11:12:29 +08:00
"sync"
2020-09-26 08:07:07 +08:00
"testing"
)
2021-12-19 11:32:26 +08:00
func TestBytePool_Memory(t *testing.T) {
var stat1 = &runtime.MemStats{}
runtime.ReadMemStats(stat1)
2024-05-03 15:31:40 +08:00
var pool = bytepool.NewPool(32 * 1024)
2021-12-19 11:32:26 +08:00
for i := 0; i < 20480; i++ {
2024-05-03 15:31:40 +08:00
pool.Put(&bytepool.Buf{
2024-04-15 09:26:00 +08:00
Bytes: make([]byte, 32*1024),
})
2021-12-19 11:32:26 +08:00
}
//pool.Purge()
//time.Sleep(60 * time.Second)
runtime.GC()
var stat2 = &runtime.MemStats{}
runtime.ReadMemStats(stat2)
2022-08-07 11:12:29 +08:00
t.Log((stat2.HeapInuse-stat1.HeapInuse)/1024/1024, "MB,")
2021-12-19 11:32:26 +08:00
}
2020-09-26 08:07:07 +08:00
func BenchmarkBytePool_Get(b *testing.B) {
runtime.GOMAXPROCS(1)
2024-05-03 15:31:40 +08:00
var pool = bytepool.NewPool(1)
2022-08-07 11:12:29 +08:00
b.ResetTimer()
2020-09-26 08:07:07 +08:00
for i := 0; i < b.N; i++ {
2022-08-07 11:12:29 +08:00
var buf = pool.Get()
2020-09-26 08:07:07 +08:00
_ = buf
pool.Put(buf)
}
2022-08-07 11:12:29 +08:00
}
func BenchmarkBytePool_Get_Parallel(b *testing.B) {
runtime.GOMAXPROCS(1)
2024-05-03 15:31:40 +08:00
var pool = bytepool.NewPool(1024)
2022-08-07 11:12:29 +08:00
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
var buf = pool.Get()
pool.Put(buf)
}
})
}
func BenchmarkBytePool_Get_Sync(b *testing.B) {
runtime.GOMAXPROCS(1)
var pool = &sync.Pool{
New: func() any {
return make([]byte, 1024)
},
}
2023-08-08 11:36:54 +08:00
b.ReportAllocs()
2022-08-07 11:12:29 +08:00
b.ResetTimer()
2020-09-26 08:07:07 +08:00
2022-08-07 11:12:29 +08:00
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
var buf = pool.Get()
pool.Put(buf)
}
})
2020-09-26 08:07:07 +08:00
}
2024-01-22 11:21:19 +08:00
2024-04-15 09:26:00 +08:00
func BenchmarkBytePool_Get_Sync2(b *testing.B) {
runtime.GOMAXPROCS(1)
var pool = &sync.Pool{
New: func() any {
2024-05-03 15:31:40 +08:00
return &bytepool.Buf{
2024-04-15 09:26:00 +08:00
Bytes: make([]byte, 1024),
}
},
}
b.ReportAllocs()
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
var buf = pool.Get()
pool.Put(buf)
}
})
}
func BenchmarkBytePool_Copy_Bytes_4(b *testing.B) {
const size = 4 << 10
var data = bytes.Repeat([]byte{'A'}, size)
2024-01-22 11:21:19 +08:00
var pool = &sync.Pool{
New: func() any {
2024-04-15 09:26:00 +08:00
return make([]byte, size)
2024-01-22 11:21:19 +08:00
},
}
2024-04-15 09:26:00 +08:00
b.ResetTimer()
2024-01-22 11:21:19 +08:00
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
var buf = pool.Get().([]byte)
copy(buf, data)
pool.Put(buf)
}
})
}
2024-04-15 09:26:00 +08:00
func BenchmarkBytePool_Copy_Wrapper_4(b *testing.B) {
const size = 4 << 10
var data = bytes.Repeat([]byte{'A'}, size)
var pool = &sync.Pool{
New: func() any {
2024-05-03 15:31:40 +08:00
return &bytepool.Buf{
2024-04-15 09:26:00 +08:00
Bytes: make([]byte, size),
}
},
}
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
2024-05-03 15:31:40 +08:00
var buf = pool.Get().(*bytepool.Buf)
2024-04-15 09:26:00 +08:00
copy(buf.Bytes, data)
pool.Put(buf)
}
})
}
func BenchmarkBytePool_Copy_Bytes_16(b *testing.B) {
const size = 16 << 10
var data = bytes.Repeat([]byte{'A'}, size)
var pool = &sync.Pool{
New: func() any {
return make([]byte, size)
},
}
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
var buf = pool.Get().([]byte)
copy(buf, data)
pool.Put(buf)
}
})
}
func BenchmarkBytePool_Copy_Wrapper_16(b *testing.B) {
const size = 16 << 10
var data = bytes.Repeat([]byte{'A'}, size)
var pool = &sync.Pool{
New: func() any {
2024-05-03 15:31:40 +08:00
return &bytepool.Buf{
2024-04-15 09:26:00 +08:00
Bytes: make([]byte, size),
}
},
}
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
2024-05-03 15:31:40 +08:00
var buf = pool.Get().(*bytepool.Buf)
2024-04-15 09:26:00 +08:00
copy(buf.Bytes, data)
pool.Put(buf)
}
})
}
func BenchmarkBytePool_Copy_Wrapper_Buf_16(b *testing.B) {
const size = 16 << 10
var data = bytes.Repeat([]byte{'A'}, size)
var pool = &sync.Pool{
New: func() any {
2024-05-03 15:31:40 +08:00
return &bytepool.Buf{
2024-04-15 09:26:00 +08:00
Bytes: make([]byte, size),
}
},
}
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
2024-05-03 15:31:40 +08:00
var bytesPtr = pool.Get().(*bytepool.Buf)
2024-04-15 09:26:00 +08:00
var buf = bytesPtr.Bytes
copy(buf, data)
pool.Put(bytesPtr)
}
})
}
func BenchmarkBytePool_Copy_Wrapper_BytePool_16(b *testing.B) {
const size = 16 << 10
var data = bytes.Repeat([]byte{'A'}, size)
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
2024-05-03 15:31:40 +08:00
var bytesPtr = bytepool.Pool16k.Get()
2024-04-15 09:26:00 +08:00
copy(bytesPtr.Bytes, data)
2024-05-03 15:31:40 +08:00
bytepool.Pool16k.Put(bytesPtr)
2024-04-15 09:26:00 +08:00
}
})
}
func BenchmarkBytePool_Copy_Bytes_32(b *testing.B) {
const size = 32 << 10
var data = bytes.Repeat([]byte{'A'}, size)
var pool = &sync.Pool{
New: func() any {
return make([]byte, size)
},
}
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
var buf = pool.Get().([]byte)
copy(buf, data)
pool.Put(buf)
}
})
}
func BenchmarkBytePool_Copy_Wrapper_32(b *testing.B) {
const size = 32 << 10
var data = bytes.Repeat([]byte{'A'}, size)
var pool = &sync.Pool{
New: func() any {
2024-05-03 15:31:40 +08:00
return &bytepool.Buf{
2024-04-15 09:26:00 +08:00
Bytes: make([]byte, size),
}
},
}
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
2024-05-03 15:31:40 +08:00
var buf = pool.Get().(*bytepool.Buf)
2024-04-15 09:26:00 +08:00
copy(buf.Bytes, data)
pool.Put(buf)
}
})
}