返回 DeepSeek-Reasonix
fork_target_bench_test.go
根目录 / internal / session / fork_target_bench_test.go
1 package session
2
3 import (
4 "context"
5 "encoding/json"
6 "fmt"
7 "path/filepath"
8 "strings"
9 "testing"
10
11 "reasonix/internal/provider"
12 )
13
14 // benchmarkSourceCommits is long enough that rebuilding the durable transcript
15 // and reading only the turn projection differ by orders of magnitude: it is the
16 // length of the history a long-lived session reaches in practice.
17 const benchmarkSourceCommits = 2000
18
19 // newBenchmarkSource builds the service and one live source with many committed
20 // turns. Every read of a live runtime is the hot path a remote surface refreshes
21 // after each turn, next to the running turn, so the fixture keeps the source
22 // live rather than closing it.
23 func newBenchmarkSource(b *testing.B, commits int) (*Service, SessionRef) {
24 b.Helper()
25 service, err := NewService("local", NewFilesystemPersistence(filepath.Join(b.TempDir(), "sessions-v4")))
26 if err != nil {
27 b.Fatal(err)
28 }
29 b.Cleanup(func() { _ = service.CloseAll(context.Background()) })
30 runtime, err := service.Create(context.Background(), CreateOptions{SessionID: "source"})
31 if err != nil {
32 b.Fatal(err)
33 }
34 // Cleanup runs before the benchmark removes its temp directory, so an open
35 // runtime stops flushing its durable log instead of racing that removal.
36 b.Cleanup(func() { _ = service.Close(context.Background(), runtime.Ref()) })
37 for index := range commits {
38 turnID := fmt.Sprintf("turn-%d", index)
39 payload, err := json.Marshal(map[string]any{"message": provider.Message{
40 ID: "message-" + turnID, Role: provider.RoleAssistant, Content: strings.Repeat("x", 256),
41 }})
42 if err != nil {
43 b.Fatal(err)
44 }
45 if _, err := runtime.Session().AppendBatch(context.Background(), turnID, []Event{
46 {Kind: "turn/start"},
47 {Kind: "message/complete", Payload: payload},
48 {Kind: "turn/end", Payload: json.RawMessage(`{"status":"completed"}`)},
49 }); err != nil {
50 b.Fatal(err)
51 }
52 }
53 return service, runtime.Ref()
54 }
55
56 // BenchmarkForkTargetSetForLiveRuntime measures the fork-target read of a live
57 // source with a long history. The read serves the surface's per-turn refresh,
58 // so it must stay proportional to the turn projection and must not rebuild the
59 // durable transcript it does not read.
60 func BenchmarkForkTargetSetForLiveRuntime(b *testing.B) {
61 service, ref := newBenchmarkSource(b, benchmarkSourceCommits)
62 b.ReportAllocs()
63 b.ResetTimer()
64 for range b.N {
65 set, err := service.ForkTargetSetFor(context.Background(), ref)
66 if err != nil {
67 b.Fatal(err)
68 }
69 if len(set.Targets) != benchmarkSourceCommits {
70 b.Fatalf("targets = %d, want %d", len(set.Targets), benchmarkSourceCommits)
71 }
72 }
73 }
74
74 lines GO