| 1 | package sessioncatalog |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "path/filepath" |
| 7 | "slices" |
| 8 | "testing" |
| 9 | "time" |
| 10 | ) |
| 11 | |
| 12 | // This measures only the persisted metadata page, not native application |
| 13 | // startup or execution readiness. Run with -benchtime=30x for a fixed sample. |
| 14 | func BenchmarkOrdinaryFirstPage(b *testing.B) { |
| 15 | for _, count := range []int{100, 10000, 100000} { |
| 16 | b.Run(fmt.Sprint(count), func(b *testing.B) { |
| 17 | c, err := Open(b.Context(), Options{Path: filepath.Join(b.TempDir(), "catalog.sqlite"), MetadataOnly: true, StartPaused: true}) |
| 18 | if err != nil { |
| 19 | b.Fatal(err) |
| 20 | } |
| 21 | defer c.Close(context.Background()) |
| 22 | // Metadata-only fixture: no transcript can be opened accidentally. |
| 23 | _, err = c.db.ExecContext(b.Context(), `WITH RECURSIVE n(i) AS (VALUES(1) UNION ALL SELECT i+1 FROM n WHERE i<?) |
| 24 | INSERT INTO catalog_sessions(path,path_key,directory,directory_key,scope,workspace_root,workspace_root_key,topic_id,topic_title,created_at,last_activity_at,ordinary_visible,logical_topic_id) |
| 25 | SELECT printf('/history/%08d.jsonl',i),printf('/history/%08d.jsonl',i),'/history','/history','global','','',printf('topic-%08d',i),'History',i,i,1,printf('topic-%08d',i) FROM n`, count) |
| 26 | if err != nil { |
| 27 | b.Fatal(err) |
| 28 | } |
| 29 | _, err = c.db.ExecContext(b.Context(), `INSERT INTO catalog_topics(scope,workspace_root,workspace_root_key,topic_id,title,created_at,last_activity_at) SELECT scope,workspace_root,workspace_root_key,topic_id,topic_title,created_at,last_activity_at FROM catalog_sessions`) |
| 30 | if err != nil { |
| 31 | b.Fatal(err) |
| 32 | } |
| 33 | samples := make([]time.Duration, 0, b.N) |
| 34 | b.ResetTimer() |
| 35 | for range b.N { |
| 36 | start := time.Now() |
| 37 | lease, err := c.OpenReadLease(b.Context()) |
| 38 | if err != nil { |
| 39 | b.Fatal(err) |
| 40 | } |
| 41 | rows, err := c.ListOrdinarySessions(lease.Context(b.Context()), OrdinaryPageRequest{Scope: "global", Limit: 50}) |
| 42 | lease.Close() |
| 43 | if err != nil || len(rows) != 50 { |
| 44 | b.Fatalf("first page count=%d error=%v", len(rows), err) |
| 45 | } |
| 46 | samples = append(samples, time.Since(start)) |
| 47 | } |
| 48 | b.StopTimer() |
| 49 | slices.Sort(samples) |
| 50 | if len(samples) > 0 { |
| 51 | b.ReportMetric(float64(samples[(len(samples)*95+99)/100-1])/float64(time.Millisecond), "p95-ms") |
| 52 | } |
| 53 | }) |
| 54 | } |
| 55 | } |
| 56 |