Files
magpie/internal/gateway/request_builds_once_test.go
T
yetone ed3a46d02b provider: what a request built of the catalog stays for the next request within a second, not built again for each
Hold dropped everything held when the last request let go, so one
request after another built the catalog again from every provider's
files and accounts (All, entries, the groups' entries, the settings):
a fifth to a third of a request's CPU with 30 providers. What is built
now stays until heldFor (1s) is past, as it already did for requests
that overlapped. It is built again at once when anything it was built
from may have moved:
- Changed() and a plugin change (dropHeld), as before;
- a write that only tells filememo (settings.Save, writePrivate, an
  agent's account file): held values carry filememo.Gen(), new here, and
  are dropped when it moved;
- providers.json or settings.json written by another process (the CLI's
  "magpie group add"): the first Hold of a request stats both and drops
  what is held when either changed, so the next request sees it, not a
  second later;
- the groups served or gated (GateGroups, ServeGroups): holdGate drops
  what is held on both changes (TestGroupsOnAndOff was red without it);
- another environment (a test's HOME), by a hash of os.Environ().
Other files another process writes (an agent's own sign-in file) are
seen within heldFor, as filememo already takes a file found unchanged.

Tests:
- TestHoldKeptForTheNextRequest: five requests one after another build
  once. Broken by hand (held.built = nil on the last release put back):
  red, "five requests one after another: built 5 times, want 1".
- TestHoldKeptUntilAWrite: each of filememo.Forget, providers.json and
  settings.json written behind magpie's back, and GateGroups, builds
  again once. Broken by hand, one at a time: the Gen check off, red
  "filememo told: built 0 times, want 1"; the stat off, red
  "providers.json written by another process: built 0 times, want 1";
  holdGate's dropHeld off, red "the groups gated: built 0 times, want 1".
- TestRequestBuildsProvidersOnce now wants 1, 0, then 1 after
  Changed() (a request later than 0.9s after the first may rightly
  build again on a loaded machine; the test allows it).

BenchmarkGroupOverhead (internal/gateway, 200KB Anthropic body,
stream=false, 100x, two rounds alternating; load average ~450, so the
cpu-ms/op it reports is the measure, not wall ns/op). base is 8215e8275:

  12x20 p0/m1  base  25.66 / 24.36 cpu-ms/op  16.8MB  41723-41827 allocs
               this  22.13 / 20.67 cpu-ms/op  13.4MB  33771-33969 allocs
  12x20 g4     base  30.59 / 30.68 cpu-ms/op  22.5MB  53213-53409 allocs
               this  25.91 / 25.60 cpu-ms/op  19.0MB  45213-45613 allocs
  30x60 p0/m1  base  54.95 / 53.26 cpu-ms/op  42.5MB  89466-90023 allocs
               this  30.80 / 29.27 cpu-ms/op  23.0MB  45853-46928 allocs
  30x60 g4     base  66.67 / 73.07 cpu-ms/op  68-70MB  112283-116845 allocs
               this  45.59 / 48.38 cpu-ms/op  49-52MB  68540-74905 allocs

Run: go test ./internal/provider -run 'Hold|Group|Changed|Served'
-count 1: ok (91s). go test ./internal/gateway -run
'^(TestGroupsOnAndOff|TestRequestBuildsProvidersOnce)$' -count 1: ok.
The full suite at the head of this series is in its last commit.
2026-10-19 15:19:31 +08:00

44 lines
1.4 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package gateway
import (
"testing"
"time"
"github.com/yetone/magpie/internal/plugin"
"github.com/yetone/magpie/internal/provider"
)
// A request builds the providers once, however many times it looks a
// model up on its way: each build reads every agent's sign-in, every
// plugin's and the settings, and three builds a request kept magpie at
// 100–350% CPU under seven agents at once (#2502). The next request,
// within a second and nothing written, builds none: one request after
// another built the providers again each time. After a write it builds
// once more.
func TestRequestBuildsProvidersOnce(t *testing.T) {
plugin.UseCached(nil)
setup(t, provider.Chat, &fake{t: t, reply: sse(`{"choices":[{"delta":{"content":"hi"}}]}`, "[DONE]")})
var first time.Time
for i, want := range []int64{1, 0, 1} {
if i == 2 {
provider.Changed() // a provider saved
}
n, start := provider.AllBuilt.Load(), time.Now()
if i == 1 && start.Sub(first) > 900*time.Millisecond {
// what was built is held for a second: a request this late
// (a loaded machine) builds again, rightly
want = 1
}
if i == 0 {
first = start
}
code, body := post(t, "/v1/chat/completions", `{"model":"fake/m1","stream":true,"messages":[{"role":"user","content":"hi"}]}`)
if code != 200 {
t.Fatalf("%d %s", code, body)
}
if got := provider.AllBuilt.Load() - n; got != want {
t.Fatalf("request %d built the providers %d times, want %d", i+1, got, want)
}
}
}