github.com/cockroachdb/pebble@v1.1.1-0.20240513155919-3622ade60459/internal/randvar/skewed_latest_test.go (about)

     1  // Copyright 2018 The Cockroach Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
    12  // implied. See the License for the specific language governing
    13  // permissions and limitations under the License. See the AUTHORS file
    14  // for names of contributors.
    15  
    16  package randvar
    17  
    18  import (
    19  	"fmt"
    20  	"sort"
    21  	"testing"
    22  
    23  	"github.com/stretchr/testify/require"
    24  )
    25  
    26  func dumpSamples(x []int) {
    27  	sort.Ints(x)
    28  
    29  	max := x[len(x)-1]
    30  	step := max / 20
    31  	if step == 0 {
    32  		step = 1
    33  	}
    34  	index := 0
    35  	count := 0
    36  	for i := 0; i <= max; i += step {
    37  		count = 0
    38  		for index < len(x) {
    39  			if x[index] >= i+step {
    40  				break
    41  			}
    42  			index++
    43  			count++
    44  		}
    45  		fmt.Printf("[%3d-%3d) ", i, i+step)
    46  		for j := 0; j < count; j++ {
    47  			if j%50 == 0 {
    48  				fmt.Printf("%c", '∎')
    49  			}
    50  		}
    51  		fmt.Println()
    52  	}
    53  }
    54  
    55  func TestSkewedLatest(t *testing.T) {
    56  	rng := NewRand()
    57  	z, err := NewSkewedLatest(0, 99, 0.99)
    58  	require.NoError(t, err)
    59  
    60  	x := make([]int, 10000)
    61  	for i := range x {
    62  		x[i] = int(z.Uint64(rng))
    63  	}
    64  
    65  	if testing.Verbose() {
    66  		dumpSamples(x)
    67  	}
    68  }