|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/githubnext/gh-aw/pkg/workflow" |
| 7 | +) |
| 8 | + |
| 9 | +// Sample log content for benchmarking |
| 10 | +const ( |
| 11 | + sampleClaudeLog = `[{"type":"session_created","timestamp":"2024-01-15T10:00:00.000Z"}] |
| 12 | +[{"type":"message","timestamp":"2024-01-15T10:00:01.000Z","message":"Starting analysis"}] |
| 13 | +[{"type":"tool_use","timestamp":"2024-01-15T10:00:02.000Z","tool":"github.get_issue"}] |
| 14 | +[{"type":"tool_result","timestamp":"2024-01-15T10:00:03.000Z"}] |
| 15 | +[{"type":"usage","timestamp":"2024-01-15T10:00:04.000Z","input_tokens":1000,"output_tokens":500}] |
| 16 | +[{"type":"message","timestamp":"2024-01-15T10:00:05.000Z","message":"Analysis complete"}] |
| 17 | +[{"type":"result","timestamp":"2024-01-15T10:00:06.000Z","total_input_tokens":1000,"total_output_tokens":500,"cost":0.015}]` |
| 18 | + |
| 19 | + sampleCopilotLog = `2024-01-15T10:00:00.123Z [INFO] Copilot started |
| 20 | +2024-01-15T10:00:01.456Z [INFO] Processing request |
| 21 | +2024-01-15T10:00:02.789Z [DEBUG] Tool call: github.get_issue |
| 22 | +2024-01-15T10:00:03.012Z [DEBUG] Tool result received |
| 23 | +2024-01-15T10:00:04.345Z [INFO] Token usage: 1500 total |
| 24 | +2024-01-15T10:00:05.678Z [ERROR] Minor issue detected |
| 25 | +2024-01-15T10:00:06.901Z [INFO] Request completed` |
| 26 | + |
| 27 | + sampleCodexLog = `] tool github.search_issues(...) |
| 28 | +tool result: [{"id": 123, "title": "Issue 1"}] |
| 29 | +] exec ls -la in /tmp |
| 30 | +exec result: total 8 |
| 31 | +] tool github.get_issue(...) |
| 32 | +tool result: {"id": 123, "body": "Issue content"} |
| 33 | +] success in 2.5s` |
| 34 | + |
| 35 | + largeClaudeLog = sampleClaudeLog + "\n" + sampleClaudeLog + "\n" + sampleClaudeLog + "\n" + sampleClaudeLog + "\n" + sampleClaudeLog |
| 36 | + |
| 37 | + largeCopilotLog = sampleCopilotLog + "\n" + sampleCopilotLog + "\n" + sampleCopilotLog + "\n" + sampleCopilotLog + "\n" + sampleCopilotLog |
| 38 | +) |
| 39 | + |
| 40 | +// BenchmarkParseClaudeLog benchmarks Claude log parsing |
| 41 | +func BenchmarkParseClaudeLog(b *testing.B) { |
| 42 | + engine := &workflow.ClaudeEngine{} |
| 43 | + |
| 44 | + b.ResetTimer() |
| 45 | + for i := 0; i < b.N; i++ { |
| 46 | + _ = engine.ParseLogMetrics(sampleClaudeLog, false) |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +// BenchmarkParseClaudeLog_Large benchmarks parsing large Claude log file |
| 51 | +func BenchmarkParseClaudeLog_Large(b *testing.B) { |
| 52 | + engine := &workflow.ClaudeEngine{} |
| 53 | + |
| 54 | + b.ResetTimer() |
| 55 | + for i := 0; i < b.N; i++ { |
| 56 | + _ = engine.ParseLogMetrics(largeClaudeLog, false) |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +// BenchmarkParseCopilotLog benchmarks Copilot log parsing |
| 61 | +func BenchmarkParseCopilotLog(b *testing.B) { |
| 62 | + engine := &workflow.CopilotEngine{} |
| 63 | + |
| 64 | + b.ResetTimer() |
| 65 | + for i := 0; i < b.N; i++ { |
| 66 | + _ = engine.ParseLogMetrics(sampleCopilotLog, false) |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +// BenchmarkParseCopilotLog_Large benchmarks parsing large Copilot log file |
| 71 | +func BenchmarkParseCopilotLog_Large(b *testing.B) { |
| 72 | + engine := &workflow.CopilotEngine{} |
| 73 | + |
| 74 | + b.ResetTimer() |
| 75 | + for i := 0; i < b.N; i++ { |
| 76 | + _ = engine.ParseLogMetrics(largeCopilotLog, false) |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +// BenchmarkParseCodexLog benchmarks Codex log parsing |
| 81 | +func BenchmarkParseCodexLog(b *testing.B) { |
| 82 | + engine := &workflow.CodexEngine{} |
| 83 | + |
| 84 | + b.ResetTimer() |
| 85 | + for i := 0; i < b.N; i++ { |
| 86 | + _ = engine.ParseLogMetrics(sampleCodexLog, false) |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +// BenchmarkParseCodexLog_WithErrors benchmarks Codex log parsing with errors |
| 91 | +func BenchmarkParseCodexLog_WithErrors(b *testing.B) { |
| 92 | + logWithErrors := sampleCodexLog + ` |
| 93 | +] error: connection timeout |
| 94 | +] warning: retry attempt |
| 95 | +] error: max retries exceeded |
| 96 | +] tool github.get_repository(...) |
| 97 | +] success in 1.2s` |
| 98 | + |
| 99 | + engine := &workflow.CodexEngine{} |
| 100 | + |
| 101 | + b.ResetTimer() |
| 102 | + for i := 0; i < b.N; i++ { |
| 103 | + _ = engine.ParseLogMetrics(logWithErrors, false) |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +// BenchmarkAggregateWorkflowStats benchmarks log aggregation across multiple runs |
| 108 | +func BenchmarkAggregateWorkflowStats(b *testing.B) { |
| 109 | + // Create sample workflow runs |
| 110 | + runs := []WorkflowRun{ |
| 111 | + { |
| 112 | + DatabaseID: 12345, |
| 113 | + WorkflowName: "test-workflow-1", |
| 114 | + Status: "completed", |
| 115 | + Conclusion: "success", |
| 116 | + TokenUsage: 1500, |
| 117 | + EstimatedCost: 0.015, |
| 118 | + Turns: 3, |
| 119 | + ErrorCount: 0, |
| 120 | + WarningCount: 1, |
| 121 | + }, |
| 122 | + { |
| 123 | + DatabaseID: 12346, |
| 124 | + WorkflowName: "test-workflow-2", |
| 125 | + Status: "completed", |
| 126 | + Conclusion: "failure", |
| 127 | + TokenUsage: 2500, |
| 128 | + EstimatedCost: 0.025, |
| 129 | + Turns: 5, |
| 130 | + ErrorCount: 2, |
| 131 | + WarningCount: 3, |
| 132 | + }, |
| 133 | + { |
| 134 | + DatabaseID: 12347, |
| 135 | + WorkflowName: "test-workflow-1", |
| 136 | + Status: "completed", |
| 137 | + Conclusion: "success", |
| 138 | + TokenUsage: 1800, |
| 139 | + EstimatedCost: 0.018, |
| 140 | + Turns: 4, |
| 141 | + ErrorCount: 0, |
| 142 | + WarningCount: 0, |
| 143 | + }, |
| 144 | + } |
| 145 | + |
| 146 | + b.ResetTimer() |
| 147 | + for i := 0; i < b.N; i++ { |
| 148 | + // Simulate aggregation logic |
| 149 | + totalTokens := 0 |
| 150 | + totalCost := 0.0 |
| 151 | + totalTurns := 0 |
| 152 | + totalErrors := 0 |
| 153 | + totalWarnings := 0 |
| 154 | + |
| 155 | + for _, run := range runs { |
| 156 | + totalTokens += run.TokenUsage |
| 157 | + totalCost += run.EstimatedCost |
| 158 | + totalTurns += run.Turns |
| 159 | + totalErrors += run.ErrorCount |
| 160 | + totalWarnings += run.WarningCount |
| 161 | + } |
| 162 | + |
| 163 | + _ = totalTokens |
| 164 | + _ = totalCost |
| 165 | + _ = totalTurns |
| 166 | + _ = totalErrors |
| 167 | + _ = totalWarnings |
| 168 | + } |
| 169 | +} |
| 170 | + |
| 171 | +// BenchmarkAggregateWorkflowStats_Large benchmarks aggregation with many runs |
| 172 | +func BenchmarkAggregateWorkflowStats_Large(b *testing.B) { |
| 173 | + // Create 100 sample workflow runs |
| 174 | + runs := make([]WorkflowRun, 100) |
| 175 | + for i := 0; i < 100; i++ { |
| 176 | + runs[i] = WorkflowRun{ |
| 177 | + DatabaseID: int64(12345 + i), |
| 178 | + WorkflowName: "test-workflow", |
| 179 | + Status: "completed", |
| 180 | + Conclusion: "success", |
| 181 | + TokenUsage: 1500 + i*10, |
| 182 | + EstimatedCost: 0.015 + float64(i)*0.001, |
| 183 | + Turns: 3 + i%5, |
| 184 | + ErrorCount: i % 3, |
| 185 | + WarningCount: i % 2, |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + b.ResetTimer() |
| 190 | + for i := 0; i < b.N; i++ { |
| 191 | + totalTokens := 0 |
| 192 | + totalCost := 0.0 |
| 193 | + totalTurns := 0 |
| 194 | + totalErrors := 0 |
| 195 | + totalWarnings := 0 |
| 196 | + |
| 197 | + for _, run := range runs { |
| 198 | + totalTokens += run.TokenUsage |
| 199 | + totalCost += run.EstimatedCost |
| 200 | + totalTurns += run.Turns |
| 201 | + totalErrors += run.ErrorCount |
| 202 | + totalWarnings += run.WarningCount |
| 203 | + } |
| 204 | + |
| 205 | + _ = totalTokens |
| 206 | + _ = totalCost |
| 207 | + _ = totalTurns |
| 208 | + _ = totalErrors |
| 209 | + _ = totalWarnings |
| 210 | + } |
| 211 | +} |
| 212 | + |
| 213 | +// BenchmarkExtractJSONMetrics benchmarks JSON metrics extraction |
| 214 | +func BenchmarkExtractJSONMetrics(b *testing.B) { |
| 215 | + jsonLine := `{"type":"usage","input_tokens":1000,"output_tokens":500,"cost":0.015}` |
| 216 | + |
| 217 | + b.ResetTimer() |
| 218 | + for i := 0; i < b.N; i++ { |
| 219 | + _ = workflow.ExtractJSONMetrics(jsonLine, false) |
| 220 | + } |
| 221 | +} |
| 222 | + |
| 223 | +// BenchmarkExtractJSONMetrics_Complex benchmarks complex JSON metrics extraction |
| 224 | +func BenchmarkExtractJSONMetrics_Complex(b *testing.B) { |
| 225 | + jsonLine := `{"type":"result","total_input_tokens":5000,"total_output_tokens":2500,"cost":0.075,"metadata":{"tool_calls":["github.get_issue","github.add_comment"],"duration_ms":1500}}` |
| 226 | + |
| 227 | + b.ResetTimer() |
| 228 | + for i := 0; i < b.N; i++ { |
| 229 | + _ = workflow.ExtractJSONMetrics(jsonLine, false) |
| 230 | + } |
| 231 | +} |
0 commit comments