| 1 | package taskcatalog |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "fmt" |
| 7 | "path/filepath" |
| 8 | "testing" |
| 9 | "time" |
| 10 | |
| 11 | "reasonix/internal/taskmonitor" |
| 12 | ) |
| 13 | |
| 14 | func BenchmarkWarmFirstPageHundredThousandTasks(b *testing.B) { |
| 15 | catalog, err := Open(context.Background(), filepath.Join(b.TempDir(), "tasks.sqlite")) |
| 16 | if err != nil { |
| 17 | b.Fatal(err) |
| 18 | } |
| 19 | b.Cleanup(func() { _ = catalog.Close(context.Background()) }) |
| 20 | root := b.TempDir() |
| 21 | project := Project{Key: ProjectKey(root), Root: root, Label: "Benchmark"} |
| 22 | if _, err := catalog.db.Exec(`INSERT INTO task_projects(project_key,project_root,project_label,state) VALUES(?,?,?,'ready')`, |
| 23 | project.Key, project.Root, project.Label); err != nil { |
| 24 | b.Fatal(err) |
| 25 | } |
| 26 | tx, err := catalog.db.Begin() |
| 27 | if err != nil { |
| 28 | b.Fatal(err) |
| 29 | } |
| 30 | now := time.Now().UTC() |
| 31 | for i := range 100_000 { |
| 32 | task := taskmonitor.TaskSnapshot{SchemaVersion: 1, TaskID: fmt.Sprintf("task-%06d", i), SessionID: "session", |
| 33 | State: taskmonitor.TaskStateSucceeded, Version: 1, CreatedAt: now, UpdatedAt: now.Add(time.Duration(i) * time.Millisecond)} |
| 34 | raw, _ := json.Marshal(task) |
| 35 | if _, err := tx.Exec(`INSERT INTO task_snapshots(project_key,task_id,session_id,state,version,created_at,updated_at,snapshot_json) |
| 36 | VALUES(?,?,?,?,?,?,?,?)`, project.Key, task.TaskID, task.SessionID, task.State, task.Version, |
| 37 | task.CreatedAt.UnixMilli(), task.UpdatedAt.UnixMilli(), raw); err != nil { |
| 38 | b.Fatal(err) |
| 39 | } |
| 40 | } |
| 41 | if err := tx.Commit(); err != nil { |
| 42 | b.Fatal(err) |
| 43 | } |
| 44 | b.ResetTimer() |
| 45 | for range b.N { |
| 46 | if _, err := catalog.ListPage(context.Background(), PageRequest{ProjectKeys: []string{project.Key}, Limit: 50}); err != nil { |
| 47 | b.Fatal(err) |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 |