Files
magpie/internal/provider/group_held_test.go
T
yetone 627e8e7d95 provider: a request's groups are built once under its Hold, not again by each look-up
groupsIn read providers.json's groups, matched each pattern against
every model and found the automatic groups over the whole catalog, and
every caller did it again: a request to a group asks FindGroup for its
route, members and window, and the group list, several times each.
Under a Hold the built groups are now kept (heldFrom, keyed on the
catalog's entries, dropped by a write as the other held values are),
and each caller gets a deep copy (cloneGroups) so a change of its own,
such as the TUI's edit before a failed save, never reaches the next.

Tests:
- TestGroupsBuiltOnceWhileHeld: 40 look-ups under one Hold build the
  groups 0 more times; a change to a returned group's members isn't
  the next caller's; a SaveGroup under the Hold is seen. Broken by
  hand (groupsIn built every time): red, "the groups were built 40
  times more in 40 looks under one Hold, want 0". Without the copy:
  red, "a change to a held group's members reached the next caller"
  (and TestHeldGroupsAreTheCallers red too).
- TestCloneGroupsSharesNothing fills every field of a Group and its
  Rules by reflection and fails on any slice, map or pointer the copy
  shares, so a field added later is caught. Broken by hand (Weights
  not cloned): red, "Group.Weights is shared with the held group".

BenchmarkGroupLooksHeld (30 providers x 60 models, 4 FindGroup and 1
Groups under one Hold; -benchtime 200x, two rounds; load average ~450):
  before  13.8 / 23.9 ms/op  26.2MB/op  41080 allocs/op
  after   0.18 / 0.14 ms/op   310KB/op    413 allocs/op
User+sys CPU of the binary, 300 iterations minus 3: before 4.5/4.2 s
(~15 ms per request's look-ups), after 0.08/0.06 s (~0.2 ms).
BenchmarkGroupOverhead (200KB, not streamed, 30x): allocs of a group
request fell (30x60 g4 72.9K/75.0K -> 60.5K/75.8K, g1 70.9K/73.1K ->
62.4K/68.4K; 12x20 g4 46.1K/45.8K -> 43.7K/44.7K); cpu-ms/op moved
within the noise of a machine at load ~450, where a request's wall
time passes heldFor and the held values are built again.

Run: go vet ./internal/provider; go test ./internal/provider -run
'Group|Hold|Held|Rule|Clone' -count 1: ok (17.0s); go test
./internal/gateway -run Group -count 1: ok (53.1s).
2026-10-19 15:19:31 +08:00

304 lines
9.3 KiB
Go

package provider
import (
"fmt"
"path/filepath"
"reflect"
"testing"
"time"
"github.com/yetone/magpie/internal/settings"
)
// groupHeldFixture is a dozen providers serving the same models, so
// magpie finds a group of each, and a group of the user's of four.
func groupHeldFixture(t *testing.T) {
t.Helper()
isolate(t)
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config"))
t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache"))
t.Setenv("MAGPIE_NO_ACCOUNTS", "1")
var models []string
for i := range 10 {
models = append(models, fmt.Sprintf("m%d", i))
}
for i := range 12 {
if err := Save(Provider{ID: fmt.Sprintf("p%d", i), Name: fmt.Sprintf("P%d", i), Key: "k", Models: models, Anthropic: "http://127.0.0.1:9"}); err != nil {
t.Fatal(err)
}
}
if err := settings.Save(settings.Settings{DisabledModels: []string{"p11/m9"}}); err != nil {
t.Fatal(err)
}
if err := SaveGroup(Group{ID: "g4", Name: "G4", Members: []string{"p0/m1", "p1/m1", "p2/m1", "p3/m1"}, Routing: Ordered}); err != nil {
t.Fatal(err)
}
Changed()
}
// A request to a group asks for the catalog's groups several times —
// Resolve's Catalog, and Served for the members' images, the reply's
// length and the window — and each built every group's entry again, from
// every member of every group: the burst of CPU at once that a request to
// a group cost with many providers (colmugx and Yayoi_no_yume on X). While
// a request holds the catalog they are built once.
func TestGroupEntriesBuiltOnceWhileHeld(t *testing.T) {
groupHeldFixture(t)
release := Hold()
defer release()
entries := providerEntries()
first := groupEntries(entries)
if len(first) < 2 {
t.Fatalf("the fixture has %d groups; the test needs the found ones too", len(first))
}
if again := groupEntries(entries); &again[0] != &first[0] {
t.Fatal("the groups' entries were built again under one Hold")
}
// what a caller is given is its own to change
c := Catalog()
c[0].Name = "changed"
if Catalog()[0].Name == "changed" || Served()[0].Name == "changed" {
t.Fatal("a change to Catalog's entries reached the next caller's")
}
// another catalog than the one held is built from, not answered for
other := append([]Entry(nil), entries[:len(entries)/2]...)
if got := groupEntries(other); len(got) > 0 && &got[0] == &first[0] {
t.Fatal("the groups of another catalog were the held one's")
}
// a write drops them
if err := SaveGroup(Group{ID: "g4", Name: "Renamed", Members: []string{"p0/m1", "p1/m1"}, Routing: Ordered}); err != nil {
t.Fatal(err)
}
found := false
for _, e := range Catalog() {
found = found || e.ID == GroupPrefix+"g4" && e.Name == "Renamed"
}
if !found {
t.Fatal("a group saved under the Hold kept its old entry")
}
}
// Each group's members (membersIn) asked whether the user disabled them,
// and that parsed settings.json each time: a catalog of found groups
// parsed it once a group, on each of the several looks a request makes.
func TestGroupLooksParseNoSettingsPerGroupWhileHeld(t *testing.T) {
groupHeldFixture(t)
release := Hold()
defer release()
Served() // the first look builds what is held
const looks = 50
before := settings.Parses()
for range looks {
Served()
Groups()
if _, _, ok := FindGroup(GroupPrefix + "g4"); !ok {
t.Fatal("no group g4")
}
}
if n := settings.Parses() - before; n >= looks {
t.Fatalf("settings.json parsed %d times in %d looks at the groups under one Hold", n, looks)
}
// and a model disabled is still left out
for _, e := range Served() {
if e.ID == "p11/m9" {
t.Fatal("a disabled model was served")
}
}
}
// BenchmarkGroupEntries is the groups' entries of a catalog of 30
// providers serving the same 60 models (60 groups magpie finds, of 30
// members each), built afresh: what each request built several times.
func BenchmarkGroupEntries(b *testing.B) {
isolate(b)
home := b.TempDir()
b.Setenv("HOME", home)
b.Setenv("USERPROFILE", home)
b.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config"))
b.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache"))
b.Setenv("MAGPIE_NO_ACCOUNTS", "1")
var models []string
for i := range 60 {
models = append(models, fmt.Sprintf("m%d", i))
}
for i := range 30 {
if err := Save(Provider{ID: fmt.Sprintf("p%d", i), Name: fmt.Sprintf("P%d", i), Key: "k", Models: models, Anthropic: "http://127.0.0.1:9"}); err != nil {
b.Fatal(err)
}
}
Changed()
entries := providerEntries()
b.ResetTimer()
for range b.N {
buildGroupEntries(entries)
}
}
// A request to a group looked its group up through FindGroup and the
// other group readers, and each built every group again: reading
// providers.json's groups, matching each pattern against every model and
// finding the automatic groups over the catalog. While a request holds the
// catalog the groups are built once, and each caller gets its own copy.
func TestGroupsBuiltOnceWhileHeld(t *testing.T) {
groupHeldFixture(t)
release := Hold()
defer release()
Groups()
before := groupsBuilt.Load()
for range 20 {
Groups()
if _, _, ok := FindGroup(GroupPrefix + "g4"); !ok {
t.Fatal("no group g4")
}
}
if n := groupsBuilt.Load() - before; n != 0 {
t.Fatalf("the groups were built %d times more in 40 looks under one Hold, want 0", n)
}
// a caller's change to its copy is not the next caller's
var g4 Group
for _, g := range Groups() {
if g.ID == "g4" {
g4 = g
}
}
g4.Members[0] = "changed"
for _, g := range Groups() {
if g.ID == "g4" && g.Members[0] == "changed" {
t.Fatal("a change to a held group's members reached the next caller")
}
}
// a write builds them again
if err := SaveGroup(Group{ID: "g4", Name: "Renamed", Members: []string{"p0/m1", "p1/m1"}, Routing: Ordered}); err != nil {
t.Fatal(err)
}
if g, _, ok := FindGroup(GroupPrefix + "g4"); !ok || g.Name != "Renamed" {
t.Fatalf("a group saved under the Hold kept its old self: %+v", g)
}
}
// cloneGroups shares nothing with what it copies: every slice, map and
// pointer of a Group and of its Rules, found by reflection, so a field
// added later is copied too or this fails.
func TestCloneGroupsSharesNothing(t *testing.T) {
var g Group
fillEvery(reflect.ValueOf(&g).Elem())
if len(g.Rules) == 0 {
t.Fatal("the fill made no rule")
}
c := cloneGroups([]Group{g})[0]
if !reflect.DeepEqual(c, g) {
t.Fatalf("the copy differs:\n%+v\n%+v", c, g)
}
if path := sharedRef(reflect.ValueOf(g), reflect.ValueOf(c), "Group"); path != "" {
t.Fatalf("%s is shared with the held group", path)
}
}
// fillEvery gives every field of v a value, and each slice, map and pointer one
// element.
func fillEvery(v reflect.Value) {
switch v.Kind() {
case reflect.Struct:
for i := range v.NumField() {
if v.Type().Field(i).IsExported() {
fillEvery(v.Field(i))
}
}
case reflect.Slice:
v.Set(reflect.MakeSlice(v.Type(), 1, 1))
fillEvery(v.Index(0))
case reflect.Map:
v.Set(reflect.MakeMap(v.Type()))
k, e := reflect.New(v.Type().Key()).Elem(), reflect.New(v.Type().Elem()).Elem()
fillEvery(k)
fillEvery(e)
v.SetMapIndex(k, e)
case reflect.Pointer:
v.Set(reflect.New(v.Type().Elem()))
fillEvery(v.Elem())
case reflect.String:
v.SetString("x")
case reflect.Bool:
v.SetBool(true)
case reflect.Int, reflect.Int64, reflect.Int32:
v.SetInt(1)
case reflect.Float64:
v.SetFloat(1)
}
}
// sharedRef names the first slice, map or pointer a and b hold in common.
func sharedRef(a, b reflect.Value, path string) string {
switch a.Kind() {
case reflect.Struct:
for i := range a.NumField() {
if a.Type().Field(i).IsExported() {
if p := sharedRef(a.Field(i), b.Field(i), path+"."+a.Type().Field(i).Name); p != "" {
return p
}
}
}
case reflect.Slice:
if a.Len() > 0 && a.Pointer() == b.Pointer() {
return path
}
for i := range a.Len() {
if p := sharedRef(a.Index(i), b.Index(i), fmt.Sprintf("%s[%d]", path, i)); p != "" {
return p
}
}
case reflect.Map, reflect.Pointer:
if !a.IsNil() && a.Pointer() == b.Pointer() {
return path
}
if a.Kind() == reflect.Pointer && !a.IsNil() {
return sharedRef(a.Elem(), b.Elem(), path)
}
}
return ""
}
// BenchmarkGroupLooksHeld is the group look-ups one request to a group
// makes under its Hold (FindGroup for the route, its members and the
// window, and the group list), over 30 providers serving the same 60
// models.
func BenchmarkGroupLooksHeld(b *testing.B) {
isolate(b)
home := b.TempDir()
b.Setenv("HOME", home)
b.Setenv("USERPROFILE", home)
b.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config"))
b.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache"))
b.Setenv("MAGPIE_NO_ACCOUNTS", "1")
var models []string
for i := range 60 {
models = append(models, fmt.Sprintf("m%d", i))
}
for i := range 30 {
if err := Save(Provider{ID: fmt.Sprintf("p%d", i), Name: fmt.Sprintf("P%d", i), Key: "k", Models: models, Anthropic: "http://127.0.0.1:9"}); err != nil {
b.Fatal(err)
}
}
if err := SaveGroup(Group{ID: "g4", Name: "G4", Members: []string{"p0/m1", "p1/m1", "p2/m1", "p3/m1"}, Routing: Ordered}); err != nil {
b.Fatal(err)
}
Changed()
defer func(d time.Duration) { heldFor = d }(heldFor)
heldFor = time.Hour // the look-ups of one request, not the Hold's age on a loaded machine
release := Hold()
defer release()
providerEntries()
b.ResetTimer()
for range b.N {
for range 4 {
if _, _, ok := FindGroup(GroupPrefix + "g4"); !ok {
b.Fatal("no group g4")
}
}
Groups()
}
}