github.com/weaviate/weaviate@v1.24.6/adapters/repos/db/lsmkv/memtable_size_advisor_test.go (about)

     1  //                           _       _
     2  // __      _____  __ ___   ___  __ _| |_ ___
     3  // \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \
     4  //  \ V  V /  __/ (_| |\ V /| | (_| | ||  __/
     5  //   \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___|
     6  //
     7  //  Copyright © 2016 - 2024 Weaviate B.V. All rights reserved.
     8  //
     9  //  CONTACT: hello@weaviate.io
    10  //
    11  
    12  package lsmkv
    13  
    14  import (
    15  	"testing"
    16  	"time"
    17  
    18  	"github.com/stretchr/testify/assert"
    19  )
    20  
    21  const Megabyte = 1024 * 1024
    22  
    23  func TestMemtableSizeAdvisor_Initial(t *testing.T) {
    24  	a := newMemtableSizeAdvisor(memtableSizeAdvisorCfg{
    25  		initial: 10 * Megabyte,
    26  	})
    27  
    28  	assert.Equal(t, 10485760, a.Initial())
    29  }
    30  
    31  func TestMemtableSizeAdvisor_NextTarget(t *testing.T) {
    32  	a := newMemtableSizeAdvisor(memtableSizeAdvisorCfg{
    33  		initial:     10 * Megabyte,
    34  		minDuration: 10 * time.Second,
    35  		maxDuration: 30 * time.Second,
    36  		stepSize:    10 * Megabyte,
    37  		maxSize:     100 * Megabyte,
    38  	})
    39  
    40  	type test struct {
    41  		name            string
    42  		current         int
    43  		lastCycle       time.Duration
    44  		expectedChanged bool
    45  		expectedTarget  int
    46  	}
    47  
    48  	tests := []test{
    49  		{
    50  			name:            "completely within range",
    51  			current:         10 * Megabyte,
    52  			lastCycle:       17 * time.Second,
    53  			expectedChanged: false,
    54  			expectedTarget:  10 * Megabyte,
    55  		},
    56  		{
    57  			name:            "cycle too short",
    58  			current:         10 * Megabyte,
    59  			lastCycle:       7 * time.Second,
    60  			expectedChanged: true,
    61  			expectedTarget:  20 * Megabyte,
    62  		},
    63  		{
    64  			name:            "cycle too long",
    65  			current:         100 * Megabyte,
    66  			lastCycle:       47 * time.Second,
    67  			expectedChanged: true,
    68  			expectedTarget:  90 * Megabyte,
    69  		},
    70  		{
    71  			name:            "cycle too short, but approaching limit",
    72  			current:         95 * Megabyte,
    73  			lastCycle:       7 * time.Second,
    74  			expectedChanged: true,
    75  			expectedTarget:  100 * Megabyte, // not 105 (!)
    76  		},
    77  		{
    78  			name:            "cycle too short, but already at limit",
    79  			current:         100 * Megabyte,
    80  			lastCycle:       7 * time.Second,
    81  			expectedChanged: false,
    82  			expectedTarget:  100 * Megabyte,
    83  		},
    84  		{
    85  			name:            "cycle too long, but barely above initial size",
    86  			current:         12 * Megabyte,
    87  			lastCycle:       47 * time.Second,
    88  			expectedChanged: true,
    89  			expectedTarget:  10 * Megabyte, // not 2 (1)
    90  		},
    91  		{
    92  			name:            "cycle too long, but already at initial size",
    93  			current:         10 * Megabyte,
    94  			lastCycle:       47 * time.Second,
    95  			expectedChanged: false,
    96  			expectedTarget:  10 * Megabyte,
    97  		},
    98  	}
    99  
   100  	for _, test := range tests {
   101  		t.Run(test.name, func(t *testing.T) {
   102  			newTarget, changed := a.NextTarget(test.current, test.lastCycle)
   103  			assert.Equal(t, test.expectedTarget, newTarget, "expect new target")
   104  			assert.Equal(t, test.expectedChanged, changed, "expect changed")
   105  		})
   106  	}
   107  
   108  	target, changed := a.NextTarget(10*1024*1024, 17*time.Second)
   109  	assert.False(t, changed)
   110  	assert.Equal(t, 10*1024*1024, target)
   111  }
   112  
   113  func TestMemtableSizeAdvisor_MissingConfig(t *testing.T) {
   114  	// even with an all-default value config the advisor should still return
   115  	// reasonable results, for example many integration tests might not provide a
   116  	// reasonable config to the advisor
   117  	a := newMemtableSizeAdvisor(memtableSizeAdvisorCfg{})
   118  	assert.Equal(t, 10485760, a.Initial())
   119  }