github.com/grafana/pyroscope@v1.18.0/pkg/segmentwriter/client/distributor/placement/adaptiveplacement/shard_allocator_test.go (about)

     1  package adaptiveplacement
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/require"
     8  )
     9  
    10  func Test_shard_allocator(t *testing.T) {
    11  	a := &shardAllocator{
    12  		unitSize:    10,
    13  		min:         1,
    14  		max:         5,
    15  		burstWindow: 50,
    16  		decayWindow: 50,
    17  	}
    18  
    19  	for i, test := range []struct {
    20  		usage uint64
    21  		now   int64
    22  		want  int
    23  	}{
    24  		{0, 0, 1},
    25  		{0, 1, 1},
    26  		{5, 2, 1},
    27  		{10, 3, 2},
    28  		{10, 4, 2},
    29  		{11, 5, 2},
    30  		{20, 6, 5},
    31  		{10, 7, 5},
    32  		{5, 8, 5},
    33  		{5, 9, 5},
    34  		{5, 51, 5},
    35  		{5, 101, 1},
    36  		{100, 151, 5},
    37  	} {
    38  		require.Equal(t, test.want, a.observe(test.usage, test.now), fmt.Sprint(i))
    39  	}
    40  }
    41  
    42  func Test_shard_limit(t *testing.T) {
    43  	a := &shardAllocator{
    44  		unitSize:    128 << 10,
    45  		min:         1,
    46  		max:         10,
    47  		burstWindow: 1e9 * 10,
    48  		decayWindow: 1e9 * 10 * 5,
    49  	}
    50  
    51  	var now int64
    52  	var hi int
    53  	for i := uint64(0); i < 100; i++ {
    54  		old := hi
    55  		hi = a.observe(2*a.unitSize*i, now)
    56  		require.GreaterOrEqual(t, hi, old)
    57  		now += 1e9 * 10
    58  	}
    59  }