github.com/outcaste-io/ristretto@v0.2.3/z/histogram_test.go (about)

     1  package z
     2  
     3  import (
     4  	"math"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/require"
     8  )
     9  
    10  func TestPercentile00(t *testing.T) {
    11  	size := int(math.Ceil((float64(514) - float64(32)) / float64(4)))
    12  	bounds := make([]float64, size+1)
    13  	for i := range bounds {
    14  		if i == 0 {
    15  			bounds[0] = 32
    16  			continue
    17  		}
    18  		if i == size {
    19  			bounds[i] = 514
    20  			break
    21  		}
    22  		bounds[i] = bounds[i-1] + 4
    23  	}
    24  
    25  	h := NewHistogramData(bounds)
    26  	for v := 16; v <= 1024; v = v + 4 {
    27  		for i := 0; i < 1000; i++ {
    28  			h.Update(int64(v))
    29  		}
    30  	}
    31  
    32  	require.Equal(t, h.Percentile(0.0), 32.0)
    33  }
    34  
    35  func TestPercentile99(t *testing.T) {
    36  	size := int(math.Ceil((float64(514) - float64(32)) / float64(4)))
    37  	bounds := make([]float64, size+1)
    38  	for i := range bounds {
    39  		if i == 0 {
    40  			bounds[0] = 32
    41  			continue
    42  		}
    43  		if i == size {
    44  			bounds[i] = 514
    45  			break
    46  		}
    47  		bounds[i] = bounds[i-1] + 4
    48  	}
    49  	h := NewHistogramData(bounds)
    50  	for v := 16; v <= 512; v = v + 4 {
    51  		for i := 0; i < 1000; i++ {
    52  			h.Update(int64(v))
    53  		}
    54  	}
    55  
    56  	require.Equal(t, h.Percentile(0.99), 512.0)
    57  }
    58  
    59  func TestPercentile100(t *testing.T) {
    60  	size := int(math.Ceil((float64(514) - float64(32)) / float64(4)))
    61  	bounds := make([]float64, size+1)
    62  	for i := range bounds {
    63  		if i == 0 {
    64  			bounds[0] = 32
    65  			continue
    66  		}
    67  		if i == size {
    68  			bounds[i] = 514
    69  			break
    70  		}
    71  		bounds[i] = bounds[i-1] + 4
    72  	}
    73  	h := NewHistogramData(bounds)
    74  	for v := 16; v <= 1024; v = v + 4 {
    75  		for i := 0; i < 1000; i++ {
    76  			h.Update(int64(v))
    77  		}
    78  	}
    79  	require.Equal(t, h.Percentile(1.0), 514.0)
    80  }