返回 DeepSeek-Reasonix
listsessions_bench_test.go
根目录 / internal / agent / listsessions_bench_test.go
1 package agent
2
3 import (
4 "encoding/json"
5 "fmt"
6 "os"
7 "path/filepath"
8 "strings"
9 "sync"
10 "testing"
11 "time"
12
13 "reasonix/internal/provider"
14 )
15
16 // TestListSessionsScalingManual reproduces the sidebar "project list loads
17 // slowly" report by timing ListSessions against a synthetically large session
18 // directory. It is gated behind REASONIX_BENCH=1 so it never runs (or writes
19 // hundreds of MB) during a normal `go test`.
20 //
21 // REASONIX_BENCH=1 go test ./internal/agent -run TestListSessionsScalingManual -v
22 func TestListSessionsScalingManual(t *testing.T) {
23 if os.Getenv("REASONIX_BENCH") != "1" {
24 t.Skip("set REASONIX_BENCH=1 to run the scaling benchmark")
25 }
26
27 type shape struct {
28 sessions int
29 turns int // user/assistant pairs per session
30 }
31 shapes := []shape{
32 {sessions: 100, turns: 20},
33 {sessions: 300, turns: 30},
34 {sessions: 500, turns: 40},
35 }
36
37 for _, s := range shapes {
38 name := fmt.Sprintf("%dsessions_%dturns", s.sessions, s.turns)
39 t.Run(name, func(t *testing.T) {
40 dir := t.TempDir()
41 totalBytes := writeSyntheticSessions(t, dir, s.sessions, s.turns)
42
43 // Metadata-only pass: ReadDir + Stat + LoadBranchMeta sidecar.
44 start := time.Now()
45 order, err := ListSessionOrder(dir)
46 if err != nil {
47 t.Fatalf("ListSessionOrder: %v", err)
48 }
49 orderDur := time.Since(start)
50
51 // Cold ListSessions: sidecars have no recorded turn count yet, so this
52 // decodes every .jsonl in full (the pre-fix behaviour) and backfills.
53 start = time.Now()
54 cold, err := ListSessions(dir)
55 if err != nil {
56 t.Fatalf("ListSessions (cold): %v", err)
57 }
58 coldDur := time.Since(start)
59
60 // Warm ListSessions: the backfill populated Turns/Preview in the
61 // sidecars, so this reads them and skips the whole-file decode — the
62 // post-fix steady state the sidebar hits on every refresh.
63 start = time.Now()
64 warm, err := ListSessions(dir)
65 if err != nil {
66 t.Fatalf("ListSessions (warm): %v", err)
67 }
68 warmDur := time.Since(start)
69
70 t.Logf("sessions=%d turns=%d onDisk=%.1fMB | order(meta)=%v (%d) | ListSessions cold(decode+backfill)=%v (%d) | ListSessions warm(sidecar)=%v (%d) | speedup=%.0fx",
71 s.sessions, s.turns, float64(totalBytes)/(1<<20),
72 orderDur.Round(time.Millisecond), len(order),
73 coldDur.Round(time.Millisecond), len(cold),
74 warmDur.Round(time.Millisecond), len(warm),
75 float64(coldDur)/float64(warmDur))
76 })
77 }
78 }
79
80 // writeSyntheticSessions writes n session .jsonl files (newline-delimited
81 // provider.Message, like Session.Save) plus a minimal .meta sidecar each,
82 // mimicking a heavy thinking-mode user: large assistant Content +
83 // ReasoningContent. Returns total on-disk session bytes.
84 func writeSyntheticSessions(t *testing.T, dir string, n, turns int) int64 {
85 t.Helper()
86 content := strings.Repeat("The quick brown fox jumps over the lazy dog. ", 140) // ~6 KB
87 reasoning := strings.Repeat("Let me think step by step about this problem. ", 220) // ~10 KB
88 var total int64
89 for i := range n {
90 base := fmt.Sprintf("20260101-%06d-deepseek-chat", i)
91 path := filepath.Join(dir, base+".jsonl")
92 f, err := os.Create(path)
93 if err != nil {
94 t.Fatalf("create session: %v", err)
95 }
96 enc := json.NewEncoder(f)
97 for tn := range turns {
98 _ = enc.Encode(provider.Message{Role: provider.RoleUser, Content: fmt.Sprintf("user message %d %s", tn, content[:200])})
99 _ = enc.Encode(provider.Message{Role: provider.RoleAssistant, Content: content, ReasoningContent: reasoning})
100 }
101 if st, err := f.Stat(); err == nil {
102 total += st.Size()
103 }
104 f.Close()
105
106 meta := BranchMeta{
107 ID: base,
108 CreatedAt: time.Now().Add(-time.Duration(i) * time.Minute),
109 UpdatedAt: time.Now().Add(-time.Duration(i) * time.Minute),
110 Scope: "global",
111 }
112 b, _ := json.Marshal(meta)
113 if err := os.WriteFile(path+".meta", b, 0o644); err != nil {
114 t.Fatalf("write meta: %v", err)
115 }
116 }
117 return total
118 }
119
120 // TestColdStartProjectTreeManual measures the steady-state cold-start cost that
121 // remains AFTER removing the desktop project-session-cache.json disk cache: a
122 // heavy user spread across many workspaces, every session already carrying
123 // sidecar Turns/Preview. It mirrors ListProjectTree's per-dir fan-out. The point
124 // is to confirm the disk cache is now redundant — sidecar-only listing of the
125 // whole tree is already in the low-millisecond range on cold start.
126 //
127 // REASONIX_BENCH=1 go test ./internal/agent -run TestColdStartProjectTreeManual -v
128 func TestColdStartProjectTreeManual(t *testing.T) {
129 if os.Getenv("REASONIX_BENCH") != "1" {
130 t.Skip("set REASONIX_BENCH=1 to run the cold-start benchmark")
131 }
132
133 const (
134 workspaces = 15
135 perDir = 200
136 turns = 25
137 )
138 root := t.TempDir()
139 dirs := make([]string, workspaces)
140 var onDisk int64
141 for w := range workspaces {
142 dir := filepath.Join(root, fmt.Sprintf("ws-%02d", w))
143 if err := os.MkdirAll(dir, 0o755); err != nil {
144 t.Fatalf("mkdir: %v", err)
145 }
146 dirs[w] = dir
147 onDisk += writeSyntheticSessions(t, dir, perDir, turns)
148 // Warm the sidecars (Turns/Preview), i.e. the steady state a real user is
149 // in after the one-time backfill — this is what cold start reads.
150 if _, err := ListSessions(dir); err != nil {
151 t.Fatalf("warm ListSessions: %v", err)
152 }
153 }
154
155 // Sequential cold start (sum over dirs).
156 start := time.Now()
157 seqTotal := 0
158 for _, dir := range dirs {
159 infos, err := ListSessions(dir)
160 if err != nil {
161 t.Fatalf("ListSessions: %v", err)
162 }
163 seqTotal += len(infos)
164 }
165 seqDur := time.Since(start)
166
167 // Concurrent fan-out, the way ListProjectTree actually loads dirs.
168 start = time.Now()
169 var wg sync.WaitGroup
170 counts := make([]int, len(dirs))
171 for i, dir := range dirs {
172 wg.Add(1)
173 go func(i int, dir string) {
174 defer wg.Done()
175 infos, _ := ListSessions(dir)
176 counts[i] = len(infos)
177 }(i, dir)
178 }
179 wg.Wait()
180 concDur := time.Since(start)
181
182 t.Logf("workspaces=%d sessions/dir=%d totalSessions=%d onDisk=%.0fMB | cold start sequential=%v | cold start concurrent(fan-out)=%v",
183 workspaces, perDir, seqTotal, float64(onDisk)/(1<<20),
184 seqDur.Round(time.Millisecond), concDur.Round(time.Millisecond))
185 }
186
186 lines GO