generatePerformanceMetrics calculates aggregated performance statistics
(processedRun ProcessedRun, metrics MetricsData, toolUsage []ToolUsageInfo)
| 305 | |
| 306 | // generatePerformanceMetrics calculates aggregated performance statistics |
| 307 | func generatePerformanceMetrics(processedRun ProcessedRun, metrics MetricsData, toolUsage []ToolUsageInfo) *PerformanceMetrics { |
| 308 | run := processedRun.Run |
| 309 | auditReportLog.Printf("Generating performance metrics: token_usage=%d, tool_count=%d, duration=%v", metrics.TokenUsage, len(toolUsage), run.Duration) |
| 310 | pm := &PerformanceMetrics{} |
| 311 | |
| 312 | // Calculate tokens per minute |
| 313 | if run.Duration > 0 && metrics.TokenUsage > 0 { |
| 314 | minutes := run.Duration.Minutes() |
| 315 | if minutes > 0 { |
| 316 | pm.TokensPerMinute = float64(metrics.TokenUsage) / minutes |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | // Find most used tool |
| 321 | if len(toolUsage) > 0 { |
| 322 | mostUsed := toolUsage[0] |
| 323 | for i := 1; i < len(toolUsage); i++ { |
| 324 | if toolUsage[i].CallCount > mostUsed.CallCount { |
| 325 | mostUsed = toolUsage[i] |
| 326 | } |
| 327 | } |
| 328 | pm.MostUsedTool = fmt.Sprintf("%s (%d calls)", mostUsed.Name, mostUsed.CallCount) |
| 329 | auditReportLog.Printf("Most used tool: %s with %d calls", mostUsed.Name, mostUsed.CallCount) |
| 330 | } |
| 331 | |
| 332 | // Calculate average tool duration |
| 333 | if len(toolUsage) > 0 { |
| 334 | totalDuration := time.Duration(0) |
| 335 | count := 0 |
| 336 | for _, tool := range toolUsage { |
| 337 | if tool.MaxDuration != "" { |
| 338 | // Try to parse duration string using time.ParseDuration |
| 339 | if d, err := time.ParseDuration(tool.MaxDuration); err == nil { |
| 340 | totalDuration += d |
| 341 | count++ |
| 342 | } |
| 343 | } |
| 344 | } |
| 345 | if count > 0 { |
| 346 | avgDuration := totalDuration / time.Duration(count) |
| 347 | pm.AvgToolDuration = timeutil.FormatDuration(avgDuration) |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | // Network request count from firewall |
| 352 | if processedRun.FirewallAnalysis != nil { |
| 353 | pm.NetworkRequests = processedRun.FirewallAnalysis.TotalRequests |
| 354 | } |
| 355 | |
| 356 | return pm |
| 357 | } |