github.com/bhojpur/cache@v0.0.4/pkg/engine/ristretto/sketch_test.go (about)

     1  package ristretto
     2  
     3  // Copyright (c) 2018 Bhojpur Consulting Private Limited, India. All rights reserved.
     4  
     5  // Permission is hereby granted, free of charge, to any person obtaining a copy
     6  // of this software and associated documentation files (the "Software"), to deal
     7  // in the Software without restriction, including without limitation the rights
     8  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     9  // copies of the Software, and to permit persons to whom the Software is
    10  // furnished to do so, subject to the following conditions:
    11  
    12  // The above copyright notice and this permission notice shall be included in
    13  // all copies or substantial portions of the Software.
    14  
    15  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    16  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    17  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    18  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    19  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    20  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    21  // THE SOFTWARE.
    22  
    23  import (
    24  	"testing"
    25  
    26  	"github.com/stretchr/testify/require"
    27  )
    28  
    29  func TestSketch(t *testing.T) {
    30  	defer func() {
    31  		require.NotNil(t, recover())
    32  	}()
    33  
    34  	s := newCmSketch(5)
    35  	require.Equal(t, uint64(7), s.mask)
    36  	newCmSketch(0)
    37  }
    38  
    39  func TestSketchIncrement(t *testing.T) {
    40  	s := newCmSketch(16)
    41  	s.Increment(1)
    42  	s.Increment(5)
    43  	s.Increment(9)
    44  	for i := 0; i < cmDepth; i++ {
    45  		if s.rows[i].string() != s.rows[0].string() {
    46  			break
    47  		}
    48  		require.False(t, i == cmDepth-1, "identical rows, bad seeding")
    49  	}
    50  }
    51  
    52  func TestSketchEstimate(t *testing.T) {
    53  	s := newCmSketch(16)
    54  	s.Increment(1)
    55  	s.Increment(1)
    56  	require.Equal(t, int64(2), s.Estimate(1))
    57  	require.Equal(t, int64(0), s.Estimate(0))
    58  }
    59  
    60  func TestSketchReset(t *testing.T) {
    61  	s := newCmSketch(16)
    62  	s.Increment(1)
    63  	s.Increment(1)
    64  	s.Increment(1)
    65  	s.Increment(1)
    66  	s.Reset()
    67  	require.Equal(t, int64(2), s.Estimate(1))
    68  }
    69  
    70  func TestSketchClear(t *testing.T) {
    71  	s := newCmSketch(16)
    72  	for i := 0; i < 16; i++ {
    73  		s.Increment(uint64(i))
    74  	}
    75  	s.Clear()
    76  	for i := 0; i < 16; i++ {
    77  		require.Equal(t, int64(0), s.Estimate(uint64(i)))
    78  	}
    79  }
    80  
    81  func TestNext2Power(t *testing.T) {
    82  	sz := 12 << 30
    83  	szf := float64(sz) * 0.01
    84  	val := int64(szf)
    85  	t.Logf("szf = %.2f val = %d\n", szf, val)
    86  	pow := next2Power(val)
    87  	t.Logf("pow = %d. mult 4 = %d\n", pow, pow*4)
    88  }
    89  
    90  func BenchmarkSketchIncrement(b *testing.B) {
    91  	s := newCmSketch(16)
    92  	b.SetBytes(1)
    93  	for n := 0; n < b.N; n++ {
    94  		s.Increment(1)
    95  	}
    96  }
    97  
    98  func BenchmarkSketchEstimate(b *testing.B) {
    99  	s := newCmSketch(16)
   100  	s.Increment(1)
   101  	b.SetBytes(1)
   102  	for n := 0; n < b.N; n++ {
   103  		s.Estimate(1)
   104  	}
   105  }