github.com/userpro/linearpool@v0.5.3-0.20231115092206-0ca073169b71/bench_test.go (about)

     1  package memorypool
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  type allocTest1 struct {
     8  	A string
     9  	B []int32
    10  	C *allocTest2
    11  }
    12  
    13  type allocTest2 struct {
    14  	D string
    15  }
    16  
    17  const objnum = 1000
    18  
    19  // BenchmarkStructRawAlloc ...
    20  func BenchmarkStructRawAlloc(b *testing.B) {
    21  	b.ResetTimer()
    22  	for i := 0; i < b.N; i++ {
    23  		d1 := make([]*allocTest1, 0, 1)
    24  		for i := 0; i < objnum; i++ {
    25  			d1 = append(d1, &allocTest1{
    26  				A: "123123123123",
    27  				B: append([]int32{}, []int32{1, 2, 3, 4, 5, 6, 7}...),
    28  				C: &allocTest2{
    29  					D: "123123123123",
    30  				},
    31  			})
    32  		}
    33  	}
    34  }
    35  
    36  // BenchmarkStructPoolAlloc ...
    37  func BenchmarkStructPoolAlloc(b *testing.B) {
    38  	ac := NewAlloctorFromPool(DiMB)
    39  
    40  	b.ResetTimer()
    41  	for i := 0; i < b.N; i++ {
    42  		d1 := NewSlice[*allocTest1](ac, 0, 1)
    43  		for i := 0; i < objnum; i++ {
    44  			d := New[allocTest1](ac)
    45  			d.A = ac.NewString("123123123123")
    46  			d.B = NewSlice[int32](ac, 0, 7)
    47  			d.B = AppendMulti[int32](ac, d.B, []int32{1, 2, 3, 4, 5, 6, 7}...)
    48  			d.C = New[allocTest2](ac)
    49  			d.C.D = ac.NewString("123123123123")
    50  			d1 = AppendMulti[*allocTest1](ac, d1, d)
    51  		}
    52  		ac.Reset()
    53  	}
    54  }
    55  
    56  // BenchmarkIntSliceRawAlloc ...
    57  func BenchmarkIntSliceRawAlloc(b *testing.B) {
    58  
    59  	b.ResetTimer()
    60  	for i := 0; i < b.N; i++ {
    61  		d1 := make([]int, 0, objnum)
    62  		for i := 0; i < objnum; i++ {
    63  			d1 = append(d1, i)
    64  		}
    65  	}
    66  }
    67  
    68  // BenchmarkIntSlicePoolAllocAppendInplaceMulti ...
    69  func BenchmarkIntSlicePoolAllocAppendInplaceMulti(b *testing.B) {
    70  	ac := NewAlloctorFromPool(DiMB)
    71  
    72  	b.ResetTimer()
    73  	for i := 0; i < b.N; i++ {
    74  		d1 := NewSlice[int](ac, 0, objnum)
    75  		for i := 0; i < objnum; i++ {
    76  			d1 = AppendInplaceMulti[int](ac, d1, i)
    77  		}
    78  		ac.Reset()
    79  	}
    80  }
    81  
    82  // BenchmarkIntSlicePoolAllocAppendInplace ...
    83  func BenchmarkIntSlicePoolAllocAppendInplace(b *testing.B) {
    84  	ac := NewAlloctorFromPool(DiMB)
    85  
    86  	b.ResetTimer()
    87  	for i := 0; i < b.N; i++ {
    88  		d1 := NewSlice[int](ac, 0, objnum)
    89  		for i := 0; i < objnum; i++ {
    90  			d1 = AppendInplace[int](ac, d1, i)
    91  		}
    92  		ac.Reset()
    93  	}
    94  }
    95  
    96  // BenchmarkIntSlicePoolAllocAppendInbound ...
    97  func BenchmarkIntSlicePoolAllocAppendInbound(b *testing.B) {
    98  	ac := NewAlloctorFromPool(DiMB)
    99  
   100  	b.ResetTimer()
   101  	for i := 0; i < b.N; i++ {
   102  		d1 := NewSlice[int](ac, 0, objnum)
   103  		for i := 0; i < objnum; i++ {
   104  			d1 = AppendInbound[int](ac, d1, i)
   105  		}
   106  		ac.Reset()
   107  	}
   108  }