github.com/weaviate/weaviate@v1.24.6/adapters/repos/db/vector/hnsw/dynamic_ef_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 hnsw
    13  
    14  import (
    15  	"context"
    16  	"testing"
    17  
    18  	"github.com/pkg/errors"
    19  	"github.com/stretchr/testify/assert"
    20  	"github.com/stretchr/testify/require"
    21  	"github.com/weaviate/weaviate/adapters/repos/db/vector/hnsw/distancer"
    22  	"github.com/weaviate/weaviate/adapters/repos/db/vector/testinghelpers"
    23  	"github.com/weaviate/weaviate/entities/cyclemanager"
    24  	ent "github.com/weaviate/weaviate/entities/vectorindex/hnsw"
    25  )
    26  
    27  // To prevent a regression on
    28  // https://github.com/weaviate/weaviate/issues/1878
    29  func Test_DynamicEF(t *testing.T) {
    30  	type test struct {
    31  		name       string
    32  		config     ent.UserConfig
    33  		limit      int
    34  		expectedEf int
    35  	}
    36  
    37  	tests := []test{
    38  		{
    39  			name: "all defaults explicitly entered, limit: 100",
    40  			config: ent.UserConfig{
    41  				VectorCacheMaxObjects: 10,
    42  				EF:                    -1,
    43  				DynamicEFMin:          100,
    44  				DynamicEFMax:          500,
    45  				DynamicEFFactor:       8,
    46  			},
    47  			limit:      100,
    48  			expectedEf: 500,
    49  		},
    50  		{
    51  			name: "limit lower than min",
    52  			config: ent.UserConfig{
    53  				VectorCacheMaxObjects: 10,
    54  				EF:                    -1,
    55  				DynamicEFMin:          100,
    56  				DynamicEFMax:          500,
    57  				DynamicEFFactor:       8,
    58  			},
    59  			limit:      10,
    60  			expectedEf: 100,
    61  		},
    62  		{
    63  			name: "limit within the dynamic range",
    64  			config: ent.UserConfig{
    65  				VectorCacheMaxObjects: 10,
    66  				EF:                    -1,
    67  				DynamicEFMin:          100,
    68  				DynamicEFMax:          500,
    69  				DynamicEFFactor:       8,
    70  			},
    71  			limit:      23,
    72  			expectedEf: 184,
    73  		},
    74  		{
    75  			name: "explicit ef",
    76  			config: ent.UserConfig{
    77  				VectorCacheMaxObjects: 10,
    78  				EF:                    78,
    79  			},
    80  			limit:      5,
    81  			expectedEf: 78,
    82  		},
    83  	}
    84  
    85  	for _, test := range tests {
    86  		t.Run(test.name, func(t *testing.T) {
    87  			index, err := New(Config{
    88  				RootPath:              "doesnt-matter-as-committlogger-is-mocked-out",
    89  				ID:                    "dynaimc-ef-test",
    90  				MakeCommitLoggerThunk: MakeNoopCommitLogger,
    91  				DistanceProvider:      distancer.NewCosineDistanceProvider(),
    92  				VectorForIDThunk: func(ctx context.Context, id uint64) ([]float32, error) {
    93  					return nil, errors.Errorf("not implemented")
    94  				},
    95  			}, test.config, cyclemanager.NewCallbackGroupNoop(), cyclemanager.NewCallbackGroupNoop(),
    96  				cyclemanager.NewCallbackGroupNoop(), testinghelpers.NewDummyStore(t))
    97  			require.Nil(t, err)
    98  
    99  			actualEF := index.searchTimeEF(test.limit)
   100  			assert.Equal(t, test.expectedEf, actualEF)
   101  
   102  			require.Nil(t, index.Drop(context.Background()))
   103  		})
   104  	}
   105  }