/api/providers took ~25ms of CPU a call with the owner's real config (25 providers, plugins signed in), polled by every open window. Its profile had file system calls at about half, each repeated for every provider the list shows: - StampNow stat'ed providers.json, settings.json and the catalog for every provider (KeptListing); under a request's Hold it is looked at once. - plugin.ListedAt was asked for every plugin provider's Listed(); once a request (heldOf "plugin-listed"). - pluginLogins took logins.json's lock, a file opened across magpies, for every plugin even with nothing to change. It now settles the list as read first, and takes the lock and settles again from a fresh read only when something changes. - clientShape read and parsed client-shapes.json for every relay a list shows (TestsAs); parsed once until the file changes (filememo), a copy given out. - Logins(agent) took the same lock for every provider, twice with its account models; listed once an agent a request, a copy each, listed anew after a write (writePrivate's filememo.Forget). Run (on mote unless named): - go test ./internal/provider ./internal/gui -timeout 45m: provider ok (102s) with the new tests; gui red on TestMainWindowCorners (macOS 27, #3949) and on TestSettingsSaveKeepsWhatItDoesNotSend, red on main sinceb09349d17, fixed by #4514's commit before this one. - At the pushed head (rebased one84870f17): go test ./internal/provider ok (100.7s), ./internal/plugin ok (102.7s), TestLoginsListedOnceARequest -count=5 ok (it now clears the 30s look another test left), gui TestSettingsSaveKeepsWhatItDoesNotSend and TestCoreScriptBudget ok; GOOS=linux and windows go vet ./internal/provider ok. Then rebased onto3defaf662(internal/agent dsh.go only); not rerun. - New tests: TestStampLooksOnceARequest, TestClientShapeParsedOnceUntilWritten, TestLoginsListedOnceARequest, and TestAccountListsCopyNoLoginsUnchanged now counts the lock. Broken by hand, each goes red: "51 stamps in one request looked 51 times, want 1", "30 asks parsed the file 30 times", "20 plugin lists with nothing to change took the lock 20 times", "20 lists in one request listed 20 times". - GOOS=linux and windows go vet ./internal/provider: ok. gofmt clean. - Real config, on this Mac: a copy of the owner's ~/.config/magpie in an offline sandbox HOME (proxy :9), 200 GET /api/providers each: v0.1.2541 5.04s CPU (25.2ms a call, median 22.5ms wall), this change 4.55s (22.8ms, median 20.3ms). The profile's stats under StampNow fell from 2.17s to 0.34s. Not run: the full ./... suite; GUI (browser) tests, as nothing the page gets changed. What is left in the profile: StepFun's sign-in file, sync.json, plugin icons and agent detection, each read once a request.
51 lines
1.4 KiB
Go
51 lines
1.4 KiB
Go
package provider
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// TestStampLooksOnceARequest: a request holding the catalog looks at
|
|
// StampNow's files once, not once for each provider it lists, and the
|
|
// next request sees a file another process wrote between them.
|
|
func TestStampLooksOnceARequest(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"))
|
|
defer func(d time.Duration) { heldFor = d }(heldFor)
|
|
heldFor = time.Hour // a loaded machine's pause isn't the bound
|
|
if err := Save(Provider{ID: "relay", Name: "Relay", Key: "k", Chat: "http://127.0.0.1:1/v1", Models: []string{"m1"}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
release := Hold()
|
|
looks := stampLooks.Load()
|
|
first := StampNow()
|
|
for range 50 {
|
|
if StampNow() != first {
|
|
t.Fatal("the stamp moved within a request")
|
|
}
|
|
}
|
|
if n := stampLooks.Load() - looks; n != 1 {
|
|
t.Fatalf("51 stamps in one request looked %d times, want 1", n)
|
|
}
|
|
release()
|
|
|
|
// another process rewrites the providers' file: a size of its own
|
|
b, err := os.ReadFile(Path())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(Path(), append(b, '\n', '\n'), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer Hold()()
|
|
if StampNow() == first {
|
|
t.Fatal("the next request's stamp missed another process's write")
|
|
}
|
|
}
|