Avoid decoding unrelated profiles
This commit is contained in:
@@ -114,13 +114,13 @@ func loadProfile(ctx context.Context, fsys fs.FS, root string, id string) (*doma
|
||||
}
|
||||
continue
|
||||
}
|
||||
if !idMatch {
|
||||
continue
|
||||
}
|
||||
|
||||
prof, err := decodeProfile(data)
|
||||
if err != nil {
|
||||
if idMatch {
|
||||
return nil, fmt.Errorf("%w: %s: %v", ErrInvalidYAML, relPath, err)
|
||||
}
|
||||
continue
|
||||
return nil, fmt.Errorf("%w: %s: %v", ErrInvalidYAML, relPath, err)
|
||||
}
|
||||
|
||||
prof.ID = strings.TrimSpace(prof.ID)
|
||||
|
||||
58
internal/profile/repository_benchmark_test.go
Normal file
58
internal/profile/repository_benchmark_test.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package profile
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"testing"
|
||||
"testing/fstest"
|
||||
)
|
||||
|
||||
func BenchmarkProfileRepositoryLookup(b *testing.B) {
|
||||
for _, size := range []int{10, 1000} {
|
||||
b.Run(fmt.Sprintf("catalog-%d", size), func(b *testing.B) {
|
||||
files := fstest.MapFS{
|
||||
"target.yaml": profileMapFile(`
|
||||
id: target
|
||||
endpoint: http://localhost:8000/v1
|
||||
model: target-model
|
||||
extra_params:
|
||||
selected: true
|
||||
`),
|
||||
}
|
||||
metadataNames := []string{"target.yaml"}
|
||||
for i := 1; i < size; i++ {
|
||||
name := fmt.Sprintf("profile-%04d.yaml", i)
|
||||
files[name] = profileMapFile(fmt.Sprintf(`
|
||||
id: profile-%04d
|
||||
endpoint: http://localhost:8000/v1
|
||||
model: unrelated-model
|
||||
temperature: 0.5
|
||||
max_tokens: 500
|
||||
extra_params:
|
||||
provider:
|
||||
order:
|
||||
- first
|
||||
- second
|
||||
`, i))
|
||||
metadataNames = append(metadataNames, name)
|
||||
}
|
||||
|
||||
fsys := &recordingProfileFS{FS: files}
|
||||
repo := NewFSRepository(fsys, ".")
|
||||
b.ReportAllocs()
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
if _, err := repo.GetProfile(context.Background(), "target"); err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
}
|
||||
b.StopTimer()
|
||||
|
||||
for _, name := range metadataNames {
|
||||
if got := fsys.openCount(name); got != b.N {
|
||||
b.Fatalf("metadata %q opens = %d, want %d", name, got, b.N)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -4,9 +4,11 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"testing/fstest"
|
||||
|
||||
@@ -720,6 +722,70 @@ unknown: true
|
||||
}
|
||||
}
|
||||
|
||||
func TestProfileRepositoryReadsSourcesFreshOnEveryLookup(t *testing.T) {
|
||||
newSource := func() (*recordingProfileFS, Repository) {
|
||||
fsys := &recordingProfileFS{FS: fstest.MapFS{
|
||||
"target.yaml": profileMapFile(`
|
||||
id: target
|
||||
endpoint: http://localhost:8000/v1
|
||||
model: target-model
|
||||
`),
|
||||
"unrelated.yaml": profileMapFile(`
|
||||
id: unrelated
|
||||
endpoint: http://localhost:8000/v1
|
||||
model: unrelated-model
|
||||
`),
|
||||
}}
|
||||
return fsys, NewFSRepository(fsys, ".")
|
||||
}
|
||||
|
||||
t.Run("selected source", func(t *testing.T) {
|
||||
fsys, repo := newSource()
|
||||
for lookup := 1; lookup <= 2; lookup++ {
|
||||
got, err := repo.GetProfile(context.Background(), "target")
|
||||
if err != nil {
|
||||
t.Fatalf("lookup %d: %v", lookup, err)
|
||||
}
|
||||
if got.Model != "target-model" {
|
||||
t.Fatalf("lookup %d model = %q", lookup, got.Model)
|
||||
}
|
||||
for _, name := range []string{"target.yaml", "unrelated.yaml"} {
|
||||
if count := fsys.openCount(name); count != lookup {
|
||||
t.Fatalf("%s opens after lookup %d = %d, want %d", name, lookup, count, lookup)
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("overlay fallthrough", func(t *testing.T) {
|
||||
primaryFS := &recordingProfileFS{FS: fstest.MapFS{
|
||||
"unrelated.yaml": profileMapFile(`
|
||||
id: unrelated
|
||||
endpoint: http://localhost:8000/v1
|
||||
model: unrelated-model
|
||||
`),
|
||||
}}
|
||||
fallbackFS, fallback := newSource()
|
||||
repo := NewOverlayRepository(NewFSRepository(primaryFS, "."), fallback)
|
||||
|
||||
for lookup := 1; lookup <= 2; lookup++ {
|
||||
got, err := repo.GetProfile(context.Background(), "target")
|
||||
if err != nil {
|
||||
t.Fatalf("lookup %d: %v", lookup, err)
|
||||
}
|
||||
if got.Model != "target-model" {
|
||||
t.Fatalf("lookup %d model = %q", lookup, got.Model)
|
||||
}
|
||||
if count := primaryFS.openCount("unrelated.yaml"); count != lookup {
|
||||
t.Fatalf("primary opens after lookup %d = %d, want %d", lookup, count, lookup)
|
||||
}
|
||||
if count := fallbackFS.openCount("target.yaml"); count != lookup {
|
||||
t.Fatalf("fallback opens after lookup %d = %d, want %d", lookup, count, lookup)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestProfileRepositoriesRejectInvalidExecutionSettings(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
@@ -849,6 +915,31 @@ type profileRepositorySource struct {
|
||||
newRepository func(t *testing.T, files map[string]string) Repository
|
||||
}
|
||||
|
||||
type recordingProfileFS struct {
|
||||
fs.FS
|
||||
mu sync.Mutex
|
||||
opened []string
|
||||
}
|
||||
|
||||
func (f *recordingProfileFS) Open(name string) (fs.File, error) {
|
||||
f.mu.Lock()
|
||||
f.opened = append(f.opened, name)
|
||||
f.mu.Unlock()
|
||||
return f.FS.Open(name)
|
||||
}
|
||||
|
||||
func (f *recordingProfileFS) openCount(name string) int {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
count := 0
|
||||
for _, opened := range f.opened {
|
||||
if opened == name {
|
||||
count++
|
||||
}
|
||||
}
|
||||
return count
|
||||
}
|
||||
|
||||
func profileRepositorySources() []profileRepositorySource {
|
||||
return []profileRepositorySource{
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user