mirror of
https://github.com/R0m1k3/Loki.git
synced 2026-10-22 22:48:11 +02:00
Lot 27 : banc d'essai tenu par la façade (LOKI_BENCH=rust)
Avec LOKI_BENCH=rust (expérimental), la façade sert /api/bench, /api/bench/status, /api/bench/cancel et /api/bench/last, et déroule le protocole de mesure contre le moteur local (rust/loki-rs/src/bench) : mode rapide (préchauffage, ligne courte) et mode complet (prefill à froid à profondeur réelle, trois tours qui reprennent le cache). Corpus, requêtes, calculs et messages sont ceux du Go ; seule une mesure complète est enregistrée (last_bench, bench_presets, empreinte de configuration du Go). Le verrou de génération reste celui de la conversation du Go : la façade le prend par le bail du moteur, avec le motif « bench ». Le Go note alors benching et donne aux autres le refus du benchmark ; changer de discussion ne rompt pas le bail ; benchRunning() le voit (clients /v1, préchauffage, optimiseur). Un stop du chat, un Reset ou un bail perdu annulent le bench. Le Go prête aussi le travail annexe (effacement du slot à la fin, /internal/v1/engine/sidejob) et la télémétrie (/internal/v1/bench/perf). Correction des tests du Go : trois tests du préchauffage échouaient dix minutes par jour, avant minuit (prewarmNearMidnight lisait l'heure réelle). L'heure du garde-fou devient remplaçable (prewarmNow), fixée à midi dans les tests. Parité : parity_bench_test.go tire du Go un journal rejoué par tests/bench.rs contre le même faux moteur ; chaque requête reçue par le moteur est comparée (longs textes réduits à leur taille et à leur empreinte). Il couvre les refus, les deux modes, les échecs de chaque phase, le raisonnement refusé puis rejoué, la profondeur partielle et l'annulation. Les tests du Go sont portés (bench/tests.rs). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016tcGfJWefW5kUGoC2wL8cv
This commit is contained in:
27 files changed
+7580
-12
No files matched your search
@@ -245,6 +245,8 @@ func prewarmNearMidnight(now time.Time) bool {
|
||||
|
||||
// Points d'accroche remplaçables par les tests.
|
||||
var (
|
||||
// prewarmNow : l'heure du garde-fou de minuit.
|
||||
prewarmNow = time.Now
|
||||
// prewarmAsync : false = le préchauffage tourne dans l'appelant (tests).
|
||||
prewarmAsync = true
|
||||
// prewarmSlots : nombre de slots du moteur local (/props total_slots).
|
||||
@@ -375,7 +377,7 @@ func (c *Conversation) prewarm(why string) {
|
||||
return
|
||||
}
|
||||
}
|
||||
if sideJobs.Load() > 0 || benchRunning() || tuneGuard() != nil || prewarmNearMidnight(time.Now()) {
|
||||
if sideJobs.Load() > 0 || benchRunning() || tuneGuard() != nil || prewarmNearMidnight(prewarmNow()) {
|
||||
return
|
||||
}
|
||||
if !healthCheck() {
|
||||
|
||||
@@ -79,6 +79,11 @@ func (m *moteurPrewarm) completions() []string {
|
||||
// prewarmSetup : base neuve, discussion active, faux moteur à un slot,
|
||||
// préchauffage synchrone. keys : réglages posés.
|
||||
func prewarmSetup(t *testing.T, keys map[string]string) *moteurPrewarm {
|
||||
// Le préchauffage se tait dans les dix minutes avant minuit : midi ici,
|
||||
// pour que les tests ne dépendent pas de l'heure à laquelle ils tournent.
|
||||
oldNow := prewarmNow
|
||||
prewarmNow = func() time.Time { return time.Date(2026, 1, 1, 12, 0, 0, 0, time.Local) }
|
||||
t.Cleanup(func() { prewarmNow = oldNow })
|
||||
t.Helper()
|
||||
withWorkspace(t)
|
||||
activeProjMu.Lock()
|
||||
|
||||
@@ -0,0 +1,95 @@
|
||||
package loki
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// facade_bench.go — le banc d'essai tenu par la façade Rust (LOKI_BENCH=rust,
|
||||
// lot 27 de la migration ; expérimental, le Go reste le défaut) : les routes
|
||||
// /api/bench*, le job et le protocole de mesure (rust/loki-rs/src/bench). Le
|
||||
// verrou de génération reste celui de la conversation du Go : la façade le
|
||||
// prend par le bail du moteur, avec le motif « bench » (benching, et le refus
|
||||
// errBenchBusy pour les autres). Le Go lui prête aussi l'isolation des travaux
|
||||
// annexes (effacement du slot à la fin) et la télémétrie (perf_log.go).
|
||||
|
||||
const facadeBenchEnv = "LOKI_BENCH"
|
||||
|
||||
// facadeBench : la façade tient-elle le banc d'essai ?
|
||||
func facadeBench() bool {
|
||||
return strings.EqualFold(strings.TrimSpace(os.Getenv(facadeBenchEnv)), "rust") &&
|
||||
facadeAddr() != "" && internalToken() != ""
|
||||
}
|
||||
|
||||
// facadeBenchRoutes : les routes que la façade sert alors.
|
||||
var facadeBenchRoutes = map[string]bool{
|
||||
"/api/bench": true,
|
||||
"/api/bench/status": true,
|
||||
"/api/bench/cancel": true,
|
||||
"/api/bench/last": true,
|
||||
}
|
||||
|
||||
// facadeSideJobs : les travaux annexes ouverts par la façade (id → fin).
|
||||
var facadeSideJobs = struct {
|
||||
mu sync.Mutex
|
||||
seq int
|
||||
m map[int]func()
|
||||
}{m: map[int]func(){}}
|
||||
|
||||
// handleInternalSideJob : POST {} ouvre un travail annexe (engineSideJob) et
|
||||
// rend son id ; POST {"end": id, "served": bool} le ferme — served : le
|
||||
// moteur a servi au moins une requête du travail (engineServed), le slot
|
||||
// porte donc son état et peut être effacé.
|
||||
func handleInternalSideJob(w http.ResponseWriter, r *http.Request) {
|
||||
var in struct {
|
||||
End int `json:"end"`
|
||||
Served bool `json:"served"`
|
||||
}
|
||||
if !readLeaseBody(w, r, &in) {
|
||||
return
|
||||
}
|
||||
if in.End == 0 {
|
||||
done := engineSideJob()
|
||||
facadeSideJobs.mu.Lock()
|
||||
facadeSideJobs.seq++
|
||||
id := facadeSideJobs.seq
|
||||
facadeSideJobs.m[id] = done
|
||||
facadeSideJobs.mu.Unlock()
|
||||
sendJSON(w, http.StatusOK, map[string]any{"id": id})
|
||||
return
|
||||
}
|
||||
facadeSideJobs.mu.Lock()
|
||||
done := facadeSideJobs.m[in.End]
|
||||
delete(facadeSideJobs.m, in.End)
|
||||
facadeSideJobs.mu.Unlock()
|
||||
if done == nil {
|
||||
sendJSON(w, http.StatusNotFound, map[string]any{"error": "travail annexe inconnu"})
|
||||
return
|
||||
}
|
||||
if in.Served {
|
||||
engineServed()
|
||||
}
|
||||
done()
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
|
||||
// handleInternalBenchPerf : la trace d'un bench de la façade dans la
|
||||
// télémétrie (le bench a pris le slot : la perte de cache du tour suivant lui
|
||||
// revient).
|
||||
func handleInternalBenchPerf(w http.ResponseWriter, r *http.Request) {
|
||||
var in struct {
|
||||
PromptN int `json:"prompt_n"`
|
||||
PromptMs float64 `json:"prompt_ms"`
|
||||
PromptPerSecond float64 `json:"prompt_per_second"`
|
||||
PredictedN int `json:"predicted_n"`
|
||||
PredictedPerSec float64 `json:"predicted_per_second"`
|
||||
}
|
||||
if !readLeaseBody(w, r, &in) {
|
||||
return
|
||||
}
|
||||
perfRecord(perfRec{Kind: perfBench, Complete: true, Total: in.PromptN, New: in.PromptN,
|
||||
PPms: in.PromptMs, PPtps: in.PromptPerSecond, Gen: in.PredictedN, TGtps: in.PredictedPerSec}, nil)
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
@@ -0,0 +1,167 @@
|
||||
package loki
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Le banc d'essai part à la façade avec LOKI_BENCH=rust (lot 27) ; le reste,
|
||||
// non. La façade tient le verrou de génération par le bail du moteur, motif
|
||||
// « bench », et le Go lui prête le travail annexe et la télémétrie.
|
||||
func TestFacadeBench(t *testing.T) {
|
||||
testHome(t)
|
||||
var mu sync.Mutex
|
||||
var calls []string
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Header.Get(internalTokenHeader) != jetonTest {
|
||||
w.WriteHeader(403)
|
||||
return
|
||||
}
|
||||
mu.Lock()
|
||||
calls = append(calls, r.Method+" "+r.URL.Path)
|
||||
mu.Unlock()
|
||||
io.WriteString(w, `{"ok":true,"de":"facade"}`)
|
||||
}))
|
||||
defer srv.Close()
|
||||
t.Setenv(internalTokenEnv, jetonTest)
|
||||
t.Setenv(facadeAddrEnv, strings.TrimPrefix(srv.URL, "http://"))
|
||||
local := func(w http.ResponseWriter, r *http.Request) { io.WriteString(w, "go") }
|
||||
|
||||
t.Setenv(facadeBenchEnv, "")
|
||||
if facadeBench() {
|
||||
t.Fatal("tenu sans LOKI_BENCH")
|
||||
}
|
||||
for p := range facadeBenchRoutes {
|
||||
w := httptest.NewRecorder()
|
||||
withFacadeChat(p, local)(w, httptest.NewRequest("POST", p, strings.NewReader("{}")))
|
||||
if w.Body.String() != "go" {
|
||||
t.Fatalf("sans LOKI_BENCH, %s : %q", p, w.Body.String())
|
||||
}
|
||||
}
|
||||
t.Setenv(facadeBenchEnv, " RUST")
|
||||
if len(facadeBenchRoutes) != 4 {
|
||||
t.Fatalf("routes : %v", facadeBenchRoutes)
|
||||
}
|
||||
for p := range facadeBenchRoutes {
|
||||
w := httptest.NewRecorder()
|
||||
withFacadeChat(p, local)(w, httptest.NewRequest("POST", p, strings.NewReader("{}")))
|
||||
if !strings.Contains(w.Body.String(), "facade") {
|
||||
t.Fatalf("%s non relayé : %q", p, w.Body.String())
|
||||
}
|
||||
}
|
||||
mu.Lock()
|
||||
n := len(calls)
|
||||
mu.Unlock()
|
||||
if n != len(facadeBenchRoutes) {
|
||||
t.Fatalf("relais : %v", calls)
|
||||
}
|
||||
w := httptest.NewRecorder()
|
||||
withFacadeChat("/api/tune", local)(w, httptest.NewRequest("POST", "/api/tune", nil))
|
||||
if w.Body.String() != "go" {
|
||||
t.Fatalf("l'optimiseur reste au Go : %q", w.Body.String())
|
||||
}
|
||||
|
||||
// Le bail « bench » : le verrou, le refus du bench pour les autres.
|
||||
post := func(h http.HandlerFunc, path, body string) (int, map[string]any) {
|
||||
rec := httptest.NewRecorder()
|
||||
h(rec, httptest.NewRequest("POST", path, strings.NewReader(body)))
|
||||
var out map[string]any
|
||||
_ = json.Unmarshal(rec.Body.Bytes(), &out)
|
||||
return rec.Code, out
|
||||
}
|
||||
code, out := post(handleInternalLease, "/internal/v1/engine/lease", `{"ttl_ms":60000,"why":"bench"}`)
|
||||
if code != 200 {
|
||||
t.Fatalf("bail : %d %v", code, out)
|
||||
}
|
||||
id, _ := out["lease"].(string)
|
||||
if !benchRunning() || conv.busyReason() != errBenchBusy {
|
||||
t.Fatalf("bail du bench : running=%v refus=%v", benchRunning(), conv.busyReason())
|
||||
}
|
||||
if code, out := post(handleInternalLease, "/internal/v1/engine/lease", `{}`); code != 409 || out["error"] != errBenchBusy.Error() {
|
||||
t.Fatalf("second bail : %d %v", code, out)
|
||||
}
|
||||
// Changer de discussion ne rompt pas le bail d'un bench.
|
||||
conv.loadFrom("autre", nil)
|
||||
if !benchRunning() || !conv.isGenerating() {
|
||||
t.Fatal("le bail du bench a sauté à la bascule de discussion")
|
||||
}
|
||||
if code, _ := post(handleInternalLeaseRelease, "/internal/v1/engine/lease/release", `{"lease":"`+id+`"}`); code != 204 {
|
||||
t.Fatalf("libération : %d", code)
|
||||
}
|
||||
if benchRunning() || conv.isGenerating() || conv.busyReason() == errBenchBusy {
|
||||
t.Fatal("bail rendu : le verrou devait être libre")
|
||||
}
|
||||
// Un bail ordinaire n'est pas un bench ; un bail de bench expiré rend
|
||||
// le verrou et le refus.
|
||||
code, out = post(handleInternalLease, "/internal/v1/engine/lease", `{}`)
|
||||
if code != 200 || benchRunning() {
|
||||
t.Fatalf("bail ordinaire : %d %v bench=%v", code, out, benchRunning())
|
||||
}
|
||||
post(handleInternalLeaseRelease, "/internal/v1/engine/lease/release", `{"lease":"`+out["lease"].(string)+`"}`)
|
||||
conv.mu.Lock()
|
||||
conv.extLeaseClearLocked()
|
||||
conv.Generating = false
|
||||
conv.mu.Unlock()
|
||||
if _, err := conv.extLeaseAcquire(5*time.Second, true); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
conv.mu.Lock()
|
||||
st := conv.extLease
|
||||
conv.mu.Unlock()
|
||||
conv.extLeaseExpire(st)
|
||||
if benchRunning() || conv.busyReason() == errBenchBusy {
|
||||
t.Fatal("bail de bench expiré : refus du bench encore là")
|
||||
}
|
||||
|
||||
// Le travail annexe ouvert puis fermé par la façade.
|
||||
code, out = post(handleInternalSideJob, "/internal/v1/engine/sidejob", `{}`)
|
||||
job, _ := out["id"].(float64)
|
||||
if code != 200 || job <= 0 || sideJobs.Load() != 1 {
|
||||
t.Fatalf("ouverture : %d %v (en cours : %d)", code, out, sideJobs.Load())
|
||||
}
|
||||
served := engineServedCount()
|
||||
if code, _ := post(handleInternalSideJob, "/internal/v1/engine/sidejob", `{"end":`+strconv.Itoa(int(job))+`,"served":true}`); code != 204 {
|
||||
t.Fatalf("fermeture : %d", code)
|
||||
}
|
||||
if sideJobs.Load() != 0 || engineServedCount() != served+1 {
|
||||
t.Fatalf("après la fermeture : en cours %d, servies %d", sideJobs.Load(), engineServedCount()-served)
|
||||
}
|
||||
if code, _ := post(handleInternalSideJob, "/internal/v1/engine/sidejob", `{"end":999}`); code != 404 {
|
||||
t.Fatalf("travail inconnu : %d", code)
|
||||
}
|
||||
rec := httptest.NewRecorder()
|
||||
handleInternalSideJob(rec, httptest.NewRequest("GET", "/internal/v1/engine/sidejob", nil))
|
||||
if rec.Code != 405 {
|
||||
t.Fatalf("GET : %d", rec.Code)
|
||||
}
|
||||
|
||||
// La trace du bench dans la télémétrie.
|
||||
var before uint64
|
||||
if recs := perfLog.snapshot(); len(recs) > 0 {
|
||||
before = recs[len(recs)-1].Seq
|
||||
}
|
||||
if code, _ := post(handleInternalBenchPerf, "/internal/v1/bench/perf",
|
||||
`{"prompt_n":2036,"prompt_ms":1018,"prompt_per_second":2000,"predicted_n":300,"predicted_per_second":50}`); code != 204 {
|
||||
t.Fatalf("trace : %d", code)
|
||||
}
|
||||
recs := perfLog.snapshot()
|
||||
if len(recs) == 0 || recs[len(recs)-1].Seq != before+1 {
|
||||
t.Fatalf("trace absente (dernière avant : %d)", before)
|
||||
}
|
||||
r := recs[len(recs)-1]
|
||||
if r.Kind != perfBench || !r.Complete || r.Total != 2036 || r.New != 2036 || r.Gen != 300 || r.TGtps != 50 || r.PPtps != 2000 || r.PPms != 1018 {
|
||||
t.Fatalf("trace : %+v", r)
|
||||
}
|
||||
|
||||
t.Setenv(internalTokenEnv, "")
|
||||
if facadeBench() {
|
||||
t.Fatal("tenu sans jeton interne")
|
||||
}
|
||||
}
|
||||
@@ -181,6 +181,8 @@ func withFacadeChat(path string, h http.HandlerFunc) http.HandlerFunc {
|
||||
held, unavailable = facadeStrata, "Strata indisponible : "
|
||||
case facadeTasksRoutes[path]:
|
||||
held, unavailable = facadeTasks, "tâches indisponibles : "
|
||||
case facadeBenchRoutes[path]:
|
||||
held, unavailable = facadeBench, "banc d'essai indisponible : "
|
||||
default:
|
||||
return h
|
||||
}
|
||||
|
||||
@@ -81,11 +81,13 @@ var benchJob struct {
|
||||
canceled bool
|
||||
}
|
||||
|
||||
// benchRunning : un benchmark de ce processus est-il en cours ?
|
||||
// benchRunning : un benchmark est-il en cours, de ce processus ou de la
|
||||
// façade (LOKI_BENCH=rust, qui tient alors le bail du moteur) ?
|
||||
func benchRunning() bool {
|
||||
benchJob.mu.Lock()
|
||||
defer benchJob.mu.Unlock()
|
||||
return benchJob.running
|
||||
running := benchJob.running
|
||||
benchJob.mu.Unlock()
|
||||
return running || benchLeased()
|
||||
}
|
||||
|
||||
// benchRunner est runBench, remplaçable dans les tests.
|
||||
|
||||
@@ -0,0 +1,375 @@
|
||||
//go:build linux
|
||||
|
||||
package loki
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// parity_bench_test.go — journal de référence du banc d'essai (lot 27 de la
|
||||
// migration Rust) : les quatre routes, chaque refus au lancement, le mode
|
||||
// rapide et le mode complet (profondeur, tours, reprise du cache), les
|
||||
// échecs d'une mesure, le niveau de raisonnement refusé puis rejoué, la
|
||||
// profondeur partielle et l'annulation, contre le faux moteur des tests du
|
||||
// bench (llm_bench_test.go). Chaque requête reçue par le moteur est
|
||||
// enregistrée — les longs textes (corpus) réduits à leur taille et à leur
|
||||
// empreinte. Rangé dans rust/loki-rs/tests/parity_bench/bench.json ;
|
||||
// tests/bench.rs le rejoue par la façade.
|
||||
//
|
||||
// LOKI_PARITY_UPDATE=1 go test ./internal/loki -run TestPariteBench
|
||||
|
||||
const parityBenchFile = "../../rust/loki-rs/tests/parity_bench/bench.json"
|
||||
|
||||
// parityBenchEngine : le réglage du faux moteur pour une étape (remis à zéro
|
||||
// à chaque étape qui en donne un).
|
||||
type parityBenchEngine struct {
|
||||
NCtx int `json:"n_ctx"`
|
||||
Busy bool `json:"busy,omitempty"`
|
||||
FailAt int `json:"fail_at,omitempty"`
|
||||
NoTiming bool `json:"no_timing,omitempty"`
|
||||
RejectEffort string `json:"reject_effort,omitempty"`
|
||||
SlowHotMs int `json:"slow_hot_ms,omitempty"`
|
||||
TimeoutMs int `json:"timeout_ms,omitempty"` // benchReqTimeout
|
||||
}
|
||||
|
||||
type parityBenchStep struct {
|
||||
Name string `json:"name"`
|
||||
Config map[string]string `json:"config,omitempty"`
|
||||
Files map[string]string `json:"files,omitempty"`
|
||||
Engine *parityBenchEngine `json:"engine,omitempty"`
|
||||
Tune bool `json:"tune,omitempty"`
|
||||
Generating bool `json:"generating,omitempty"`
|
||||
Method string `json:"method"`
|
||||
Path string `json:"path"`
|
||||
Body string `json:"body,omitempty"`
|
||||
Status int `json:"status"`
|
||||
Out any `json:"out"`
|
||||
// Again : un second lancement pendant le bench ; CancelAt : la phase
|
||||
// (préfixe) à laquelle l'annuler.
|
||||
Again bool `json:"again,omitempty"`
|
||||
AgainOut any `json:"again_out,omitempty"`
|
||||
CancelAt string `json:"cancel_at,omitempty"`
|
||||
Wait bool `json:"wait,omitempty"`
|
||||
Job any `json:"job,omitempty"` // état final du job
|
||||
Calls []any `json:"calls"` // requêtes reçues par le moteur
|
||||
Last any `json:"last"` // state/last_bench
|
||||
Presets any `json:"presets"` // state/bench_presets
|
||||
}
|
||||
|
||||
var (
|
||||
parityBenchIDRe = regexp.MustCompile(`"b[0-9]+-[0-9]+"`)
|
||||
parityBenchTRe = regexp.MustCompile(`"(elapsed_sec|at)":[0-9.e+-]+`)
|
||||
parityBenchFPRe = regexp.MustCompile(`"fingerprint":"[0-9a-f]+"`)
|
||||
)
|
||||
|
||||
// parityBenchDigest : une valeur JSON dont les longs textes sont réduits à
|
||||
// leur taille et à leur empreinte.
|
||||
func parityBenchDigest(v any) any {
|
||||
switch x := v.(type) {
|
||||
case map[string]any:
|
||||
out := map[string]any{}
|
||||
for k, e := range x {
|
||||
out[k] = parityBenchDigest(e)
|
||||
}
|
||||
return out
|
||||
case []any:
|
||||
out := make([]any, len(x))
|
||||
for i, e := range x {
|
||||
out[i] = parityBenchDigest(e)
|
||||
}
|
||||
return out
|
||||
case string:
|
||||
if len(x) > 64 {
|
||||
sum := sha256.Sum256([]byte(x))
|
||||
return fmt.Sprintf("<%d octets %x>", len(x), sum[:6])
|
||||
}
|
||||
return x
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
func TestPariteBench(t *testing.T) {
|
||||
home := testHome(t)
|
||||
oldTimeout := benchReqTimeout
|
||||
// Un état laissé par un autre test du paquet : ni niveau appris, ni bench
|
||||
// précédent.
|
||||
resetBench := func() {
|
||||
effortFallbacks.Range(func(k, _ any) bool {
|
||||
effortFallbacks.Delete(k)
|
||||
return true
|
||||
})
|
||||
benchJob.mu.Lock()
|
||||
benchJob.running, benchJob.id, benchJob.mode, benchJob.phase = false, "", "", ""
|
||||
benchJob.step, benchJob.steps, benchJob.cancel, benchJob.result = 0, 0, nil, nil
|
||||
benchJob.err, benchJob.canceled = "", false
|
||||
benchJob.mu.Unlock()
|
||||
}
|
||||
resetBench()
|
||||
t.Cleanup(func() {
|
||||
benchReqTimeout = oldTimeout
|
||||
// Les niveaux de raisonnement appris ici ne valent que pour ce test.
|
||||
effortFallbacks.Range(func(k, _ any) bool {
|
||||
effortFallbacks.Delete(k)
|
||||
return true
|
||||
})
|
||||
})
|
||||
|
||||
// Le faux moteur : celui des tests du bench, derrière un journal de
|
||||
// toutes les requêtes reçues.
|
||||
var mu sync.Mutex
|
||||
var calls []any
|
||||
fake := &fakeBenchEngine{}
|
||||
inner := http.Handler(nil)
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
b, _ := io.ReadAll(r.Body)
|
||||
entry := map[string]any{"method": r.Method, "path": r.URL.Path}
|
||||
if len(b) > 0 {
|
||||
var body any
|
||||
if json.Unmarshal(b, &body) == nil {
|
||||
entry["body"] = parityBenchDigest(body)
|
||||
}
|
||||
}
|
||||
mu.Lock()
|
||||
calls = append(calls, entry)
|
||||
h := inner
|
||||
mu.Unlock()
|
||||
r.Body = io.NopCloser(strings.NewReader(string(b)))
|
||||
h.ServeHTTP(w, r)
|
||||
}))
|
||||
defer srv.Close()
|
||||
port := strings.TrimPrefix(srv.URL, "http://127.0.0.1:")
|
||||
l, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
closed := strconv.Itoa(l.Addr().(*net.TCPAddr).Port)
|
||||
l.Close()
|
||||
|
||||
mask := func(s string) string {
|
||||
s = strings.ReplaceAll(s, home, "{{HOME}}")
|
||||
s = parityBenchIDRe.ReplaceAllString(s, `"{{ID}}"`)
|
||||
s = parityBenchTRe.ReplaceAllString(s, `"$1":"{{T}}"`)
|
||||
return parityBenchFPRe.ReplaceAllString(s, `"fingerprint":"{{FP}}"`)
|
||||
}
|
||||
unmask := func(s string) string {
|
||||
s = strings.ReplaceAll(s, "{{PORT}}", port)
|
||||
s = strings.ReplaceAll(s, "{{CLOSED}}", closed)
|
||||
return strings.ReplaceAll(s, "{{HOME}}", home)
|
||||
}
|
||||
decode := func(raw string) any {
|
||||
var v any
|
||||
if raw == "" {
|
||||
return nil
|
||||
}
|
||||
if err := json.Unmarshal([]byte(mask(raw)), &v); err != nil {
|
||||
t.Fatalf("illisible : %q", raw)
|
||||
}
|
||||
return v
|
||||
}
|
||||
handlers := map[string]http.HandlerFunc{
|
||||
"/api/bench": handleBench,
|
||||
"/api/bench/status": handleBenchStatus,
|
||||
"/api/bench/cancel": handleBenchCancel,
|
||||
"/api/bench/last": handleBenchLast,
|
||||
}
|
||||
call := func(method, path, body string) (int, any) {
|
||||
rec := httptest.NewRecorder()
|
||||
handlers[path](rec, httptest.NewRequest(method, path, strings.NewReader(body)))
|
||||
return rec.Code, decode(rec.Body.String())
|
||||
}
|
||||
status := func() map[string]any {
|
||||
_, v := call("GET", "/api/bench/status", "")
|
||||
return v.(map[string]any)
|
||||
}
|
||||
wait := func(name string) {
|
||||
deadline := time.Now().Add(60 * time.Second)
|
||||
for status()["running"] == true {
|
||||
if time.Now().After(deadline) {
|
||||
t.Fatalf("%s : le bench ne finit pas", name)
|
||||
}
|
||||
time.Sleep(5 * time.Millisecond)
|
||||
}
|
||||
}
|
||||
|
||||
var steps []parityBenchStep
|
||||
run := func(st parityBenchStep) {
|
||||
for k, v := range st.Config {
|
||||
if err := SetConfigKey(k, unmask(v)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
files := map[string]string{}
|
||||
for k, v := range st.Files {
|
||||
files[k] = unmask(v)
|
||||
}
|
||||
parityApplyFiles(t, home, files)
|
||||
if e := st.Engine; e != nil {
|
||||
f := &fakeBenchEngine{nCtx: e.NCtx, busy: e.Busy, failAt: e.FailAt, noTiming: e.NoTiming,
|
||||
rejectEffort: e.RejectEffort, slowHot: time.Duration(e.SlowHotMs) * time.Millisecond}
|
||||
mu.Lock()
|
||||
fake = f
|
||||
inner = f.handler(t)
|
||||
mu.Unlock()
|
||||
benchReqTimeout = oldTimeout
|
||||
if e.TimeoutMs > 0 {
|
||||
benchReqTimeout = time.Duration(e.TimeoutMs) * time.Millisecond
|
||||
}
|
||||
}
|
||||
mu.Lock()
|
||||
calls = nil
|
||||
mu.Unlock()
|
||||
var lock *tuneLock
|
||||
if st.Tune {
|
||||
l, err := tuneLockAcquire("web", false)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
lock = l
|
||||
}
|
||||
if st.Generating {
|
||||
conv.mu.Lock()
|
||||
conv.Generating = true
|
||||
conv.mu.Unlock()
|
||||
}
|
||||
st.Status, st.Out = call(st.Method, st.Path, unmask(st.Body))
|
||||
if st.Generating {
|
||||
conv.mu.Lock()
|
||||
conv.Generating = false
|
||||
conv.mu.Unlock()
|
||||
}
|
||||
if lock != nil {
|
||||
lock.release()
|
||||
}
|
||||
if st.Again {
|
||||
_, st.AgainOut = call("POST", "/api/bench", "{}")
|
||||
}
|
||||
if st.CancelAt != "" {
|
||||
deadline := time.Now().Add(30 * time.Second)
|
||||
for {
|
||||
p, _ := status()["phase"].(string)
|
||||
if strings.HasPrefix(p, st.CancelAt) {
|
||||
break
|
||||
}
|
||||
if time.Now().After(deadline) {
|
||||
t.Fatalf("%s : phase %q jamais atteinte", st.Name, st.CancelAt)
|
||||
}
|
||||
time.Sleep(2 * time.Millisecond)
|
||||
}
|
||||
// Le moteur a reçu la requête de cette phase avant l'annulation.
|
||||
for {
|
||||
mu.Lock()
|
||||
n := len(fake.calls)
|
||||
mu.Unlock()
|
||||
if n >= 4 {
|
||||
break
|
||||
}
|
||||
time.Sleep(2 * time.Millisecond)
|
||||
}
|
||||
call("POST", "/api/bench/cancel", "")
|
||||
}
|
||||
if st.Wait || st.CancelAt != "" {
|
||||
wait(st.Name)
|
||||
st.Job = status()
|
||||
}
|
||||
mu.Lock()
|
||||
st.Calls = append([]any{}, calls...)
|
||||
mu.Unlock()
|
||||
st.Last = decode(string(getBytes(bkState, "last_bench")))
|
||||
st.Presets = decode(string(getBytes(bkState, "bench_presets")))
|
||||
steps = append(steps, st)
|
||||
}
|
||||
|
||||
preset := "MODEL=/models/qwen.gguf\nPORT={{PORT}}\nCTX=16384\nTOP_K=40\nMIN_P=0.05\nREASONING=off\nCACHE_ISOLATE=off\n"
|
||||
base := map[string]string{"MODEL": "/models/qwen.gguf", "PORT": "{{PORT}}", "CTX": "16384",
|
||||
"TOP_K": "40", "MIN_P": "0.05", "REASONING": "off", "CACHE_ISOLATE": "off"}
|
||||
for _, st := range []parityBenchStep{
|
||||
{Name: "statut sans bench", Config: base, Files: map[string]string{"presets/qwen.env": preset},
|
||||
Engine: &parityBenchEngine{NCtx: 16384}, Method: "GET", Path: "/api/bench/status"},
|
||||
{Name: "dernier bench : aucun", Method: "GET", Path: "/api/bench/last"},
|
||||
{Name: "lancement en GET", Method: "GET", Path: "/api/bench"},
|
||||
{Name: "annulation en GET", Method: "GET", Path: "/api/bench/cancel"},
|
||||
{Name: "annulation sans bench", Method: "POST", Path: "/api/bench/cancel"},
|
||||
{Name: "preset externe", Config: map[string]string{"EXTERNAL": "1"}, Method: "POST", Path: "/api/bench", Body: `{}`},
|
||||
{Name: "optimisation en cours", Config: map[string]string{"EXTERNAL": ""}, Tune: true, Method: "POST", Path: "/api/bench", Body: `{}`},
|
||||
{Name: "modèle pas prêt", Config: map[string]string{"PORT": "{{CLOSED}}"}, Method: "POST", Path: "/api/bench", Body: `{}`},
|
||||
{Name: "génération en cours", Config: map[string]string{"PORT": "{{PORT}}"}, Generating: true, Method: "POST", Path: "/api/bench", Body: `{}`},
|
||||
{Name: "moteur occupé par un autre", Engine: &parityBenchEngine{NCtx: 16384, Busy: true},
|
||||
Method: "POST", Path: "/api/bench", Body: `{}`, Wait: true},
|
||||
{Name: "mode rapide", Engine: &parityBenchEngine{NCtx: 16384}, Method: "POST", Path: "/api/bench", Body: `{}`, Wait: true},
|
||||
{Name: "dernier bench", Method: "GET", Path: "/api/bench/last"},
|
||||
{Name: "mode complet", Engine: &parityBenchEngine{NCtx: 16384}, Method: "POST", Path: "/api/bench", Body: `{"mode":" FULL "}`, Wait: true},
|
||||
{Name: "mode complet, contexte trop petit", Engine: &parityBenchEngine{NCtx: 4096},
|
||||
Method: "POST", Path: "/api/bench", Body: `{"mode":"full"}`, Wait: true},
|
||||
{Name: "contexte inconnu du moteur : celui du preset", Engine: &parityBenchEngine{NCtx: 0},
|
||||
Method: "POST", Path: "/api/bench", Body: `{"mode":"full"}`, Wait: true},
|
||||
{Name: "paramètres bornés", Engine: &parityBenchEngine{NCtx: 65536},
|
||||
Method: "POST", Path: "/api/bench", Body: `{"prompt":99999,"n":9999,"mode":"quick"}`, Wait: true},
|
||||
{Name: "petits paramètres", Engine: &parityBenchEngine{NCtx: 16384},
|
||||
Method: "POST", Path: "/api/bench", Body: `{"prompt":100,"n":8}`, Wait: true},
|
||||
{Name: "corps illisible : défauts", Engine: &parityBenchEngine{NCtx: 16384},
|
||||
Method: "POST", Path: "/api/bench", Body: `{"prompt":"beaucoup"}`, Wait: true},
|
||||
{Name: "le préchauffage échoue", Engine: &parityBenchEngine{NCtx: 16384, FailAt: 1},
|
||||
Method: "POST", Path: "/api/bench", Body: `{}`, Wait: true},
|
||||
{Name: "la ligne courte échoue", Engine: &parityBenchEngine{NCtx: 16384, FailAt: 2},
|
||||
Method: "POST", Path: "/api/bench", Body: `{}`, Wait: true},
|
||||
{Name: "le prefill à froid échoue", Engine: &parityBenchEngine{NCtx: 16384, FailAt: 3},
|
||||
Method: "POST", Path: "/api/bench", Body: `{"mode":"full"}`, Wait: true},
|
||||
{Name: "un tour en profondeur échoue", Engine: &parityBenchEngine{NCtx: 16384, FailAt: 5},
|
||||
Method: "POST", Path: "/api/bench", Body: `{"mode":"full"}`, Wait: true},
|
||||
{Name: "sans timings", Engine: &parityBenchEngine{NCtx: 16384, NoTiming: true},
|
||||
Method: "POST", Path: "/api/bench", Body: `{}`, Wait: true},
|
||||
{Name: "niveau de raisonnement refusé puis rejoué",
|
||||
Config: map[string]string{"REASONING": "", "REASONING_EFFORT": "high"},
|
||||
Engine: &parityBenchEngine{NCtx: 16384, RejectEffort: "high"},
|
||||
Method: "POST", Path: "/api/bench", Body: `{}`, Wait: true},
|
||||
{Name: "niveau appris : envoyé d'emblée", Engine: &parityBenchEngine{NCtx: 16384, RejectEffort: "high"},
|
||||
Method: "POST", Path: "/api/bench", Body: `{}`, Wait: true},
|
||||
{Name: "budget d'une requête dépassé : profondeur partielle",
|
||||
Config: map[string]string{"REASONING_EFFORT": "", "REASONING": "off"},
|
||||
Engine: &parityBenchEngine{NCtx: 16384, SlowHotMs: 400, TimeoutMs: 150},
|
||||
Method: "POST", Path: "/api/bench", Body: `{"mode":"full"}`, Wait: true},
|
||||
{Name: "annulation pendant la profondeur", Engine: &parityBenchEngine{NCtx: 16384, SlowHotMs: 20000},
|
||||
Method: "POST", Path: "/api/bench", Body: `{"mode":"full"}`, Again: true, CancelAt: "tour 1/"},
|
||||
{Name: "preset Strata", Config: map[string]string{"ENGINE": "strata"}, Engine: &parityBenchEngine{NCtx: 16384},
|
||||
Method: "POST", Path: "/api/bench", Body: `{}`, Wait: true},
|
||||
{Name: "dernier bench, inchangé", Config: map[string]string{"ENGINE": ""}, Method: "GET", Path: "/api/bench/last"},
|
||||
} {
|
||||
run(st)
|
||||
}
|
||||
|
||||
got, err := parityJSON(map[string]any{"steps": steps})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if os.Getenv("LOKI_PARITY_UPDATE") == "1" {
|
||||
if err := os.MkdirAll("../../rust/loki-rs/tests/parity_bench", 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(parityBenchFile, got, 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return
|
||||
}
|
||||
want, err := os.ReadFile(parityBenchFile)
|
||||
if err != nil {
|
||||
t.Fatalf("journal de référence absent (%v) : LOKI_PARITY_UPDATE=1 pour le créer", err)
|
||||
}
|
||||
if string(got) != string(want) {
|
||||
t.Errorf("le banc d'essai ne produit plus le journal rangé dans %s.\n"+
|
||||
"Si c'est voulu : LOKI_PARITY_UPDATE=1 go test ./internal/loki -run TestPariteBench,\n"+
|
||||
"puis cargo test --test bench dit si le port Rust suit.\n--- obtenu ---\n%s", parityBenchFile, got)
|
||||
}
|
||||
}
|
||||
@@ -129,6 +129,10 @@ func withInternalAPI(next http.Handler) http.Handler {
|
||||
handleInternalProjectSwitch(w, r)
|
||||
case "/internal/v1/project/drop-convs":
|
||||
handleInternalProjectDropConvs(w, r)
|
||||
case "/internal/v1/engine/sidejob":
|
||||
handleInternalSideJob(w, r)
|
||||
case "/internal/v1/bench/perf":
|
||||
handleInternalBenchPerf(w, r)
|
||||
case "/internal/v1/tasks/run":
|
||||
handleInternalTaskRun(w, r)
|
||||
case "/internal/v1/push/send":
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -43,18 +44,29 @@ type extLeaseState struct {
|
||||
stop bool
|
||||
ttl time.Duration
|
||||
timer *time.Timer
|
||||
// bench : le bail d'un benchmark de la façade (LOKI_BENCH=rust) — les
|
||||
// autres reçoivent errBenchBusy, et changer de discussion ne le rompt pas.
|
||||
bench bool
|
||||
}
|
||||
|
||||
// extLeaseClearLocked oublie le bail de la façade. c.mu détenu.
|
||||
// benchLeaseHeld : un bail « bench » de la façade est tenu. Lu sans le verrou
|
||||
// de la conversation (benchRunning est appelé par des chemins qui le tiennent
|
||||
// déjà, le préchauffage par exemple).
|
||||
var benchLeaseHeld atomic.Bool
|
||||
|
||||
func (c *Conversation) extLeaseClearLocked() {
|
||||
if c.extLease != nil {
|
||||
if c.extLease.bench {
|
||||
benchLeaseHeld.Store(false)
|
||||
}
|
||||
c.extLease.timer.Stop()
|
||||
c.extLease = nil
|
||||
}
|
||||
}
|
||||
|
||||
// extLeaseAcquire prend le verrou de génération pour la façade.
|
||||
func (c *Conversation) extLeaseAcquire(ttl time.Duration) (string, error) {
|
||||
func (c *Conversation) extLeaseAcquire(ttl time.Duration, bench bool) (string, error) {
|
||||
if err := tuneGuard(); err != nil {
|
||||
return "", err
|
||||
}
|
||||
@@ -67,9 +79,13 @@ func (c *Conversation) extLeaseAcquire(ttl time.Duration) (string, error) {
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
st := &extLeaseState{id: fmt.Sprintf("%x", b), ttl: ttl}
|
||||
st := &extLeaseState{id: fmt.Sprintf("%x", b), ttl: ttl, bench: bench}
|
||||
st.timer = time.AfterFunc(ttl, func() { c.extLeaseExpire(st) })
|
||||
c.Generating = true
|
||||
if bench {
|
||||
c.benching, c.leaseWhy = true, errBenchBusy
|
||||
benchLeaseHeld.Store(true)
|
||||
}
|
||||
c.cancel = func() {
|
||||
c.mu.Lock()
|
||||
st.stop = true
|
||||
@@ -88,6 +104,10 @@ func (c *Conversation) extLeaseExpire(st *extLeaseState) {
|
||||
}
|
||||
c.extLease = nil
|
||||
c.Generating, c.cancel = false, nil
|
||||
if st.bench {
|
||||
c.benching, c.leaseWhy = false, nil
|
||||
benchLeaseHeld.Store(false)
|
||||
}
|
||||
fmt.Fprintf(os.Stderr, "[lease] bail de la façade expiré sans renouvellement (%s) : verrou du moteur rendu\n", st.ttl)
|
||||
}
|
||||
|
||||
@@ -110,10 +130,16 @@ func (c *Conversation) extLeaseRelease(id string) {
|
||||
if c.extLease == nil || c.extLease.id != id {
|
||||
return
|
||||
}
|
||||
if c.extLease.bench {
|
||||
c.benching, c.leaseWhy = false, nil
|
||||
}
|
||||
c.extLeaseClearLocked()
|
||||
c.Generating, c.cancel = false, nil
|
||||
}
|
||||
|
||||
// benchLeased : un benchmark de la façade tient-il le verrou ?
|
||||
func benchLeased() bool { return benchLeaseHeld.Load() }
|
||||
|
||||
func readLeaseBody(w http.ResponseWriter, r *http.Request, v any) bool {
|
||||
if r.Method != http.MethodPost {
|
||||
w.Header().Set("Allow", http.MethodPost)
|
||||
@@ -134,7 +160,8 @@ func readLeaseBody(w http.ResponseWriter, r *http.Request, v any) bool {
|
||||
|
||||
func handleInternalLease(w http.ResponseWriter, r *http.Request) {
|
||||
var in struct {
|
||||
TTLms int64 `json:"ttl_ms"`
|
||||
TTLms int64 `json:"ttl_ms"`
|
||||
Why string `json:"why"` // "bench" : le bail d'un benchmark
|
||||
}
|
||||
if !readLeaseBody(w, r, &in) {
|
||||
return
|
||||
@@ -143,7 +170,7 @@ func handleInternalLease(w http.ResponseWriter, r *http.Request) {
|
||||
if in.TTLms > 0 {
|
||||
ttl = min(max(time.Duration(in.TTLms)*time.Millisecond, extLeaseMinTTL), extLeaseMaxTTL)
|
||||
}
|
||||
id, err := conv.extLeaseAcquire(ttl)
|
||||
id, err := conv.extLeaseAcquire(ttl, in.Why == "bench")
|
||||
if err != nil {
|
||||
if err == ErrBusy {
|
||||
err = conv.busyReason() // dit QUI occupe le moteur
|
||||
|
||||
@@ -86,7 +86,7 @@ func TestBailInterneVerrouPartage(t *testing.T) {
|
||||
func TestBailInterneNeLibereQueLeSien(t *testing.T) {
|
||||
testHome(t)
|
||||
libérerVerrou(t)
|
||||
id, err := conv.extLeaseAcquire(time.Minute)
|
||||
id, err := conv.extLeaseAcquire(time.Minute, false)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -110,7 +110,7 @@ func TestBailInterneNeLibereQueLeSien(t *testing.T) {
|
||||
func TestBailInterneExpire(t *testing.T) {
|
||||
testHome(t)
|
||||
libérerVerrou(t)
|
||||
id, err := conv.extLeaseAcquire(50 * time.Millisecond)
|
||||
id, err := conv.extLeaseAcquire(50*time.Millisecond, false)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user