mirror of
https://github.com/TeaOSLab/EdgeNode.git
synced 2025-11-09 20:10:27 +08:00
优化字节缓冲区相关代码
This commit is contained in:
@@ -14,7 +14,9 @@ func TestBytePool_Memory(t *testing.T) {
|
||||
|
||||
var pool = utils.NewBytePool(32 * 1024)
|
||||
for i := 0; i < 20480; i++ {
|
||||
pool.Put(make([]byte, 32*1024))
|
||||
pool.Put(&utils.BytesBuf{
|
||||
Bytes: make([]byte, 32*1024),
|
||||
})
|
||||
}
|
||||
|
||||
//pool.Purge()
|
||||
@@ -75,14 +77,40 @@ func BenchmarkBytePool_Get_Sync(b *testing.B) {
|
||||
})
|
||||
}
|
||||
|
||||
func BenchmarkBytePool_Copy(b *testing.B) {
|
||||
var data = bytes.Repeat([]byte{'A'}, 8<<10)
|
||||
func BenchmarkBytePool_Get_Sync2(b *testing.B) {
|
||||
runtime.GOMAXPROCS(1)
|
||||
|
||||
var pool = &sync.Pool{
|
||||
New: func() any {
|
||||
return make([]byte, 8<<10)
|
||||
return &utils.BytesBuf{
|
||||
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)
|
||||
|
||||
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)
|
||||
@@ -91,3 +119,154 @@ func BenchmarkBytePool_Copy(b *testing.B) {
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
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 {
|
||||
return &utils.BytesBuf{
|
||||
Bytes: make([]byte, size),
|
||||
}
|
||||
},
|
||||
}
|
||||
b.ResetTimer()
|
||||
|
||||
b.RunParallel(func(pb *testing.PB) {
|
||||
for pb.Next() {
|
||||
var buf = pool.Get().(*utils.BytesBuf)
|
||||
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 {
|
||||
return &utils.BytesBuf{
|
||||
Bytes: make([]byte, size),
|
||||
}
|
||||
},
|
||||
}
|
||||
b.ResetTimer()
|
||||
|
||||
b.RunParallel(func(pb *testing.PB) {
|
||||
for pb.Next() {
|
||||
var buf = pool.Get().(*utils.BytesBuf)
|
||||
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 {
|
||||
return &utils.BytesBuf{
|
||||
Bytes: make([]byte, size),
|
||||
}
|
||||
},
|
||||
}
|
||||
b.ResetTimer()
|
||||
|
||||
b.RunParallel(func(pb *testing.PB) {
|
||||
for pb.Next() {
|
||||
var bytesPtr = pool.Get().(*utils.BytesBuf)
|
||||
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() {
|
||||
var bytesPtr = utils.BytePool16k.Get()
|
||||
copy(bytesPtr.Bytes, data)
|
||||
utils.BytePool16k.Put(bytesPtr)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
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 {
|
||||
return &utils.BytesBuf{
|
||||
Bytes: make([]byte, size),
|
||||
}
|
||||
},
|
||||
}
|
||||
b.ResetTimer()
|
||||
|
||||
b.RunParallel(func(pb *testing.PB) {
|
||||
for pb.Next() {
|
||||
var buf = pool.Get().(*utils.BytesBuf)
|
||||
copy(buf.Bytes, data)
|
||||
pool.Put(buf)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user