package engine import ( "fmt" "testing" "git.flytoex.net/yuanwei/flyto-agent/pkg/query" ) // --- NormalizeMessagesForAPI benchmarks --- func makeMessages(n int) []query.Message { msgs := make([]query.Message, n) for i := range msgs { role := query.RoleUser if i%2 == 1 { role = query.RoleAssistant } msgs[i] = query.Message{ Role: role, Content: []query.Content{ {Type: query.ContentText, Text: fmt.Sprintf("Message %d with some content to normalize.", i)}, }, } } return msgs } func makeMessagesWithTools(n int) []query.Message { msgs := make([]query.Message, 0, n*2) for i := 0; i < n; i++ { msgs = append(msgs, query.Message{ Role: query.RoleAssistant, Content: []query.Content{ {Type: query.ContentToolUse, ID: fmt.Sprintf("tool_%d", i), Name: "Bash", Input: map[string]any{"command": "ls"}}, }, }) msgs = append(msgs, query.Message{ Role: query.RoleUser, Content: []query.Content{ {Type: query.ContentToolResult, ToolUseID: fmt.Sprintf("tool_%d", i), Text: "file1.go\nfile2.go"}, }, }) } return msgs } func BenchmarkNormalizeMessagesForAPI(b *testing.B) { for _, size := range []int{10, 50, 100, 500} { msgs := makeMessages(size) b.Run(fmt.Sprintf("text_%d_msgs", size), func(b *testing.B) { b.ReportAllocs() for b.Loop() { NormalizeMessagesForAPI(msgs) } }) } } func BenchmarkNormalizeMessagesForAPI_WithTools(b *testing.B) { for _, pairs := range []int{5, 20, 50} { msgs := makeMessagesWithTools(pairs) b.Run(fmt.Sprintf("%d_tool_pairs", pairs), func(b *testing.B) { b.ReportAllocs() for b.Loop() { NormalizeMessagesForAPI(msgs) } }) } } // --- FileStateCache benchmarks --- func BenchmarkFileStateCache_RecordAndGet(b *testing.B) { c := NewFileStateCache(1000) content := []byte("package main\n\nfunc main() {\n\tfmt.Println(\"hello\")\n}\n") // pre-populate for i := 0; i < 100; i++ { c.Record(fmt.Sprintf("/home/user/project/file_%d.go", i), content) } b.Run("Record", func(b *testing.B) { b.ReportAllocs() for i := range b.N { c.Record(fmt.Sprintf("/home/user/project/bench_%d.go", i%200), content) } }) b.Run("Get_Hit", func(b *testing.B) { b.ReportAllocs() for i := range b.N { c.Get(fmt.Sprintf("/home/user/project/file_%d.go", i%100)) } }) b.Run("Get_Miss", func(b *testing.B) { b.ReportAllocs() for i := range b.N { c.Get(fmt.Sprintf("/nonexistent/path_%d.go", i)) } }) } func BenchmarkFileStateCache_RecentFiles(b *testing.B) { c := NewFileStateCache(500) content := []byte("x") for i := 0; i < 200; i++ { c.Record(fmt.Sprintf("/f%d.go", i), content) } b.Run("top_10", func(b *testing.B) { b.ReportAllocs() for b.Loop() { c.RecentFiles(10) } }) b.Run("top_50", func(b *testing.B) { b.ReportAllocs() for b.Loop() { c.RecentFiles(50) } }) }