Files
magpie/internal/provider/live_shared_test.go
T
yetone 1f9f74d1c5 provider: the live-model test weighs the same steps on Windows too
TestLiveModelReadWithoutACopy (v0.1.2449) took LiveListed alone as the
cost of a look before the copy. liveByID also asks whether the list is
the provider's and the id it is listed by, which cost more on Windows,
whose stat allocates more: green on macOS, red on Windows ("a look at
one model took 2552 bytes, the list's read 1232"). The baseline now
takes the same steps, and the bar is half a model: the same steps leave
~0 bytes over, a copy 448 and more (on macOS the bar was 16 bytes from
the old code's 1112).

Run on mote and on the Windows box: the test ok -count=10 on each; with
liveByID back on the copying LiveByID it is red 10 of 10 on each. go
vet darwin and windows of internal/provider ok, gofmt -l clean. The
rest of internal/provider on Windows has 14 reds that are the same at
origin/main f9ca3dd44 (keychain, git links, daemon env, two allocation
tests): TestResolveKeptAcrossRequests is #4633, filed now; the
others are #4115, #2808 and #4446.
2026-11-01 03:05:59 +08:00

76 lines
2.6 KiB
Go

package provider
import (
"fmt"
"os"
"path/filepath"
"runtime"
"slices"
"testing"
"time"
"unsafe"
"github.com/yetone/magpie/internal/catalog"
"github.com/yetone/magpie/internal/filememo"
)
// A look at one model of the vendor's list (its APIs, price, tiers), asked
// for every model a page lists, reads the model as the list has it, not a
// copy: the copies were 32 MB of a live magpie's 2.6 GB in a minute and a
// half. A caller's append is its own, and a model the user gave its own id
// is still a copy with that id.
func TestLiveModelReadWithoutACopy(t *testing.T) {
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"))
var ms []catalog.Model
for i := range 200 {
ms = append(ms, catalog.Model{ID: fmt.Sprintf("m-%d", i), Name: fmt.Sprintf("M %d", i), APIs: []string{"openai-responses"}})
}
if err := catalog.SaveLive("r", "http://127.0.0.1:1/v1", ms); err != nil {
t.Fatal(err)
}
old := time.Now().Add(-time.Hour)
if err := os.Chtimes(catalog.LivePath("r"), old, old); err != nil {
t.Fatal(err)
}
p := Provider{ID: "r", Name: "R", Key: "k", Chat: "http://127.0.0.1:1/v1", Models: []string{"m-7"}}
defer Hold()()
defer filememo.Hold()()
if got := p.ListedAPIs("m-7"); !slices.Equal(got, []Protocol{"openai-responses"}) {
t.Fatalf("m-7's APIs: %v", got)
}
bytesPer := func(f func()) uint64 {
var before, after runtime.MemStats
runtime.ReadMemStats(&before)
for range 200 {
f()
}
runtime.ReadMemStats(&after)
return (after.TotalAlloc - before.TotalAlloc) / 200
}
// the steps before the list is read (whether it is the provider's, the
// id it is listed by) and the read itself, the file's look, are the
// same either way; what is over them is the copy. Each costs more on
// Windows, whose stat allocates more.
read := bytesPer(func() { _ = p.lists(); _ = p.OwnModelID("m-7"); catalog.LiveListed("r") })
looked := bytesPer(func() { p.liveByID("m-7") })
// the same steps leave nothing over; a copy, a model and more. Half a
// model leaves room both ways.
if size := uint64(unsafe.Sizeof(catalog.Model{})); looked >= read+size/2 {
t.Fatalf("a look at one model took %d bytes, the list's read %d; a model is %d", looked, read, size)
}
got := p.liveByID("m-7")
_ = append(got, catalog.Model{ID: "added"})
if again := p.liveByID("m-7"); len(again) != 1 || again[0].ID != "m-7" {
t.Fatalf("a caller's append reached the list: %+v", again)
}
if ms := catalog.LiveByID("r", "m-8"); len(ms) != 1 || ms[0].ID != "m-8" {
t.Fatalf("LiveByID: %+v", ms)
}
}