| 1 | package mapa |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "fmt" |
| 6 | "io" |
| 7 | "log/slog" |
| 8 | "path/filepath" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | "time" |
| 12 | |
| 13 | "amadan.net/rastrillo/rastrillo/db" |
| 14 | |
| 15 | "amadan.net/oficina/mapa/internal/mapa/seal" |
| 16 | ) |
| 17 | |
| 18 | // Recently edited's cost for a heavy editor of few team pages (build |
| 19 | // note 80, review round 6 L1): six team pages and n of the person's |
| 20 | // edits on them, written straight to the event log. |
| 21 | // |
| 22 | // go test -run '^$' -bench RecentEdits ./internal/mapa/ |
| 23 | |
| 24 | func benchStore(tb testing.TB) *Store { |
| 25 | tb.Helper() |
| 26 | logger := slog.New(slog.NewTextHandler(io.Discard, nil)) |
| 27 | database, err := db.Open(filepath.Join(tb.TempDir(), "mapa.db"), |
| 28 | logger) |
| 29 | if err != nil { |
| 30 | tb.Fatal(err) |
| 31 | } |
| 32 | tb.Cleanup(func() { database.Close() }) |
| 33 | sealer, err := seal.New(bytes.Repeat([]byte{3}, 32)) |
| 34 | if err != nil { |
| 35 | tb.Fatal(err) |
| 36 | } |
| 37 | if _, err := App(database, Config{Sealer: sealer}, logger); err != nil { |
| 38 | tb.Fatal(err) |
| 39 | } |
| 40 | return NewStore(database.G, sealer) |
| 41 | } |
| 42 | |
| 43 | // heavyEditor is a store holding count team pages one person created, |
| 44 | // then events edits by them, spread over the first spread pages, and |
| 45 | // that person's member id. |
| 46 | func heavyEditor(tb testing.TB, events, count, spread int) (*Store, |
| 47 | int64) { |
| 48 | tb.Helper() |
| 49 | store := benchStore(tb) |
| 50 | var pages []int64 |
| 51 | for number := range count { |
| 52 | page, err := store.CreateTeamPage(ctx(), "ana@example.com", |
| 53 | fmt.Sprintf("Page %d", number), "Text.") |
| 54 | if err != nil { |
| 55 | tb.Fatal(err) |
| 56 | } |
| 57 | pages = append(pages, page.ID) |
| 58 | } |
| 59 | var member Member |
| 60 | if err := store.database.First(&member).Error; err != nil { |
| 61 | tb.Fatal(err) |
| 62 | } |
| 63 | start := time.Now().UTC() |
| 64 | rows := make([]Event, 0, events) |
| 65 | for number := range events { |
| 66 | rows = append(rows, Event{Nonce: int64(number + 1), |
| 67 | At: start.Add(time.Duration(number) * time.Millisecond), |
| 68 | ActorID: member.ID, ObjectKind: ObjectPage, |
| 69 | ObjectID: pages[number%spread], Verb: VerbEdited}) |
| 70 | } |
| 71 | if err := store.database.CreateInBatches(rows, 1000).Error; err != nil { |
| 72 | tb.Fatal(err) |
| 73 | } |
| 74 | return store, member.ID |
| 75 | } |
| 76 | |
| 77 | func benchmarkRecentEdits(b *testing.B, events, count, spread int) { |
| 78 | store, member := heavyEditor(b, events, count, spread) |
| 79 | b.ResetTimer() |
| 80 | for b.Loop() { |
| 81 | ids, err := store.recentEdits(ctx(), member) |
| 82 | if err != nil { |
| 83 | b.Fatal(err) |
| 84 | } |
| 85 | b.ReportMetric(float64(len(ids)), "pages") |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | // The edits on one page: the five others are found only by reading |
| 90 | // past all of them. |
| 91 | func BenchmarkRecentEdits20k(b *testing.B) { |
| 92 | benchmarkRecentEdits(b, 20000, 6, 1) |
| 93 | } |
| 94 | |
| 95 | func BenchmarkRecentEdits200k(b *testing.B) { |
| 96 | benchmarkRecentEdits(b, 200000, 6, 1) |
| 97 | } |
| 98 | |
| 99 | // The edits spread over all six: the newest few events find them. |
| 100 | func BenchmarkRecentEditsSpread20k(b *testing.B) { |
| 101 | benchmarkRecentEdits(b, 20000, 6, 6) |
| 102 | } |
| 103 | |
| 104 | // A person who wrote in 1,000 team pages: one step per page. |
| 105 | func BenchmarkRecentEditsWide200k(b *testing.B) { |
| 106 | benchmarkRecentEdits(b, 200000, 1000, 1000) |
| 107 | } |
| 108 | |
| 109 | // The plan reads events only through indexes (review round 6 L1): the |
| 110 | // person's pages by idx_events_actor_page, a transaction by |
| 111 | // idx_events_at, and never a scan of the table. |
| 112 | func TestRecentlyEditedReadsByIndex(t *testing.T) { |
| 113 | store, member := heavyEditor(t, 50, 6, 6) |
| 114 | var plan []struct{ Detail string } |
| 115 | err := store.database.Raw("EXPLAIN QUERY PLAN "+recentQuery, |
| 116 | recentArgs(member)...).Scan(&plan).Error |
| 117 | if err != nil { |
| 118 | t.Fatal(err) |
| 119 | } |
| 120 | var lines []string |
| 121 | for _, step := range plan { |
| 122 | lines = append(lines, step.Detail) |
| 123 | } |
| 124 | all := strings.Join(lines, "\n") |
| 125 | if strings.Contains(all, "SCAN events") || strings.Contains(all, |
| 126 | "SCAN e ") || strings.Contains(all, "SCAN other") || |
| 127 | !strings.Contains(all, "idx_events_actor_page") || |
| 128 | !strings.Contains(all, "INDEX idx_events_at") { |
| 129 | t.Fatalf("the plan:\n%s", all) |
| 130 | } |
| 131 | } |
| 132 | |