Files
tidb/br/pkg/membuf/buffer_test.go

253 lines
5.6 KiB
Go

// Copyright 2021 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package membuf
import (
"bytes"
"crypto/rand"
rand2 "math/rand"
"runtime"
"slices"
"testing"
"github.com/stretchr/testify/require"
)
type testAllocator struct {
allocs int
frees int
}
func (t *testAllocator) Alloc(n int) []byte {
t.allocs++
return make([]byte, n)
}
func (t *testAllocator) Free(_ []byte) {
t.frees++
}
func TestBufferPool(t *testing.T) {
allocator := &testAllocator{}
pool := NewPool(
WithBlockNum(2),
WithAllocator(allocator),
WithBlockSize(1024),
WithLargeAllocThreshold(512),
)
defer pool.Destroy()
bytesBuf := pool.NewBuffer()
bytesBuf.AllocBytes(256)
require.Equal(t, 1, allocator.allocs)
bytesBuf.AllocBytes(512)
require.Equal(t, 1, allocator.allocs)
bytesBuf.AllocBytes(257)
require.Equal(t, 2, allocator.allocs)
bytesBuf.AllocBytes(767)
require.Equal(t, 2, allocator.allocs)
largeBytes := bytesBuf.AllocBytes(513)
require.Equal(t, 513, len(largeBytes))
require.Equal(t, 2, allocator.allocs)
require.Equal(t, 0, allocator.frees)
bytesBuf.Destroy()
require.Equal(t, 0, allocator.frees)
bytesBuf = pool.NewBuffer()
for i := 0; i < 6; i++ {
bytesBuf.AllocBytes(512)
}
bytesBuf.Destroy()
require.Equal(t, 3, allocator.allocs)
require.Equal(t, 1, allocator.frees)
}
func TestBufferIsolation(t *testing.T) {
pool := NewPool(WithBlockSize(1024))
defer pool.Destroy()
bytesBuf := pool.NewBuffer()
defer bytesBuf.Destroy()
b1 := bytesBuf.AllocBytes(16)
b2 := bytesBuf.AllocBytes(16)
require.Len(t, b1, cap(b1))
require.Len(t, b2, cap(b2))
_, err := rand.Read(b2)
require.NoError(t, err)
b3 := append([]byte(nil), b2...)
b1 = append(b1, 0, 1, 2, 3)
require.Equal(t, b3, b2)
require.NotEqual(t, b2, b1)
}
func TestBufferMemLimit(t *testing.T) {
pool := NewPool(WithBlockSize(10))
defer pool.Destroy()
// the actual memory limit is 10 bytes.
bytesBuf := pool.NewBuffer(WithMemoryLimit(5))
got, _ := bytesBuf.AllocBytesWithSliceLocation(9)
require.NotNil(t, got)
got, _ = bytesBuf.AllocBytesWithSliceLocation(3)
require.Nil(t, got)
bytesBuf.Destroy()
// exactly 2 block
bytesBuf = pool.NewBuffer(WithMemoryLimit(20))
got, _ = bytesBuf.AllocBytesWithSliceLocation(9)
require.NotNil(t, got)
got, _ = bytesBuf.AllocBytesWithSliceLocation(9)
require.NotNil(t, got)
got, _ = bytesBuf.AllocBytesWithSliceLocation(2)
require.Nil(t, got)
}
const dataNum = 100 * 1024 * 1024
func BenchmarkStoreSlice(b *testing.B) {
data := make([][]byte, dataNum)
for i := 0; i < b.N; i++ {
func() {
pool := NewPool()
defer pool.Destroy()
bytesBuf := pool.NewBuffer()
defer bytesBuf.Destroy()
for j := range data {
data[j] = bytesBuf.AllocBytes(10)
}
}()
}
}
func BenchmarkStoreLocation(b *testing.B) {
data := make([]SliceLocation, dataNum)
for i := 0; i < b.N; i++ {
func() {
pool := NewPool()
defer pool.Destroy()
bytesBuf := pool.NewBuffer()
defer bytesBuf.Destroy()
for j := range data {
_, data[j] = bytesBuf.AllocBytesWithSliceLocation(10)
}
}()
}
}
const sortDataNum = 1024 * 1024
func BenchmarkSortSlice(b *testing.B) {
data := make([][]byte, sortDataNum)
// fixed seed for benchmark
rnd := rand2.New(rand2.NewSource(6716))
for i := 0; i < b.N; i++ {
func() {
pool := NewPool()
defer pool.Destroy()
bytesBuf := pool.NewBuffer()
defer bytesBuf.Destroy()
for j := range data {
data[j] = bytesBuf.AllocBytes(10)
rnd.Read(data[j])
}
slices.SortFunc(data, func(a, b []byte) int {
return bytes.Compare(a, b)
})
}()
}
}
func BenchmarkSortLocation(b *testing.B) {
data := make([]SliceLocation, sortDataNum)
// fixed seed for benchmark
rnd := rand2.New(rand2.NewSource(6716))
for i := 0; i < b.N; i++ {
func() {
pool := NewPool()
defer pool.Destroy()
bytesBuf := pool.NewBuffer()
defer bytesBuf.Destroy()
for j := range data {
var buf []byte
buf, data[j] = bytesBuf.AllocBytesWithSliceLocation(10)
rnd.Read(buf)
}
slices.SortFunc(data, func(a, b SliceLocation) int {
return bytes.Compare(bytesBuf.GetSlice(a), bytesBuf.GetSlice(b))
})
}()
}
}
func BenchmarkSortSliceWithGC(b *testing.B) {
data := make([][]byte, sortDataNum)
// fixed seed for benchmark
rnd := rand2.New(rand2.NewSource(6716))
for i := 0; i < b.N; i++ {
func() {
pool := NewPool()
defer pool.Destroy()
bytesBuf := pool.NewBuffer()
defer bytesBuf.Destroy()
for j := range data {
data[j] = bytesBuf.AllocBytes(10)
rnd.Read(data[j])
}
runtime.GC()
slices.SortFunc(data, func(a, b []byte) int {
return bytes.Compare(a, b)
})
}()
}
}
func BenchmarkSortLocationWithGC(b *testing.B) {
data := make([]SliceLocation, sortDataNum)
// fixed seed for benchmark
rnd := rand2.New(rand2.NewSource(6716))
for i := 0; i < b.N; i++ {
func() {
pool := NewPool()
defer pool.Destroy()
bytesBuf := pool.NewBuffer()
defer bytesBuf.Destroy()
for j := range data {
var buf []byte
buf, data[j] = bytesBuf.AllocBytesWithSliceLocation(10)
rnd.Read(buf)
}
runtime.GC()
slices.SortFunc(data, func(a, b SliceLocation) int {
return bytes.Compare(bytesBuf.GetSlice(a), bytesBuf.GetSlice(b))
})
}()
}
}