github.com/nyan233/littlerpc@v0.4.6-0.20230316182519-0c8d5c48abaf/core/container/slice_test.go (about)

     1  package container
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/nyan233/littlerpc/core/common/inters"
     6  	"github.com/nyan233/littlerpc/core/utils/random"
     7  	"github.com/stretchr/testify/assert"
     8  	"math"
     9  	"net"
    10  	"testing"
    11  )
    12  
    13  var (
    14  	_ SliceTester = new(Slice[byte])
    15  	_ SliceTester = new(ByteSlice)
    16  )
    17  
    18  type SliceTester interface {
    19  	Len() int
    20  	Cap() int
    21  	Unique()
    22  	AppendSingle(v byte)
    23  	Append(v []byte)
    24  	AppendS(vs ...byte)
    25  	Available() bool
    26  	inters.Reset
    27  }
    28  
    29  func TestSliceGeneric(t *testing.T) {
    30  	var gs Slice[byte] = genSeqNumOnByte(1000)
    31  	var rs ByteSlice = genSeqNumOnByte(1000)
    32  	testSlice(t, &gs)
    33  	testSlice(t, &rs)
    34  }
    35  
    36  func testSlice(t *testing.T, inter SliceTester) {
    37  	inter.AppendS(100)
    38  	inter.AppendSingle(101)
    39  	inter.Append(genSeqNumOnByte(10))
    40  	inter.Unique()
    41  	assert.LessOrEqual(t, inter.Len(), math.MaxUint8)
    42  	assert.True(t, inter.Available())
    43  	inter.Reset()
    44  	assert.False(t, inter.Available())
    45  }
    46  
    47  func TestConcurrentArray(t *testing.T) {
    48  	a := NewConcurrentArray[net.Conn](1024)
    49  	var conn net.Conn = new(net.TCPConn)
    50  	a.Swap(100, &conn)
    51  	assert.NotNil(t, a.Access(100))
    52  }
    53  
    54  func BenchmarkConcurrentArray(b *testing.B) {
    55  	a := NewConcurrentArray[net.Conn](1024)
    56  	b.Run("Access", func(b *testing.B) {
    57  		b.ReportAllocs()
    58  		b.SetParallelism(1024)
    59  		b.RunParallel(func(pb *testing.PB) {
    60  			for pb.Next() {
    61  				a.Access(500)
    62  			}
    63  		})
    64  	})
    65  	b.Run("Swap", func(b *testing.B) {
    66  		b.ReportAllocs()
    67  		b.SetParallelism(1024)
    68  		b.RunParallel(func(pb *testing.PB) {
    69  			for pb.Next() {
    70  				a.Swap(200, nil)
    71  			}
    72  		})
    73  	})
    74  	b.Run("AccessAndSwap", func(b *testing.B) {
    75  		b.ReportAllocs()
    76  		b.SetParallelism(1024)
    77  		b.RunParallel(func(pb *testing.PB) {
    78  			for pb.Next() {
    79  				a.Swap(500, nil)
    80  				a.Access(500)
    81  			}
    82  		})
    83  	})
    84  }
    85  
    86  func genSeqNumOnByte(n int) []byte {
    87  	seq := random.GenSequenceNumberOnMathRand(n)
    88  	seqByte := make([]byte, n)
    89  	for k, v := range seq {
    90  		seqByte[k] = byte(v)
    91  	}
    92  	return seqByte
    93  }
    94  
    95  func BenchmarkSliceUnique(b *testing.B) {
    96  	b.ReportAllocs()
    97  	for i := 0; i < b.N; i++ {
    98  		testData := []int{40, 55, 2746, 30, 55, 40, 66, 77, 44, 77}
    99  		s := Slice[int]{}
   100  		s = testData
   101  		s.Unique()
   102  	}
   103  }
   104  
   105  func BenchmarkSliceGeneric(b *testing.B) {
   106  	b.ReportAllocs()
   107  	var g Slice[byte] = make([]byte, 1<<20)
   108  	sliceBench(b, "Generic", &g)
   109  	var bs ByteSlice = make([]byte, 1<<20)
   110  	sliceBench(b, "RawType", &bs)
   111  }
   112  
   113  func sliceBench(b *testing.B, name string, inter SliceTester) {
   114  	b.Run(fmt.Sprintf("%s-Length", name), func(b *testing.B) {
   115  		for i := 0; i < b.N; i++ {
   116  			inter.Len()
   117  		}
   118  	})
   119  	b.Run(fmt.Sprintf("%s-Cap", name), func(b *testing.B) {
   120  		for i := 0; i < b.N; i++ {
   121  			inter.Cap()
   122  		}
   123  	})
   124  	b.Run(fmt.Sprintf("%s-Unique", name), func(b *testing.B) {
   125  		for i := 0; i < b.N; i++ {
   126  			inter.Unique()
   127  		}
   128  	})
   129  	b.Run(fmt.Sprintf("%s-Reset", name), func(b *testing.B) {
   130  		for i := 0; i < b.N; i++ {
   131  			inter.Reset()
   132  		}
   133  	})
   134  	b.Run(fmt.Sprintf("%s-Append-Slice", name), func(b *testing.B) {
   135  		for i := 0; i < b.N; i++ {
   136  			inter.AppendS('h', 'e', 'l', 'l', 'o')
   137  		}
   138  	})
   139  	b.Run(fmt.Sprintf("%s-Append", name), func(b *testing.B) {
   140  		for i := 0; i < b.N; i++ {
   141  			inter.Append([]byte("hello"))
   142  		}
   143  	})
   144  	b.Run(fmt.Sprintf("%s-Append-Single", name), func(b *testing.B) {
   145  		for i := 0; i < b.N; i++ {
   146  			inter.AppendSingle('h')
   147  		}
   148  	})
   149  }
   150  
   151  func BenchmarkSlice(b *testing.B) {
   152  	b.Run("Append", func(b *testing.B) {
   153  		b.ReportAllocs()
   154  		buf1 := make([]byte, 0, 4096)
   155  		buf2 := make([]byte, 4096)
   156  		for i := 0; i < b.N; i++ {
   157  			buf1 = append(buf1, buf2...)
   158  			buf1 = buf1[:0]
   159  		}
   160  	})
   161  	b.Run("Copy", func(b *testing.B) {
   162  		b.ReportAllocs()
   163  		buf1 := make([]byte, 0, 4096)
   164  		buf2 := make([]byte, 4096)
   165  		for i := 0; i < b.N; i++ {
   166  			buf1 = buf1[:len(buf2)]
   167  			copy(buf1, buf2)
   168  		}
   169  	})
   170  	b.Run("AppendOnGeneric", func(b *testing.B) {
   171  		b.ReportAllocs()
   172  		var buf1 Slice[byte] = make([]byte, 0, 4096)
   173  		buf2 := make([]byte, 4096)
   174  		for i := 0; i < b.N; i++ {
   175  			buf1.Append(buf2)
   176  			buf1.Reset()
   177  		}
   178  	})
   179  }