| 1 | package workspacestate |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "fmt" |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | ) |
| 11 | |
| 12 | // A settled registry can have much larger completed journals than display data. |
| 13 | func BenchmarkRegistryRead(b *testing.B) { |
| 14 | state := newState() |
| 15 | for i := range 150 { |
| 16 | id := fmt.Sprintf("session-%d", i) |
| 17 | state.Presentation[id] = Presentation{Title: id} |
| 18 | state.PendingOperations[id] = Operation{ID: id, Lifecycle: "active", Kind: "import", Phase: "committed", Request: json.RawMessage(`{"payload":"` + strings.Repeat("x", 5000) + `"}`)} |
| 19 | } |
| 20 | body, err := json.Marshal(state) |
| 21 | if err != nil { |
| 22 | b.Fatal(err) |
| 23 | } |
| 24 | path := filepath.Join(b.TempDir(), "registry.json") |
| 25 | if err := os.WriteFile(path, body, 0600); err != nil { |
| 26 | b.Fatal(err) |
| 27 | } |
| 28 | for _, mode := range []string{"cold", "warm-full", "warm-projection"} { |
| 29 | b.Run(mode, func(b *testing.B) { |
| 30 | store := NewStore(path) |
| 31 | if _, err := store.Load(b.Context()); err != nil { |
| 32 | b.Fatal(err) |
| 33 | } |
| 34 | b.ReportAllocs() |
| 35 | b.ResetTimer() |
| 36 | for b.Loop() { |
| 37 | if mode == "cold" { |
| 38 | store = NewStore(path) |
| 39 | } |
| 40 | var err error |
| 41 | if mode == "warm-projection" { |
| 42 | _, err = store.LoadProjection(b.Context()) |
| 43 | } else { |
| 44 | _, err = store.Load(b.Context()) |
| 45 | } |
| 46 | if err != nil { |
| 47 | b.Fatal(err) |
| 48 | } |
| 49 | } |
| 50 | }) |
| 51 | } |
| 52 | } |
| 53 |