github.com/cockroachdb/pebble@v1.1.2/sstable/unsafe_test.go (about) 1 // Copyright 2018 The LevelDB-Go and Pebble Authors. All rights reserved. Use 2 // of this source code is governed by a BSD-style license that can be found in 3 // the LICENSE file. 4 5 package sstable 6 7 import ( 8 "encoding/binary" 9 "fmt" 10 "io" 11 "testing" 12 "time" 13 "unsafe" 14 15 "github.com/stretchr/testify/require" 16 "golang.org/x/exp/rand" 17 ) 18 19 func TestGetBytes(t *testing.T) { 20 const size = (1 << 31) - 1 21 // No need to actually allocate a huge slice, which can cause OOM on small 22 // machines (like the GitHub CI runners). 23 block := make([]byte, 100) 24 data := getBytes(unsafe.Pointer(&block[0]), size) 25 require.EqualValues(t, size, len(data)) 26 } 27 28 func TestDecodeVarint(t *testing.T) { 29 vals := []uint32{ 30 0, 31 1, 32 1 << 7, 33 1 << 8, 34 1 << 14, 35 1 << 15, 36 1 << 20, 37 1 << 21, 38 1 << 28, 39 1 << 29, 40 1 << 31, 41 } 42 buf := make([]byte, 5) 43 for _, v := range vals { 44 binary.PutUvarint(buf, uint64(v)) 45 u, _ := decodeVarint(unsafe.Pointer(&buf[0])) 46 if v != u { 47 fmt.Printf("%d %d\n", v, u) 48 } 49 } 50 } 51 52 func BenchmarkDecodeVarint(b *testing.B) { 53 rng := rand.New(rand.NewSource(uint64(time.Now().UnixNano()))) 54 vals := make([]unsafe.Pointer, 10000) 55 for i := range vals { 56 buf := make([]byte, 5) 57 binary.PutUvarint(buf, uint64(rng.Uint32())) 58 vals[i] = unsafe.Pointer(&buf[0]) 59 } 60 61 b.ResetTimer() 62 var ptr unsafe.Pointer 63 for i, n := 0, 0; i < b.N; i += n { 64 n = len(vals) 65 if n > b.N-i { 66 n = b.N - i 67 } 68 for j := 0; j < n; j++ { 69 _, ptr = decodeVarint(vals[j]) 70 } 71 } 72 if testing.Verbose() { 73 fmt.Fprint(io.Discard, ptr) 74 } 75 }