|
| 1 | +package graphstore |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/alibaba/UnifiedModel/pkg/model" |
| 9 | +) |
| 10 | + |
| 11 | +// seedMemoryStore fills a workspace with n entities and n relations (a ring) so |
| 12 | +// the scan paths have realistic work to do. |
| 13 | +func seedMemoryStore(tb testing.TB, store *MemoryStore, n int) { |
| 14 | + tb.Helper() |
| 15 | + ctx := context.Background() |
| 16 | + ents := make([]model.EntityPayload, n) |
| 17 | + rels := make([]model.RelationPayload, n) |
| 18 | + for i := 0; i < n; i++ { |
| 19 | + id := fmt.Sprintf("%032x", i+1) |
| 20 | + ents[i] = entityPayload(id, "Update", 100, 200, map[string]any{"display_name": fmt.Sprintf("svc-%d", i)}) |
| 21 | + } |
| 22 | + if _, err := store.WriteEntities(ctx, model.EntityWriteBatch{Workspace: "demo", Entities: ents}); err != nil { |
| 23 | + tb.Fatalf("seed entities: %v", err) |
| 24 | + } |
| 25 | + for i := 0; i < n; i++ { |
| 26 | + src := fmt.Sprintf("%032x", i+1) |
| 27 | + dst := fmt.Sprintf("%032x", (i+1)%n+1) |
| 28 | + rels[i] = relationPayload(src, dst, "Update", 100, 200, nil) |
| 29 | + } |
| 30 | + if _, err := store.WriteRelations(ctx, model.RelationWriteBatch{Workspace: "demo", Relations: rels}); err != nil { |
| 31 | + tb.Fatalf("seed relations: %v", err) |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +func BenchmarkQueryEntities(b *testing.B) { |
| 36 | + store := NewMemoryStore() |
| 37 | + seedMemoryStore(b, store, 2000) |
| 38 | + ctx := context.Background() |
| 39 | + plan := model.EntityQueryPlan{Workspace: "demo", Limit: 1000} |
| 40 | + b.ReportAllocs() |
| 41 | + b.ResetTimer() |
| 42 | + for i := 0; i < b.N; i++ { |
| 43 | + if _, err := store.QueryEntities(ctx, plan); err != nil { |
| 44 | + b.Fatal(err) |
| 45 | + } |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +func BenchmarkQueryTopo(b *testing.B) { |
| 50 | + store := NewMemoryStore() |
| 51 | + seedMemoryStore(b, store, 2000) |
| 52 | + ctx := context.Background() |
| 53 | + plan := model.TopoQueryPlan{Workspace: "demo", Limit: 1000} |
| 54 | + b.ReportAllocs() |
| 55 | + b.ResetTimer() |
| 56 | + for i := 0; i < b.N; i++ { |
| 57 | + if _, err := store.QueryTopo(ctx, plan); err != nil { |
| 58 | + b.Fatal(err) |
| 59 | + } |
| 60 | + } |
| 61 | +} |
0 commit comments