aiandcloud (2.98B tokens, 12,654 requests a day, server mode behind a
proxy) saw Gateway keys draw a Cloudflare 524 page as raw HTML.
Slow list: every poll of /api/caller-keys read up to a month of ledger
rows once per window per key. budget.AllOf now reads each distinct window
once for all keys, and keeps the settled blocks' sums with a cursor
(usage.VisitFrom), so a poll only reads what was appended since; the
unfinished tail block is always read again. A rewritten or shorter log
resets the cursor. All(k) is AllOf of one key.
Numbers (BenchmarkAll vs BenchmarkReadEach, 8 keys x day/week/month,
31 days x 12000 rows/day = 372k rows, warm): 211-228 ms before, 23.5 ms
after.
Raw HTML: fail() now turns an error string holding a web page (a remote
magpie's or a vendor's 524/502 page inside the JSON error) into a short
said/what pair, "A proxy in front of magpie answered, not magpie ({what})"
with the page's title, translated in all 5 languages (key already in
i18n.js). The browser already handled non-JSON bodies (notJSON, #2032).
Run (Windows box, mote ssh was down earlier): go test budget, usage, gui
(AllOf, Budget, FailSays, I18n, Keys); gateway-keys-proxy-page and
gateway-key-limit GUI files in chromium and webkit, 5 languages: 24/24;
gofmt; vet windows, linux nogui, darwin (mote, only a clang deprecation
warning). Broken by hand: with the old fail() TestFailSaysAProxyPageByItsTitle
is red; with VisitFrom's reset() removed TestAllOfIsWhatEachWindowRead is red.
Not tried: a user-scale cold ledger, a server behind a real Cloudflare,
the full suite before the tag (started after).
139 lines
4.1 KiB
Go
139 lines
4.1 KiB
Go
package budget
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/access"
|
|
"github.com/yetone/magpie/internal/usage"
|
|
)
|
|
|
|
// a big ledger: perDay calls a day for days days over keys keys, each key
|
|
// holding a day, a week and a month limit
|
|
func bigLedger(tb testing.TB, days, perDay, keys int) []access.Key {
|
|
home := tb.TempDir()
|
|
tb.Setenv("HOME", home)
|
|
tb.Setenv("USERPROFILE", home)
|
|
tb.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config"))
|
|
tb.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache"))
|
|
Forget()
|
|
var ks []access.Key
|
|
for i := 0; i < keys; i++ {
|
|
name := fmt.Sprintf("k%d", i)
|
|
if _, err := access.Update("add-key", access.Change{Name: name}); err != nil {
|
|
tb.Fatal(err)
|
|
}
|
|
}
|
|
list, _ := access.List()
|
|
for _, k := range list {
|
|
for _, p := range []string{"day", "week", "month"} {
|
|
if _, err := access.Update("limit-key", access.Change{Key: k.ID, Limit: &access.Limit{Period: p, Tokens: 1 << 40}}); err != nil {
|
|
tb.Fatal(err)
|
|
}
|
|
}
|
|
}
|
|
list, _ = access.List()
|
|
ks = list
|
|
now := time.Now()
|
|
for d := 0; d < days; d++ {
|
|
for i := 0; i < perDay; i++ {
|
|
at := now.Add(-time.Duration(d)*24*time.Hour - time.Duration(i)*time.Second*86400/time.Duration(perDay))
|
|
usage.Append(usage.Record{Time: at, Provider: "p", Model: "m", Input: 1000, Output: 100, Status: 200, CallerKeyID: ks[i%len(ks)].ID})
|
|
}
|
|
}
|
|
return ks
|
|
}
|
|
|
|
func BenchmarkAll(b *testing.B) {
|
|
ks := bigLedger(b, 31, 12000, 8)
|
|
All(ks[0], time.Now()) // the log's first parse is the ledger's, not this read's
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
now := time.Now()
|
|
for _, k := range ks {
|
|
All(k, now)
|
|
}
|
|
}
|
|
}
|
|
|
|
// AllOf counts what read counts for each window of each key, from one pass
|
|
// and from what the last call kept: keys with several limits, a scoped
|
|
// limit, a key issued under another, the log growing past full blocks
|
|
// between calls, and the log rewritten.
|
|
func TestAllOfIsWhatEachWindowRead(t *testing.T) {
|
|
ks := bigLedger(t, 3, 700, 3)
|
|
if _, err := access.Update("limit-key", access.Change{Key: ks[0].ID, Limit: &access.Limit{Period: "day", Tokens: 5000, Models: []string{"p/m"}}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ks, _ = access.List()
|
|
now := time.Now()
|
|
same := func(when string) int {
|
|
t.Helper()
|
|
all := AllOf(ks, now)
|
|
if len(all) != len(ks) {
|
|
t.Fatalf("%s: %d rows for %d keys", when, len(all), len(ks))
|
|
}
|
|
n := 0
|
|
for i, k := range ks {
|
|
ls := k.Limits()
|
|
if len(all[i]) != len(ls) {
|
|
t.Fatalf("%s: key %d: %d statuses for %d limits", when, i, len(all[i]), len(ls))
|
|
}
|
|
for j, l := range ls {
|
|
start, _ := l.Window(now)
|
|
want := read(k.ID, start, l)
|
|
got := all[i][j]
|
|
if got.Calls != want.calls || got.Tokens != want.tokens(l) || got.Cost != want.cost {
|
|
t.Fatalf("%s: key %d limit %d (%s): got %d calls %d tokens, read %d calls %d tokens", when, i, j, l.Period, got.Calls, got.Tokens, want.calls, want.tokens(l))
|
|
}
|
|
n += got.Calls
|
|
}
|
|
}
|
|
return n
|
|
}
|
|
if same("first") == 0 {
|
|
t.Fatal("no call was counted at all")
|
|
}
|
|
same("again")
|
|
for round := 0; round < 3; round++ {
|
|
for i := 0; i < 400+round*300; i++ {
|
|
rec := usage.Record{Time: now.Add(-time.Duration(i) * time.Second), Provider: "p", Model: "m", Input: 10, Status: 200, CallerKeyID: ks[i%len(ks)].ID}
|
|
if i%5 == 0 {
|
|
rec.CallerKeyID, rec.CallerIssuerID = "child", ks[1].ID
|
|
}
|
|
Append(rec)
|
|
}
|
|
same(fmt.Sprintf("grown %d", round))
|
|
}
|
|
// rewritten: the log is another file's
|
|
if err := os.WriteFile(usage.Path(), nil, 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if n := same("cleared"); n != 0 {
|
|
t.Fatalf("a cleared log counted %d calls", n)
|
|
}
|
|
if one := All(ks[1], now); len(one) != len(ks[1].Limits()) {
|
|
t.Fatalf("All: %+v", one)
|
|
}
|
|
}
|
|
|
|
// BenchmarkReadEach is how All answered before AllOf: the ledger read
|
|
// again for each window of each key.
|
|
func BenchmarkReadEach(b *testing.B) {
|
|
ks := bigLedger(b, 31, 12000, 8)
|
|
All(ks[0], time.Now()) // the log's first parse is the ledger's, not this read's
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
now := time.Now()
|
|
for _, k := range ks {
|
|
for _, l := range k.Limits() {
|
|
start, _ := l.Window(now)
|
|
read(k.ID, start, l)
|
|
}
|
|
}
|
|
}
|
|
}
|