github.com/zeebo/mon@v0.0.0-20211012163247-13d39bdb54fa/floathist/varint_test.go (about)

     1  package floathist
     2  
     3  import (
     4  	"fmt"
     5  	"runtime"
     6  	"testing"
     7  
     8  	"github.com/zeebo/assert"
     9  	"github.com/zeebo/mon/internal/buffer"
    10  	"github.com/zeebo/pcg"
    11  )
    12  
    13  func TestVarint(t *testing.T) {
    14  	t.Run("Safe", func(t *testing.T) {
    15  		for i := uint(0); i <= 64; i++ {
    16  			buf := buffer.Of(make([]byte, 9))
    17  
    18  			nbytes := varintAppend(buf.Front9(), 1<<i-1)
    19  			buf = buf.Advance(nbytes)
    20  			dec, _, ok := safeVarintConsume(buf.Reset())
    21  
    22  			t.Logf("%-2d %064b %08b\n", i, dec, buf.Prefix())
    23  
    24  			assert.That(t, ok)
    25  			assert.Equal(t, uint64(1<<i-1), dec)
    26  		}
    27  	})
    28  
    29  	t.Run("Fast", func(t *testing.T) {
    30  		for i := uint(0); i <= 64; i++ {
    31  			buf := buffer.Of(make([]byte, 9))
    32  
    33  			nbytes := varintAppend(buf.Front9(), 1<<i-1)
    34  			buf = buf.Advance(nbytes)
    35  			_, dec := fastVarintConsume(buf.Reset().Front9())
    36  
    37  			t.Logf("%-2d %064b %08b\n", i, dec, buf.Prefix())
    38  
    39  			assert.Equal(t, uint64(1<<i-1), dec)
    40  		}
    41  	})
    42  
    43  	t.Run("FastDirty", func(t *testing.T) {
    44  		for i := uint(0); i <= 64; i++ {
    45  			buf := buffer.Of(make([]byte, 9))
    46  
    47  			nbytes := varintAppend(buf.Front9(), 1<<i-1)
    48  			for i := nbytes; i < 9; i++ {
    49  				*buf.Index(uintptr(i)) = uint8(pcg.Uint32())
    50  			}
    51  
    52  			buf = buf.Advance(nbytes)
    53  			_, dec := fastVarintConsume(buf.Reset().Front9())
    54  
    55  			t.Logf("%-2d %064b %08b\n", i, dec, buf.Prefix())
    56  
    57  			assert.Equal(t, uint64(1<<i-1), dec)
    58  		}
    59  	})
    60  
    61  	t.Run("RandomSafe", func(t *testing.T) {
    62  		for nb := 1; nb <= 9; nb++ {
    63  			mask := uint64(1)<<(7*nb) - 1
    64  			if nb == 9 {
    65  				mask = 1<<64 - 1
    66  			}
    67  
    68  			for i := 0; i < 10; i++ {
    69  				exp := pcg.Uint64() & mask
    70  				buf := buffer.Of(make([]byte, 9))
    71  
    72  				nbytes := varintAppend(buf.Front9(), exp)
    73  				buf = buf.Advance(nbytes)
    74  				dec, _, ok := safeVarintConsume(buf.Reset())
    75  
    76  				t.Logf("%-2d %064b %08b\n", i, dec, buf.Prefix())
    77  
    78  				assert.That(t, ok)
    79  				assert.Equal(t, exp, dec)
    80  			}
    81  		}
    82  	})
    83  
    84  	t.Run("RandomFast", func(t *testing.T) {
    85  		for nb := 1; nb <= 9; nb++ {
    86  			mask := uint64(1)<<(7*nb) - 1
    87  			if nb == 9 {
    88  				mask = 1<<64 - 1
    89  			}
    90  
    91  			for i := 0; i < 10; i++ {
    92  				exp := pcg.Uint64() & mask
    93  				buf := buffer.Of(make([]byte, 9))
    94  
    95  				nbytes := varintAppend(buf.Front9(), exp)
    96  				buf = buf.Advance(nbytes)
    97  				_, dec := fastVarintConsume(buf.Reset().Front9())
    98  
    99  				t.Logf("%-2d %064b %08b\n", i, dec, buf.Prefix())
   100  
   101  				assert.Equal(t, exp, dec)
   102  			}
   103  		}
   104  	})
   105  }
   106  
   107  func BenchmarkVarint(b *testing.B) {
   108  	randVals := make([]uint64, 1024*1024)
   109  	for i := range randVals {
   110  		randVals[i] = uint64(1<<pcg.Uint32n(65) - 1)
   111  	}
   112  	randBuf := buffer.Of(make([]byte, 16))
   113  	for _, val := range randVals {
   114  		randBuf = randBuf.Grow()
   115  		nbytes := varintAppend(randBuf.Front9(), val)
   116  		randBuf = randBuf.Advance(nbytes)
   117  	}
   118  	randBuf = randBuf.Reset()
   119  
   120  	b.Run("Append", func(b *testing.B) {
   121  		for _, i := range []uint{1, 64} {
   122  			b.Run(fmt.Sprint(i), func(b *testing.B) {
   123  				n := uint64(1<<i - 1)
   124  				buf := buffer.Of(make([]byte, 16))
   125  
   126  				for i := 0; i < b.N; i++ {
   127  					buf = buf.Grow()
   128  					varintAppend(buf.Front9(), n)
   129  				}
   130  			})
   131  		}
   132  
   133  		b.Run("Rand", func(b *testing.B) {
   134  			buf := buffer.Of(make([]byte, 16))
   135  
   136  			for i := 0; i < b.N; i++ {
   137  				buf = buf.Grow()
   138  				varintAppend(buf.Front9(), randVals[i%(1024*1024)])
   139  			}
   140  		})
   141  	})
   142  
   143  	b.Run("Consume", func(b *testing.B) {
   144  		for _, i := range []uint{1, 64} {
   145  			b.Run(fmt.Sprint(i), func(b *testing.B) {
   146  				n := uint64(1<<i - 1)
   147  				buf := buffer.Of(make([]byte, 9))
   148  				nbytes := varintAppend(buf.Front9(), n)
   149  				buf = buf.Advance(nbytes)
   150  
   151  				for i := 0; i < b.N; i++ {
   152  					safeVarintConsume(buf)
   153  				}
   154  			})
   155  		}
   156  
   157  		b.Run("Rand", func(b *testing.B) {
   158  			buf := randBuf.Reset()
   159  			for i := 0; i < b.N; i++ {
   160  				if buf.Remaining() == 0 {
   161  					buf = buf.Reset()
   162  				}
   163  				_, buf, _ = safeVarintConsume(buf)
   164  			}
   165  		})
   166  	})
   167  
   168  	b.Run("FastConsume", func(b *testing.B) {
   169  		for _, i := range []uint{1, 64} {
   170  			b.Run(fmt.Sprint(i), func(b *testing.B) {
   171  				n := uint64(1<<i - 1)
   172  				buf := buffer.Of(make([]byte, 9))
   173  				nbytes := varintAppend(buf.Front9(), n)
   174  				buf = buf.Advance(nbytes)
   175  
   176  				var dec uint64
   177  				for i := 0; i < b.N; i++ {
   178  					_, dec = fastVarintConsume(buf.Front9())
   179  				}
   180  				runtime.KeepAlive(dec)
   181  			})
   182  		}
   183  
   184  		b.Run("Rand", func(b *testing.B) {
   185  			var nbytes uintptr
   186  			var dec uint64
   187  
   188  			buf := randBuf.Reset()
   189  			for i := 0; i < b.N; i++ {
   190  				if buf.Remaining() < 9 {
   191  					buf = buf.Reset()
   192  				}
   193  				nbytes, dec = fastVarintConsume(buf.Front9())
   194  				buf = buf.Advance(nbytes)
   195  			}
   196  
   197  			runtime.KeepAlive(nbytes)
   198  			runtime.KeepAlive(dec)
   199  		})
   200  	})
   201  }