|
| 1 | +package codemode |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/universal-tool-calling-protocol/go-utcp/src/tools" |
| 8 | +) |
| 9 | + |
| 10 | +// Benchmark tool specs without cache |
| 11 | +func BenchmarkToolSpecs_NoCache(b *testing.B) { |
| 12 | + mock := &mockUTCP{ |
| 13 | + searchToolsFn: func(query string, limit int) ([]tools.Tool, error) { |
| 14 | + // Simulate SearchTools returning a large list |
| 15 | + result := make([]tools.Tool, 50) |
| 16 | + for i := 0; i < 50; i++ { |
| 17 | + result[i] = tools.Tool{ |
| 18 | + Name: "test.tool" + string(rune(i)), |
| 19 | + Description: "Test tool " + string(rune(i)), |
| 20 | + } |
| 21 | + } |
| 22 | + return result, nil |
| 23 | + }, |
| 24 | + } |
| 25 | + |
| 26 | + mockModel := &mockModel{ |
| 27 | + GenerateFunc: func(ctx context.Context, prompt string) (any, error) { |
| 28 | + return "{}", nil |
| 29 | + }, |
| 30 | + } |
| 31 | + |
| 32 | + cm := NewCodeModeUTCP(mock, mockModel) |
| 33 | + // Disable cache to benchmark without caching |
| 34 | + cm.cache = nil |
| 35 | + |
| 36 | + b.ResetTimer() |
| 37 | + for i := 0; i < b.N; i++ { |
| 38 | + _ = cm.ToolSpecs() |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +// Benchmark tool specs with cache (first call - miss) |
| 43 | +func BenchmarkToolSpecs_WithCache_Miss(b *testing.B) { |
| 44 | + mock := &mockUTCP{ |
| 45 | + searchToolsFn: func(query string, limit int) ([]tools.Tool, error) { |
| 46 | + result := make([]tools.Tool, 50) |
| 47 | + for i := 0; i < 50; i++ { |
| 48 | + result[i] = tools.Tool{ |
| 49 | + Name: "test.tool" + string(rune(i)), |
| 50 | + Description: "Test tool " + string(rune(i)), |
| 51 | + } |
| 52 | + } |
| 53 | + return result, nil |
| 54 | + }, |
| 55 | + } |
| 56 | + |
| 57 | + mockModel := &mockModel{ |
| 58 | + GenerateFunc: func(ctx context.Context, prompt string) (any, error) { |
| 59 | + return "{}", nil |
| 60 | + }, |
| 61 | + } |
| 62 | + |
| 63 | + b.ResetTimer() |
| 64 | + for i := 0; i < b.N; i++ { |
| 65 | + b.StopTimer() |
| 66 | + cm := NewCodeModeUTCP(mock, mockModel) |
| 67 | + b.StartTimer() |
| 68 | + _ = cm.ToolSpecs() |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +// Benchmark tool specs with cache (subsequent calls - hits) |
| 73 | +func BenchmarkToolSpecs_WithCache_Hit(b *testing.B) { |
| 74 | + mock := &mockUTCP{ |
| 75 | + searchToolsFn: func(query string, limit int) ([]tools.Tool, error) { |
| 76 | + result := make([]tools.Tool, 50) |
| 77 | + for i := 0; i < 50; i++ { |
| 78 | + result[i] = tools.Tool{ |
| 79 | + Name: "test.tool" + string(rune(i)), |
| 80 | + Description: "Test tool " + string(rune(i)), |
| 81 | + } |
| 82 | + } |
| 83 | + return result, nil |
| 84 | + }, |
| 85 | + } |
| 86 | + |
| 87 | + mockModel := &mockModel{ |
| 88 | + GenerateFunc: func(ctx context.Context, prompt string) (any, error) { |
| 89 | + return "{}", nil |
| 90 | + }, |
| 91 | + } |
| 92 | + |
| 93 | + cm := NewCodeModeUTCP(mock, mockModel) |
| 94 | + // Warm up cache |
| 95 | + _ = cm.ToolSpecs() |
| 96 | + |
| 97 | + b.ResetTimer() |
| 98 | + for i := 0; i < b.N; i++ { |
| 99 | + _ = cm.ToolSpecs() |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +// Benchmark selectTools without cache |
| 104 | +func BenchmarkSelectTools_NoCache(b *testing.B) { |
| 105 | + mock := &mockUTCP{} |
| 106 | + mockModel := &mockModel{ |
| 107 | + GenerateFunc: func(ctx context.Context, prompt string) (any, error) { |
| 108 | + return `{"tools": ["test.tool1", "test.tool2"]}`, nil |
| 109 | + }, |
| 110 | + } |
| 111 | + |
| 112 | + cm := NewCodeModeUTCP(mock, mockModel) |
| 113 | + // Disable cache |
| 114 | + cm.cache = nil |
| 115 | + |
| 116 | + ctx := context.Background() |
| 117 | + query := "find memory tools" |
| 118 | + toolsStr := "test.tool1, test.tool2, test.tool3" |
| 119 | + |
| 120 | + b.ResetTimer() |
| 121 | + for i := 0; i < b.N; i++ { |
| 122 | + _, _ = cm.selectTools(ctx, query, toolsStr) |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +// Benchmark selectTools with cache (first call - miss) |
| 127 | +func BenchmarkSelectTools_WithCache_Miss(b *testing.B) { |
| 128 | + mock := &mockUTCP{} |
| 129 | + mockModel := &mockModel{ |
| 130 | + GenerateFunc: func(ctx context.Context, prompt string) (any, error) { |
| 131 | + return `{"tools": ["test.tool1", "test.tool2"]}`, nil |
| 132 | + }, |
| 133 | + } |
| 134 | + |
| 135 | + ctx := context.Background() |
| 136 | + query := "find memory tools" |
| 137 | + toolsStr := "test.tool1, test.tool2, test.tool3" |
| 138 | + |
| 139 | + b.ResetTimer() |
| 140 | + for i := 0; i < b.N; i++ { |
| 141 | + b.StopTimer() |
| 142 | + cm := NewCodeModeUTCP(mock, mockModel) |
| 143 | + b.StartTimer() |
| 144 | + _, _ = cm.selectTools(ctx, query, toolsStr) |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +// Benchmark selectTools with cache (subsequent calls - hits) |
| 149 | +func BenchmarkSelectTools_WithCache_Hit(b *testing.B) { |
| 150 | + mock := &mockUTCP{} |
| 151 | + mockModel := &mockModel{ |
| 152 | + GenerateFunc: func(ctx context.Context, prompt string) (any, error) { |
| 153 | + return `{"tools": ["test.tool1", "test.tool2"]}`, nil |
| 154 | + }, |
| 155 | + } |
| 156 | + |
| 157 | + cm := NewCodeModeUTCP(mock, mockModel) |
| 158 | + |
| 159 | + ctx := context.Background() |
| 160 | + query := "find memory tools" |
| 161 | + toolsStr := "test.tool1, test.tool2, test.tool3" |
| 162 | + |
| 163 | + // Warm up cache |
| 164 | + _, _ = cm.selectTools(ctx, query, toolsStr) |
| 165 | + |
| 166 | + b.ResetTimer() |
| 167 | + for i := 0; i < b.N; i++ { |
| 168 | + _, _ = cm.selectTools(ctx, query, toolsStr) |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | +// Benchmark full CallTool workflow with cache |
| 173 | +func BenchmarkCallTool_WithCache(b *testing.B) { |
| 174 | + mock := &mockUTCP{ |
| 175 | + searchToolsFn: func(query string, limit int) ([]tools.Tool, error) { |
| 176 | + return []tools.Tool{ |
| 177 | + {Name: "test.tool1", Description: "Test tool 1"}, |
| 178 | + {Name: "test.tool2", Description: "Test tool 2"}, |
| 179 | + }, nil |
| 180 | + }, |
| 181 | + } |
| 182 | + |
| 183 | + mockModel := &mockModel{ |
| 184 | + GenerateFunc: func(ctx context.Context, prompt string) (any, error) { |
| 185 | + // Simulate different responses for different stages |
| 186 | + if stringContains(prompt, "Decide if") { |
| 187 | + return `{"needs": true}`, nil |
| 188 | + } |
| 189 | + if stringContains(prompt, "Select ALL UTCP tools") { |
| 190 | + return `{"tools": ["test.tool1"]}`, nil |
| 191 | + } |
| 192 | + if stringContains(prompt, "Generate a Go snippet") { |
| 193 | + return `{"code": "__out = map[string]any{\"result\": \"test\"}", "stream": false}`, nil |
| 194 | + } |
| 195 | + return "{}", nil |
| 196 | + }, |
| 197 | + } |
| 198 | + |
| 199 | + cm := NewCodeModeUTCP(mock, mockModel) |
| 200 | + // Mock execute to avoid actual code execution |
| 201 | + cm.executeFunc = func(ctx context.Context, args CodeModeArgs) (CodeModeResult, error) { |
| 202 | + return CodeModeResult{Value: "mocked result"}, nil |
| 203 | + } |
| 204 | + |
| 205 | + ctx := context.Background() |
| 206 | + query := "test query" |
| 207 | + |
| 208 | + // First call to warm up cache |
| 209 | + _, _, _ = cm.CallTool(ctx, query) |
| 210 | + |
| 211 | + b.ResetTimer() |
| 212 | + for i := 0; i < b.N; i++ { |
| 213 | + _, _, _ = cm.CallTool(ctx, query) |
| 214 | + } |
| 215 | +} |
| 216 | + |
| 217 | +// Helper function |
| 218 | +func stringContains(s, substr string) bool { |
| 219 | + if len(s) < len(substr) { |
| 220 | + return false |
| 221 | + } |
| 222 | + for i := 0; i <= len(s)-len(substr); i++ { |
| 223 | + if s[i:i+len(substr)] == substr { |
| 224 | + return true |
| 225 | + } |
| 226 | + } |
| 227 | + return false |
| 228 | +} |
0 commit comments