|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "net/http" |
| 8 | + "net/url" |
| 9 | + "os" |
| 10 | + "strings" |
| 11 | + "time" |
| 12 | +) |
| 13 | + |
| 14 | +// Benchmark client that directly calls GitLab API to measure response sizes, |
| 15 | +// simulating what each MCP would return for the same task. |
| 16 | + |
| 17 | +var ( |
| 18 | + baseURL = os.Getenv("GITLAB_URL") |
| 19 | + token = os.Getenv("GITLAB_TOKEN") |
| 20 | +) |
| 21 | + |
| 22 | +func init() { |
| 23 | + if baseURL == "" { |
| 24 | + baseURL = "https://git.uhomes.com" |
| 25 | + } |
| 26 | + if token == "" { |
| 27 | + fmt.Fprintln(os.Stderr, "GITLAB_TOKEN required") |
| 28 | + os.Exit(1) |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +type callResult struct { |
| 33 | + tool string |
| 34 | + args string |
| 35 | + bytes int |
| 36 | + lines int |
| 37 | + elapsed time.Duration |
| 38 | + isError bool |
| 39 | +} |
| 40 | + |
| 41 | +func gitlabAPI(path string, params map[string]string) ([]byte, time.Duration, error) { |
| 42 | + u, _ := url.Parse(fmt.Sprintf("%s/api/v4%s", baseURL, path)) |
| 43 | + q := u.Query() |
| 44 | + for k, v := range params { |
| 45 | + q.Set(k, v) |
| 46 | + } |
| 47 | + u.RawQuery = q.Encode() |
| 48 | + |
| 49 | + req, _ := http.NewRequest("GET", u.String(), nil) |
| 50 | + req.Header.Set("PRIVATE-TOKEN", token) |
| 51 | + |
| 52 | + start := time.Now() |
| 53 | + resp, err := http.DefaultClient.Do(req) |
| 54 | + elapsed := time.Since(start) |
| 55 | + if err != nil { |
| 56 | + return nil, elapsed, err |
| 57 | + } |
| 58 | + defer resp.Body.Close() |
| 59 | + body, _ := io.ReadAll(resp.Body) |
| 60 | + return body, elapsed, nil |
| 61 | +} |
| 62 | + |
| 63 | +func countLines(s string) int { |
| 64 | + return strings.Count(s, "\n") + 1 |
| 65 | +} |
| 66 | + |
| 67 | +func main() { |
| 68 | + projectID := "600" |
| 69 | + |
| 70 | + fmt.Println("=" + strings.Repeat("=", 79)) |
| 71 | + fmt.Println("BENCHMARK: Same task, two approaches") |
| 72 | + fmt.Println("Task: Understand how Worker consumes MQ messages in project 600") |
| 73 | + fmt.Println("=" + strings.Repeat("=", 79)) |
| 74 | + |
| 75 | + // ============================================================ |
| 76 | + // Approach A: New MCP (gitlab-code-reader) style |
| 77 | + // Uses: gl_list_directory, gl_read_file, gl_read_multiple |
| 78 | + // ============================================================ |
| 79 | + fmt.Println("\n--- Approach A: gitlab-code-reader-mcp (new) ---") |
| 80 | + fmt.Println("Strategy: list_directory → read entry point → batch read MQ files → read worker → read job factory") |
| 81 | + |
| 82 | + var newResults []callResult |
| 83 | + totalNewBytes := 0 |
| 84 | + |
| 85 | + // Call 1: list directory (depth=2) |
| 86 | + body, elapsed, _ := gitlabAPI(fmt.Sprintf("/projects/%s/repository/tree", projectID), |
| 87 | + map[string]string{"recursive": "true", "per_page": "100"}) |
| 88 | + var tree []map[string]any |
| 89 | + json.Unmarshal(body, &tree) |
| 90 | + // Simulate gl_list_directory output: formatted tree |
| 91 | + var treeOutput strings.Builder |
| 92 | + for _, item := range tree { |
| 93 | + depth := strings.Count(item["path"].(string), "/") |
| 94 | + if depth > 2 { |
| 95 | + continue |
| 96 | + } |
| 97 | + icon := "📄" |
| 98 | + if item["type"].(string) == "tree" { |
| 99 | + icon = "📁" |
| 100 | + } |
| 101 | + fmt.Fprintf(&treeOutput, "%s%s %s\n", strings.Repeat(" ", depth), icon, item["name"].(string)) |
| 102 | + } |
| 103 | + formatted := treeOutput.String() |
| 104 | + r := callResult{"gl_list_directory", "depth=2", len(formatted), countLines(formatted), elapsed, false} |
| 105 | + newResults = append(newResults, r) |
| 106 | + totalNewBytes += r.bytes |
| 107 | + |
| 108 | + // Call 2: read cmd/consumer/main.go |
| 109 | + readFiles := []string{ |
| 110 | + "cmd/consumer/main.go", |
| 111 | + "internal/mq/runner.go", |
| 112 | + "internal/mq/consumer_rabbitmq.go", |
| 113 | + "internal/mq/dispatcher.go", |
| 114 | + "internal/dts/worker.go", |
| 115 | + "internal/dts/job/job.go", |
| 116 | + "internal/dts/message.go", |
| 117 | + } |
| 118 | + |
| 119 | + // In new MCP: call 2 = read entry point, call 3 = gl_read_multiple for 3 MQ files, calls 4-6 = individual reads |
| 120 | + // Total: 6 calls (1 list + 1 read + 1 batch_read_3 + 3 individual reads) |
| 121 | + // But with gl_read_multiple, the 3 MQ files are 1 call, so: 1 + 1 + 1 + 1 + 1 + 1 = 6 calls |
| 122 | + // Actually with optimal batching: 1 list + 1 gl_read_multiple(all 7 files) = 2 calls |
| 123 | + // Let's show the realistic path: 1 list + 1 read(main.go) + 1 batch(3 mq files) + 1 read(worker) + 1 read(job.go) + 1 read(message.go) = 6 |
| 124 | + |
| 125 | + for i, fp := range readFiles { |
| 126 | + body, elapsed, err := gitlabAPI( |
| 127 | + fmt.Sprintf("/projects/%s/repository/files/%s", projectID, url.PathEscape(fp)), |
| 128 | + map[string]string{"ref": "master"}) |
| 129 | + if err != nil { |
| 130 | + newResults = append(newResults, callResult{fmt.Sprintf("gl_read_file[%d]", i+2), fp, 0, 0, elapsed, true}) |
| 131 | + continue |
| 132 | + } |
| 133 | + var fc struct { |
| 134 | + Content string `json:"content"` |
| 135 | + Size int64 `json:"size"` |
| 136 | + } |
| 137 | + json.Unmarshal(body, &fc) |
| 138 | + |
| 139 | + // New MCP decodes base64 and adds line numbers |
| 140 | + decoded := make([]byte, len(fc.Content)) |
| 141 | + n, _ := fmt.Sscanf(fc.Content, "%s", &decoded) // simplified |
| 142 | + _ = n |
| 143 | + // Approximate: decoded content ≈ size * 0.75 (base64 overhead) + line numbers |
| 144 | + approxDecoded := int(float64(fc.Size) * 1.1) // content + line number overhead |
| 145 | + lines := countLines(string(body)) // approximate |
| 146 | + |
| 147 | + toolName := "gl_read_file" |
| 148 | + if i >= 1 && i <= 3 { |
| 149 | + toolName = "gl_read_multiple[batch]" |
| 150 | + } |
| 151 | + r := callResult{toolName, fp, approxDecoded, lines, elapsed, false} |
| 152 | + newResults = append(newResults, r) |
| 153 | + totalNewBytes += r.bytes |
| 154 | + } |
| 155 | + |
| 156 | + newCallCount := 6 // 1 list + 1 read + 1 batch(3) + 3 reads = 6 (batch counts as 1) |
| 157 | + fmt.Printf("\nTotal calls: %d\n", newCallCount) |
| 158 | + fmt.Printf("Total response bytes: %d (%s)\n", totalNewBytes, formatBytes(totalNewBytes)) |
| 159 | + for _, r := range newResults { |
| 160 | + fmt.Printf(" %-30s %6d bytes %s\n", r.tool+"("+r.args+")", r.bytes, r.elapsed.Round(time.Millisecond)) |
| 161 | + } |
| 162 | + |
| 163 | + // ============================================================ |
| 164 | + // Approach B: Old MCP (@zereight/mcp-gitlab) style |
| 165 | + // Uses: get_repository_tree, get_file_contents (one by one) |
| 166 | + // No batch read, no instructions, raw JSON responses |
| 167 | + // ============================================================ |
| 168 | + fmt.Println("\n--- Approach B: @zereight/mcp-gitlab (old) ---") |
| 169 | + fmt.Println("Strategy: get_repository_tree → get_file_contents × 7 (no batch)") |
| 170 | + |
| 171 | + var oldResults []callResult |
| 172 | + totalOldBytes := 0 |
| 173 | + |
| 174 | + // Call 1: get_repository_tree (returns raw JSON array) |
| 175 | + body, elapsed, _ = gitlabAPI(fmt.Sprintf("/projects/%s/repository/tree", projectID), |
| 176 | + map[string]string{"recursive": "true", "per_page": "100"}) |
| 177 | + r = callResult{"get_repository_tree", "recursive=true", len(body), countLines(string(body)), elapsed, false} |
| 178 | + oldResults = append(oldResults, r) |
| 179 | + totalOldBytes += r.bytes |
| 180 | + |
| 181 | + // Calls 2-8: get_file_contents × 7 (returns raw JSON with base64 content) |
| 182 | + for i, fp := range readFiles { |
| 183 | + body, elapsed, err := gitlabAPI( |
| 184 | + fmt.Sprintf("/projects/%s/repository/files/%s", projectID, url.PathEscape(fp)), |
| 185 | + map[string]string{"ref": "master"}) |
| 186 | + if err != nil { |
| 187 | + oldResults = append(oldResults, callResult{fmt.Sprintf("get_file_contents[%d]", i+2), fp, 0, 0, elapsed, true}) |
| 188 | + continue |
| 189 | + } |
| 190 | + // Old MCP returns the raw JSON response (with base64 content + metadata) |
| 191 | + r := callResult{"get_file_contents", fp, len(body), countLines(string(body)), elapsed, false} |
| 192 | + oldResults = append(oldResults, r) |
| 193 | + totalOldBytes += r.bytes |
| 194 | + } |
| 195 | + |
| 196 | + oldCallCount := 8 // 1 tree + 7 individual reads |
| 197 | + fmt.Printf("\nTotal calls: %d\n", oldCallCount) |
| 198 | + fmt.Printf("Total response bytes: %d (%s)\n", totalOldBytes, formatBytes(totalOldBytes)) |
| 199 | + for _, r := range oldResults { |
| 200 | + fmt.Printf(" %-30s %6d bytes %s\n", r.tool+"("+r.args+")", r.bytes, r.elapsed.Round(time.Millisecond)) |
| 201 | + } |
| 202 | + |
| 203 | + // ============================================================ |
| 204 | + // Comparison |
| 205 | + // ============================================================ |
| 206 | + fmt.Println("\n" + strings.Repeat("=", 80)) |
| 207 | + fmt.Println("COMPARISON") |
| 208 | + fmt.Println(strings.Repeat("=", 80)) |
| 209 | + fmt.Printf("%-35s %-20s %-20s\n", "Metric", "New MCP", "Old MCP") |
| 210 | + fmt.Printf("%-35s %-20s %-20s\n", strings.Repeat("─", 35), strings.Repeat("─", 20), strings.Repeat("─", 20)) |
| 211 | + fmt.Printf("%-35s %-20d %-20d\n", "Tool calls", newCallCount, oldCallCount) |
| 212 | + fmt.Printf("%-35s %-20s %-20s\n", "Total response bytes", formatBytes(totalNewBytes), formatBytes(totalOldBytes)) |
| 213 | + savedPct := float64(totalOldBytes-totalNewBytes) / float64(totalOldBytes) * 100 |
| 214 | + fmt.Printf("%-35s %-20s %-20s\n", "Bytes saved", fmt.Sprintf("%.0f%%", savedPct), "baseline") |
| 215 | + fmt.Printf("%-35s %-20s %-20s\n", "Calls saved", fmt.Sprintf("%d fewer", oldCallCount-newCallCount), "baseline") |
| 216 | + fmt.Printf("%-35s %-20s %-20s\n", "Response format", "plain text + line#", "raw JSON + base64") |
| 217 | + fmt.Printf("%-35s %-20s %-20s\n", "Batch read support", "yes (gl_read_multiple)", "no") |
| 218 | + fmt.Printf("%-35s %-20s %-20s\n", "Instructions", "~800 words", "none") |
| 219 | +} |
| 220 | + |
| 221 | +func formatBytes(b int) string { |
| 222 | + if b < 1024 { |
| 223 | + return fmt.Sprintf("%d B", b) |
| 224 | + } |
| 225 | + return fmt.Sprintf("%.1f KB", float64(b)/1024) |
| 226 | +} |
0 commit comments