|
| 1 | +package github |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "sync/atomic" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/github/github-mcp-server/pkg/inventory" |
| 10 | + "github.com/github/github-mcp-server/pkg/translations" |
| 11 | + "github.com/modelcontextprotocol/go-sdk/mcp" |
| 12 | +) |
| 13 | + |
| 14 | +func BenchmarkFeatureInventory(b *testing.B) { |
| 15 | + for _, distribution := range featureBenchmarkDistributions() { |
| 16 | + b.Run(distribution.name, func(b *testing.B) { |
| 17 | + b.Run("build", func(b *testing.B) { |
| 18 | + var calls atomic.Int64 |
| 19 | + b.ReportAllocs() |
| 20 | + for b.Loop() { |
| 21 | + _, err := featureBenchmarkBuilder(distribution, &calls).Build() |
| 22 | + if err != nil { |
| 23 | + b.Fatal(err) |
| 24 | + } |
| 25 | + } |
| 26 | + b.ReportMetric(float64(calls.Load())/float64(b.N), "checks/op") |
| 27 | + }) |
| 28 | + |
| 29 | + builder := featureBenchmarkBuilder(distribution, nil) |
| 30 | + b.Run("preconstructed-builder", func(b *testing.B) { |
| 31 | + b.ReportAllocs() |
| 32 | + for b.Loop() { |
| 33 | + if _, err := builder.Build(); err != nil { |
| 34 | + b.Fatal(err) |
| 35 | + } |
| 36 | + } |
| 37 | + }) |
| 38 | + |
| 39 | + b.Run("tools-list", func(b *testing.B) { |
| 40 | + inv, calls := featureBenchmarkInventory(b, distribution) |
| 41 | + b.ReportAllocs() |
| 42 | + b.ResetTimer() |
| 43 | + for b.Loop() { |
| 44 | + _ = inv.ForMCPRequest(inventory.MCPMethodToolsList, "").ToolsForRegistration(context.Background()) |
| 45 | + } |
| 46 | + b.ReportMetric(float64(calls.Load())/float64(b.N), "checks/op") |
| 47 | + }) |
| 48 | + |
| 49 | + b.Run("unflagged-tool-call", func(b *testing.B) { |
| 50 | + inv, calls := featureBenchmarkInventory(b, distribution) |
| 51 | + b.ReportAllocs() |
| 52 | + b.ResetTimer() |
| 53 | + for b.Loop() { |
| 54 | + _ = inv.ForMCPRequest(inventory.MCPMethodToolsCall, "get_commit").ToolsForRegistration(context.Background()) |
| 55 | + } |
| 56 | + b.ReportMetric(float64(calls.Load())/float64(b.N), "checks/op") |
| 57 | + }) |
| 58 | + |
| 59 | + b.Run("gated-tool-call", func(b *testing.B) { |
| 60 | + inv, calls := featureBenchmarkInventory(b, distribution) |
| 61 | + b.ReportAllocs() |
| 62 | + b.ResetTimer() |
| 63 | + for b.Loop() { |
| 64 | + _ = inv.ForMCPRequest(inventory.MCPMethodToolsCall, "get_file_blame").ToolsForRegistration(context.Background()) |
| 65 | + } |
| 66 | + b.ReportMetric(float64(calls.Load())/float64(b.N), "checks/op") |
| 67 | + }) |
| 68 | + |
| 69 | + b.Run("ui-tool-call", func(b *testing.B) { |
| 70 | + inv, calls := featureBenchmarkInventory(b, distribution) |
| 71 | + b.ReportAllocs() |
| 72 | + b.ResetTimer() |
| 73 | + for b.Loop() { |
| 74 | + _ = inv.ForMCPRequest(inventory.MCPMethodToolsCall, "ui_get").ToolsForRegistration(context.Background()) |
| 75 | + } |
| 76 | + b.ReportMetric(float64(calls.Load())/float64(b.N), "checks/op") |
| 77 | + }) |
| 78 | + |
| 79 | + b.Run("direct-handler-checks", func(b *testing.B) { |
| 80 | + var calls atomic.Int64 |
| 81 | + checker := func(_ context.Context, flag string) (bool, error) { |
| 82 | + calls.Add(1) |
| 83 | + return distribution.enabled["*"] || distribution.enabled[flag], nil |
| 84 | + } |
| 85 | + b.ReportAllocs() |
| 86 | + for b.Loop() { |
| 87 | + ctx := inventory.WithFeatureState(context.Background(), checker) |
| 88 | + _ = inventory.ResolveFeature(ctx, checker, inventory.FeatureFlag(FeatureFlagCSVOutput)) |
| 89 | + _ = inventory.ResolveFeature(ctx, checker, inventory.FeatureFlag(FeatureFlagCSVOutput)) |
| 90 | + } |
| 91 | + b.ReportMetric(float64(calls.Load())/float64(b.N), "checks/op") |
| 92 | + }) |
| 93 | + |
| 94 | + b.Run("build-list-register", func(b *testing.B) { |
| 95 | + var calls atomic.Int64 |
| 96 | + b.ReportAllocs() |
| 97 | + for b.Loop() { |
| 98 | + inv, err := featureBenchmarkBuilder(distribution, &calls).Build() |
| 99 | + if err != nil { |
| 100 | + b.Fatal(err) |
| 101 | + } |
| 102 | + inv = inv.ForMCPRequest(inventory.MCPMethodToolsList, "") |
| 103 | + server := mcp.NewServer(&mcp.Implementation{Name: "benchmark", Version: "v0"}, nil) |
| 104 | + inv.RegisterAll(context.Background(), server, nil) |
| 105 | + } |
| 106 | + b.ReportMetric(float64(calls.Load())/float64(b.N), "checks/op") |
| 107 | + }) |
| 108 | + }) |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +type featureBenchmarkDistribution struct { |
| 113 | + name string |
| 114 | + enabled map[string]bool |
| 115 | +} |
| 116 | + |
| 117 | +func featureBenchmarkDistributions() []featureBenchmarkDistribution { |
| 118 | + return []featureBenchmarkDistribution{ |
| 119 | + {name: "all-false"}, |
| 120 | + { |
| 121 | + name: "mixed", |
| 122 | + enabled: map[string]bool{ |
| 123 | + MCPAppsFeatureFlag: true, |
| 124 | + FeatureFlagFileBlame: true, |
| 125 | + FeatureFlagIssuesGranular: true, |
| 126 | + FeatureFlagIssueDependencies: true, |
| 127 | + }, |
| 128 | + }, |
| 129 | + {name: "all-true", enabled: map[string]bool{"*": true}}, |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +func featureBenchmarkBuilder(distribution featureBenchmarkDistribution, calls *atomic.Int64) *inventory.Builder { |
| 134 | + checker := func(_ context.Context, flag string) (bool, error) { |
| 135 | + if calls != nil { |
| 136 | + calls.Add(1) |
| 137 | + } |
| 138 | + return distribution.enabled["*"] || distribution.enabled[flag], nil |
| 139 | + } |
| 140 | + tools := AllTools(translations.NullTranslationHelper) |
| 141 | + for i, baseCount := 0, len(tools); len(tools) < 139; i++ { |
| 142 | + tool := tools[i%baseCount] |
| 143 | + tool.Tool.Name = fmt.Sprintf("%s_remote_%d", tool.Tool.Name, i) |
| 144 | + tools = append(tools, tool) |
| 145 | + } |
| 146 | + return inventory.NewBuilder(). |
| 147 | + SetTools(tools). |
| 148 | + SetResources(AllResources(translations.NullTranslationHelper)). |
| 149 | + SetPrompts(AllPrompts(translations.NullTranslationHelper)). |
| 150 | + WithToolsets([]string{"all"}). |
| 151 | + WithFeatureChecker(checker) |
| 152 | +} |
| 153 | + |
| 154 | +func featureBenchmarkInventory(b *testing.B, distribution featureBenchmarkDistribution) (*inventory.Inventory, *atomic.Int64) { |
| 155 | + b.Helper() |
| 156 | + var calls atomic.Int64 |
| 157 | + inv, err := featureBenchmarkBuilder(distribution, &calls).Build() |
| 158 | + if err != nil { |
| 159 | + b.Fatal(err) |
| 160 | + } |
| 161 | + return inv, &calls |
| 162 | +} |
0 commit comments