github.com/jxskiss/gopkg/v2@v2.14.9-0.20240514120614-899f3e7952b4/perf/bbp/sized_test.go (about)

     1  package bbp
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  )
     8  
     9  const (
    10  	_4K  = 4096
    11  	_10M = 10 << 20
    12  )
    13  
    14  func TestGet(t *testing.T) {
    15  	buf := Get(_4K, _4K)
    16  	t.Log(cap(buf))
    17  
    18  	buf = Get(_10M, _10M)
    19  	t.Log(cap(buf))
    20  }
    21  
    22  func Test_indexGet(t *testing.T) {
    23  	assert.Equal(t, 6, indexGet(63))
    24  	assert.Equal(t, 6, indexGet(64))
    25  	assert.Equal(t, 7, indexGet(65))
    26  	assert.Equal(t, 7, indexGet(127))
    27  	assert.Equal(t, 7, indexGet(128))
    28  	assert.Equal(t, 8, indexGet(129))
    29  }
    30  
    31  func Test_indexPut(t *testing.T) {
    32  	assert.Equal(t, 5, indexPut(63))
    33  	assert.Equal(t, 6, indexPut(64))
    34  	assert.Equal(t, 6, indexPut(65))
    35  	assert.Equal(t, 6, indexPut(127))
    36  	assert.Equal(t, 7, indexPut(128))
    37  	assert.Equal(t, 7, indexPut(129))
    38  	assert.Equal(t, 7, indexPut(255))
    39  	assert.Equal(t, 8, indexPut(256))
    40  }
    41  
    42  func BenchmarkAlloc_4K(b *testing.B) {
    43  	var buf []byte
    44  	b.ReportAllocs()
    45  	for i := 0; i < b.N; i++ {
    46  		buf = make([]byte, 0, _4K)
    47  	}
    48  	_ = buf
    49  }
    50  
    51  func BenchmarkPool_4K(b *testing.B) {
    52  	var buf []byte
    53  	b.ReportAllocs()
    54  	for i := 0; i < b.N; i++ {
    55  		buf = Get(_4K, _4K)
    56  		Put(buf)
    57  	}
    58  	_ = buf
    59  }
    60  
    61  func BenchmarkAlloc_10M(b *testing.B) {
    62  	var buf []byte
    63  	b.ReportAllocs()
    64  	for i := 0; i < b.N; i++ {
    65  		buf = make([]byte, 0, _10M)
    66  	}
    67  	_ = buf
    68  }
    69  
    70  func BenchmarkPool_10M(b *testing.B) {
    71  	var buf []byte
    72  	b.ReportAllocs()
    73  	for i := 0; i < b.N; i++ {
    74  		buf = Get(_10M, _10M)
    75  		Put(buf)
    76  	}
    77  	_ = buf
    78  }
    79  
    80  func BenchmarkAlloc_4K_Parallel(b *testing.B) {
    81  	b.ReportAllocs()
    82  	b.RunParallel(func(pb *testing.PB) {
    83  		var buf []byte
    84  		for pb.Next() {
    85  			for i := 0; i < 100; i++ {
    86  				buf = make([]byte, 0, _4K)
    87  			}
    88  		}
    89  		_ = buf
    90  	})
    91  }
    92  
    93  func BenchmarkPool_4K_Parallel(b *testing.B) {
    94  	b.ReportAllocs()
    95  	b.RunParallel(func(pb *testing.PB) {
    96  		var buf []byte
    97  		for pb.Next() {
    98  			for i := 0; i < 100; i++ {
    99  				buf = Get(_4K, _4K)
   100  				Put(buf)
   101  			}
   102  		}
   103  		_ = buf
   104  	})
   105  }
   106  
   107  func BenchmarkAlloc_10M_Parallel(b *testing.B) {
   108  	b.ReportAllocs()
   109  	b.RunParallel(func(pb *testing.PB) {
   110  		var buf []byte
   111  		for pb.Next() {
   112  			for i := 0; i < 100; i++ {
   113  				buf = make([]byte, 0, _10M)
   114  			}
   115  		}
   116  		_ = buf
   117  	})
   118  }
   119  
   120  func BenchmarkPool_10M_Parallel(b *testing.B) {
   121  	b.ReportAllocs()
   122  	b.RunParallel(func(pb *testing.PB) {
   123  		var buf []byte
   124  		for pb.Next() {
   125  			for i := 0; i < 100; i++ {
   126  				buf = Get(_10M, _10M)
   127  				Put(buf)
   128  			}
   129  		}
   130  		_ = buf
   131  	})
   132  }