Repository navigation
Expand file tree
/
Copy pathindex_test.go
More file actions
120 lines (102 loc) · 2.83 KB
/
Copy pathindex_test.go
File metadata and controls
120 lines (102 loc) · 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Copyright (c) Roman Atachiants and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for details.
package search
import (
"fmt"
"math/rand/v2"
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
/*
cpu: 13th Gen Intel(R) Core(TM) i7-13700K
BenchmarkIndex/search-24 10000 102210 ns/op 160 B/op 3 allocs/op
*/
func BenchmarkIndex(b *testing.B) {
index := loadIndex(b)
b.Run("search", func(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = index.Search(index.arr[i%1000].Vector, 5)
}
})
}
func TestIndex(t *testing.T) {
index := loadIndex(t)
assert.Equal(t, 4802, index.Len())
for i := 0; i < 1000; i++ {
record := index.arr[i]
results := index.Search(record.Vector, 5)
assert.Equal(t, 5, len(results))
assert.InDelta(t, 1, results[0].Relevance, 1e-4)
}
}
func TestSearchLimits(t *testing.T) {
index := NewIndex[string]()
assert.Empty(t, index.Search(Vector{1, 0}, 3))
vector := Vector{3, 4}
index.Add(vector, "diagonal")
assert.InDeltaSlice(t, Vector{0.6, 0.8}, vector, 1e-6)
index.Add(Vector{-1, 0}, "opposite")
index.Add(Vector{1, 0}, "aligned")
for _, k := range []int{-1, 0} {
query := Vector{5, 0}
assert.Nil(t, index.Search(query, k))
assert.Equal(t, Vector{5, 0}, query)
}
query := Vector{5, 0}
results := index.Search(query, 10)
assert.Equal(t, Vector{1, 0}, query)
require.Len(t, results, 3)
assert.Equal(t, "aligned", results[0].Value)
assert.Equal(t, "diagonal", results[1].Value)
assert.Equal(t, "opposite", results[2].Value)
assert.Equal(t, float64(-1), results[2].Relevance)
assert.Equal(t, results[:2], index.Search(Vector{1, 0}, 2))
}
func TestCodec_String(t *testing.T) {
const name = "test.bin"
// Create an index
input := NewIndex[string]()
for i := 0; i < 10; i++ {
input.Add(randVec(), fmt.Sprintf("item-%d", i))
}
// Marshal, unmarshal and compare
assert.NoError(t, input.WriteFile(name))
defer os.Remove(name)
output := NewIndex[string]()
assert.NoError(t, output.ReadFile(name))
assert.Equal(t, input, output)
}
func TestCodec_Binary(t *testing.T) {
const name = "test.bin"
// Create an index
input := NewIndex[[]byte]()
for i := 0; i < 10; i++ {
input.Add(randVec(), []byte(fmt.Sprintf("item-%d", i)))
}
// Marshal, unmarshal and compare
assert.NoError(t, input.WriteFile(name))
defer os.Remove(name)
output := NewIndex[[]byte]()
assert.NoError(t, output.ReadFile(name))
assert.Equal(t, input, output)
}
type record struct {
Text string
Vector []float32
}
func loadIndex(t testing.TB) *Index[string] {
index := NewIndex[string]()
assert.NoError(t, index.ReadFile("dist/dataset.bin"))
return index
}
func randVec() []float32 {
v := make([]float32, 384)
for i := range v {
v[i] = rand.Float32()
}
return v
}