| 1 | package sessioncatalog |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "fmt" |
| 7 | "path/filepath" |
| 8 | "testing" |
| 9 | ) |
| 10 | |
| 11 | // Measure the page users first see when recently indexed history includes |
| 12 | // deleted topics. The filter must stay bounded by the requested page and the |
| 13 | // tombstones, not the full retained library. |
| 14 | func BenchmarkOrdinaryFirstPageWithDeletedTopics(b *testing.B) { |
| 15 | const sessions = 100000 |
| 16 | for _, deletedCount := range []int{0, 100, 1000, 10000} { |
| 17 | b.Run(fmt.Sprint(deletedCount), func(b *testing.B) { |
| 18 | catalog, err := Open(b.Context(), Options{Path: filepath.Join(b.TempDir(), "catalog.sqlite"), MetadataOnly: true, StartPaused: true}) |
| 19 | if err != nil { |
| 20 | b.Fatal(err) |
| 21 | } |
| 22 | defer catalog.Close(context.Background()) |
| 23 | _, err = catalog.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`, sessions) |
| 26 | if err != nil { |
| 27 | b.Fatal(err) |
| 28 | } |
| 29 | _, err = catalog.db.ExecContext(b.Context(), `INSERT INTO catalog_topics(scope,workspace_root,workspace_root_key,topic_id,title,created_at,last_activity_at) |
| 30 | SELECT scope,workspace_root,workspace_root_key,topic_id,topic_title,created_at,last_activity_at FROM catalog_sessions`) |
| 31 | if err != nil { |
| 32 | b.Fatal(err) |
| 33 | } |
| 34 | deleted := make([]string, 0, deletedCount) |
| 35 | for i := sessions; i > sessions-deletedCount; i-- { |
| 36 | deleted = append(deleted, fmt.Sprintf("topic-%08d", i)) |
| 37 | } |
| 38 | encoded := "" |
| 39 | if len(deleted) > 0 { |
| 40 | body, err := json.Marshal(deleted) |
| 41 | if err != nil { |
| 42 | b.Fatal(err) |
| 43 | } |
| 44 | encoded = string(body) |
| 45 | } |
| 46 | b.ResetTimer() |
| 47 | for range b.N { |
| 48 | lease, err := catalog.OpenReadLease(b.Context()) |
| 49 | if err != nil { |
| 50 | b.Fatal(err) |
| 51 | } |
| 52 | rows, err := catalog.ListOrdinarySessions(lease.Context(b.Context()), OrdinaryPageRequest{Scope: "global", Limit: 50, ExcludedTopicIDsJSON: encoded}) |
| 53 | lease.Close() |
| 54 | if err != nil || len(rows) != 50 { |
| 55 | b.Fatalf("first page count=%d error=%v", len(rows), err) |
| 56 | } |
| 57 | } |
| 58 | }) |
| 59 | } |
| 60 | } |
| 61 |