github.com/influx6/npkg@v0.8.8/nbytes/pbytes/pbytes_test.go (about)

     1  package pbytes_test
     2  
     3  import (
     4  	"math/rand"
     5  	"testing"
     6  
     7  	"github.com/influx6/npkg/nbytes/pbytes"
     8  )
     9  
    10  func BenchmarkBitsBoot(b *testing.B) {
    11  	b.ReportAllocs()
    12  	b.StopTimer()
    13  
    14  	provider := pbytes.NewBitsBoot(128, 10)
    15  	benchmarkPool(b, provider)
    16  }
    17  
    18  func BenchmarkBitsPool(b *testing.B) {
    19  	b.ReportAllocs()
    20  	b.StopTimer()
    21  
    22  	provider := pbytes.NewBitsPool(128, 10)
    23  	benchmarkPool(b, provider)
    24  }
    25  
    26  type provider interface {
    27  	Get(int) *pbytes.Buffer
    28  }
    29  
    30  func benchmarkPool(b *testing.B, handler provider) {
    31  	b.Run("2bytes", func(b *testing.B) {
    32  		b.ReportAllocs()
    33  		b.StopTimer()
    34  
    35  		b.StartTimer()
    36  		b.RunParallel(func(pb *testing.PB) {
    37  			for pb.Next() {
    38  				buff := handler.Get(2)
    39  				buff.Discard()
    40  			}
    41  		})
    42  		b.StopTimer()
    43  	})
    44  
    45  	b.Run("4bytes", func(b *testing.B) {
    46  		b.ReportAllocs()
    47  		b.StopTimer()
    48  		b.StartTimer()
    49  		b.RunParallel(func(pb *testing.PB) {
    50  			for pb.Next() {
    51  				buff := handler.Get(4)
    52  				buff.Discard()
    53  			}
    54  		})
    55  		b.StopTimer()
    56  	})
    57  
    58  	b.Run("16bytes", func(b *testing.B) {
    59  		b.ReportAllocs()
    60  		b.StopTimer()
    61  		b.StartTimer()
    62  		b.RunParallel(func(pb *testing.PB) {
    63  			for pb.Next() {
    64  				buff := handler.Get(16)
    65  				buff.Discard()
    66  			}
    67  		})
    68  		b.StopTimer()
    69  	})
    70  
    71  	b.Run("32bytes", func(b *testing.B) {
    72  		b.ReportAllocs()
    73  		b.StopTimer()
    74  		b.StartTimer()
    75  		b.RunParallel(func(pb *testing.PB) {
    76  			for pb.Next() {
    77  				buff := handler.Get(32)
    78  				buff.Discard()
    79  			}
    80  		})
    81  		b.StopTimer()
    82  	})
    83  
    84  	b.Run("64bytes", func(b *testing.B) {
    85  		b.ReportAllocs()
    86  		b.StopTimer()
    87  		b.StartTimer()
    88  		b.RunParallel(func(pb *testing.PB) {
    89  			for pb.Next() {
    90  				buff := handler.Get(64)
    91  				buff.Discard()
    92  			}
    93  		})
    94  		b.StopTimer()
    95  	})
    96  
    97  	b.Run("128bytes", func(b *testing.B) {
    98  		b.ReportAllocs()
    99  		b.StopTimer()
   100  		b.StartTimer()
   101  		for i := 0; i < b.N; i++ {
   102  			buff := handler.Get(128)
   103  			buff.Discard()
   104  		}
   105  		b.RunParallel(func(pb *testing.PB) {
   106  			for pb.Next() {
   107  				buff := handler.Get(128)
   108  				buff.Discard()
   109  			}
   110  		})
   111  		b.StopTimer()
   112  	})
   113  
   114  	b.Run("512bytes", func(b *testing.B) {
   115  		b.ReportAllocs()
   116  		b.StopTimer()
   117  		b.StartTimer()
   118  		b.RunParallel(func(pb *testing.PB) {
   119  			for pb.Next() {
   120  				buff := handler.Get(512)
   121  				buff.Discard()
   122  			}
   123  		})
   124  		b.StopTimer()
   125  	})
   126  
   127  	b.Run("1024bytes", func(b *testing.B) {
   128  		b.ReportAllocs()
   129  		b.StopTimer()
   130  		b.StartTimer()
   131  		b.RunParallel(func(pb *testing.PB) {
   132  			for pb.Next() {
   133  				buff := handler.Get(1024)
   134  				buff.Discard()
   135  			}
   136  		})
   137  		b.StopTimer()
   138  	})
   139  
   140  	b.Run("4024bytes", func(b *testing.B) {
   141  		b.ReportAllocs()
   142  		b.StopTimer()
   143  		b.StartTimer()
   144  
   145  		b.RunParallel(func(pb *testing.PB) {
   146  			for pb.Next() {
   147  				buff := handler.Get(1024 * 4)
   148  				buff.Discard()
   149  			}
   150  		})
   151  		b.StopTimer()
   152  	})
   153  
   154  	b.Run("8024bytes", func(b *testing.B) {
   155  		b.ReportAllocs()
   156  		b.StopTimer()
   157  		b.StartTimer()
   158  
   159  		b.RunParallel(func(pb *testing.PB) {
   160  			for pb.Next() {
   161  				buff := handler.Get(1024 * 8)
   162  				buff.Discard()
   163  			}
   164  		})
   165  		b.StopTimer()
   166  	})
   167  
   168  	b.Run("16024bytes", func(b *testing.B) {
   169  		b.ReportAllocs()
   170  		b.StopTimer()
   171  		b.StartTimer()
   172  		b.RunParallel(func(pb *testing.PB) {
   173  			for pb.Next() {
   174  				buff := handler.Get(1024 * 16)
   175  				buff.Discard()
   176  			}
   177  		})
   178  		b.StopTimer()
   179  	})
   180  }
   181  
   182  var pub = "PUB "
   183  var empty = []byte("")
   184  var ch = []byte("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@$#%^&*()")
   185  
   186  func sizedPayload(sz int) []byte {
   187  	payload := make([]byte, len(pub)+sz)
   188  	nx := copy(payload, pub)
   189  	copy(payload[nx:], sizedBytes(sz))
   190  	return payload
   191  }
   192  
   193  func sizedBytes(sz int) []byte {
   194  	if sz <= 0 {
   195  		return empty
   196  	}
   197  
   198  	b := make([]byte, sz)
   199  	for i := range b {
   200  		b[i] = ch[rand.Intn(len(ch))]
   201  	}
   202  	return b
   203  }