-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
index_test.go
95 lines (78 loc) · 1.99 KB
/
index_test.go
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
// 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"
)
/*
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 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
}