answered() (and unanswered()) called saveSticks on every request answered: up to 512 conversations sorted, marshalled, written to a temp file and renamed, one answer after another under sticksSaving. Now the stick is in memory at once (stickOf reads the map first) and the file follows within sticksFlushAfter (2s): what is answered meanwhile goes in one write. The gateway's drain (ListenAndServe) and magpie ending on a signal (cli/signals.go, beside FlushServed) write what is waiting, so a restart or the magpie handed over to reads every answer. A crash loses the last 2s, as served.json does since 4f9e8331a. In a test binary the wait is 0 and the file is written at once, as it was (no timer outlives a test; 8215e8275 did the same for served.json). A write waiting for another HOME's file (a test's) is dropped, not written into this one. Test: TestSticksWrittenBehind (2s set by the test): 200 answers write no file, the last is known in memory, flushSticks writes all 200, and a short wait writes by itself. Broken by hand (noteSticks calling saveSticks always): red, "affinity.json written on a request answered". BenchmarkAnswered (512 sticks kept, 200x, 2 runs; load average ~450): old (saveSticks inline) 21.3 / 25.0 ms/op 506KB/op 2140 allocs/op new 2.5 / 5.6 us/op 392-434B/op 8 allocs/op Run: go test ./internal/gateway -run '^TestSticksWrittenBehind$' -count 1: ok. go test ./internal/gateway -run 'TestSticksWrittenBehind|Affinity| Sealed|Stick|Strict' -count 1: ok (57.8s).
80 lines
2.4 KiB
Go
80 lines
2.4 KiB
Go
package gateway
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// Who answered a conversation is written to affinity.json behind the
|
|
// answer, once in a while: each answer marshalled up to 512 conversations
|
|
// and wrote and renamed the file, one answer after another.
|
|
func TestSticksWrittenBehind(t *testing.T) {
|
|
fresh(t)
|
|
was := sticksFlushAfter
|
|
sticksFlushAfter = 2 * time.Second // as outside a test binary
|
|
t.Cleanup(func() { flushSticks(); sticksFlushAfter = was })
|
|
sticks.Lock()
|
|
sticks.m = map[string]stick{}
|
|
sticks.Unlock()
|
|
os.Remove(sticksPath())
|
|
c := candidate{p: provider.Provider{ID: "a", Key: "k"}, model: "m", rest: "a"}
|
|
for i := range 200 {
|
|
answered(fmt.Sprintf("conv-%d", i), c, 1, Usage{})
|
|
}
|
|
if _, err := os.Stat(sticksPath()); err == nil {
|
|
t.Fatal("affinity.json written on a request answered")
|
|
}
|
|
sticks.Lock()
|
|
_, ok := stickOf("conv-199")
|
|
sticks.Unlock()
|
|
if !ok {
|
|
t.Fatal("the last answer isn't known in memory")
|
|
}
|
|
flushSticks() // as the gateway ends
|
|
b, err := os.ReadFile(sticksPath())
|
|
if err != nil {
|
|
t.Fatalf("not written as the gateway ends: %v", err)
|
|
}
|
|
var saved map[string]savedStick
|
|
if err := json.Unmarshal(b, &saved); err != nil || len(saved) != 200 {
|
|
t.Fatalf("written %d conversations (%v), want 200", len(saved), err)
|
|
}
|
|
|
|
// and by itself, a moment later
|
|
sticksFlushAfter = 10 * time.Millisecond
|
|
os.Remove(sticksPath())
|
|
answered("conv-late", c, 1, Usage{})
|
|
for deadline := time.Now().Add(30 * time.Second); ; time.Sleep(5 * time.Millisecond) {
|
|
if b, err := os.ReadFile(sticksPath()); err == nil && json.Unmarshal(b, &saved) == nil && saved["conv-late"].Who != "" {
|
|
break
|
|
}
|
|
if time.Now().After(deadline) {
|
|
t.Fatal("an answer never written")
|
|
}
|
|
}
|
|
}
|
|
|
|
func BenchmarkAnswered(b *testing.B) {
|
|
was := sticksFlushAfter
|
|
sticksFlushAfter = 2 * time.Second // as outside a test binary
|
|
b.Cleanup(func() { flushSticks(); sticksFlushAfter = was })
|
|
sticks.Lock()
|
|
sticks.m = map[string]stick{}
|
|
for i := range sticksKept {
|
|
sticks.m[fmt.Sprintf("conv-%d", i)] = stick{rest: "a", who: "a#k", model: "m", turn: 3, at: time.Now()}
|
|
}
|
|
sticks.Unlock()
|
|
c := candidate{p: provider.Provider{ID: "a", Key: "k"}, model: "m", rest: "a"}
|
|
b.ReportAllocs()
|
|
for i := 0; b.Loop(); i++ {
|
|
answered(fmt.Sprintf("conv-%d", i%sticksKept), c, 3, Usage{Input: 100})
|
|
}
|
|
b.StopTimer()
|
|
flushSticks()
|
|
}
|