github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/workload/workloadimpl/precomputedrand_test.go (about) 1 // Copyright 2019 The Cockroach Authors. 2 // 3 // Use of this software is governed by the Business Source License 4 // included in the file licenses/BSL.txt. 5 // 6 // As of the Change Date specified in that file, in accordance with 7 // the Business Source License, use of this software will be governed 8 // by the Apache License, Version 2.0, included in the file 9 // licenses/APL.txt. 10 11 package workloadimpl_test 12 13 import ( 14 "fmt" 15 "testing" 16 17 "github.com/cockroachdb/cockroach/pkg/util/timeutil" 18 "github.com/cockroachdb/cockroach/pkg/workload/workloadimpl" 19 "github.com/stretchr/testify/require" 20 "golang.org/x/exp/rand" 21 ) 22 23 const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 24 25 func TestPrecomputedRand(t *testing.T) { 26 const precomputeLen = 100 27 28 seed0 := workloadimpl.PrecomputedRandInit(rand.NewSource(0), precomputeLen, alphabet)() 29 seed1 := workloadimpl.PrecomputedRandInit(rand.NewSource(1), precomputeLen, alphabet)() 30 numbers := workloadimpl.PrecomputedRandInit(rand.NewSource(0), precomputeLen, `0123456789`)() 31 32 const shorterThanPrecomputed, longerThanPrecomputed = precomputeLen / 10, precomputeLen + 7 33 34 offset := 0 35 fillBytes := func(pr workloadimpl.PrecomputedRand, length int) []byte { 36 buf := make([]byte, length) 37 offset = pr.FillBytes(offset, buf) 38 return buf 39 } 40 41 short0 := fillBytes(seed0, shorterThanPrecomputed) 42 long0 := fillBytes(seed0, longerThanPrecomputed) 43 44 // Offset has advanced, we should get a different result. 45 short0Different := fillBytes(seed0, shorterThanPrecomputed) 46 require.NotEqual(t, short0, short0Different) 47 48 // Reset the offset and verify that the results are repeatable 49 offset = 0 50 short0B := fillBytes(seed0, shorterThanPrecomputed) 51 long0B := fillBytes(seed0, longerThanPrecomputed) 52 require.Equal(t, short0, short0B) 53 require.Equal(t, long0, long0B) 54 55 // Reset the offset and verify that a different seed gets different results. 56 offset = 0 57 short1 := fillBytes(seed1, shorterThanPrecomputed) 58 long1 := fillBytes(seed1, longerThanPrecomputed) 59 require.NotEqual(t, short0, short1) 60 require.NotEqual(t, long0, long1) 61 62 // Reset the offset and verify that a different alphabet gets different 63 // results. 64 offset = 0 65 shortNumbers := fillBytes(numbers, shorterThanPrecomputed) 66 longNumbers := fillBytes(numbers, longerThanPrecomputed) 67 require.NotEqual(t, short0, shortNumbers) 68 require.NotEqual(t, long0, longNumbers) 69 } 70 71 func BenchmarkPrecomputedRand(b *testing.B) { 72 const precomputeLen = 10000 73 pr := workloadimpl.PrecomputedRandInit( 74 rand.NewSource(uint64(timeutil.Now().UnixNano())), precomputeLen, alphabet)() 75 76 const shortLen, mediumLen, longLen = 2, 100, 100000 77 scratch := make([]byte, longLen) 78 var randOffset int 79 80 for _, l := range []int{shortLen, mediumLen, longLen} { 81 b.Run(fmt.Sprintf(`len=%d`, l), func(b *testing.B) { 82 randOffset = 0 83 buf := scratch[:l] 84 for i := 0; i < b.N; i++ { 85 randOffset = pr.FillBytes(randOffset, buf) 86 } 87 b.SetBytes(int64(len(buf))) 88 }) 89 } 90 }