openai_responses_image_intent_benchmark_test.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package handler
  2. import (
  3. "strings"
  4. "testing"
  5. "github.com/Wei-Shaw/sub2api/internal/service"
  6. )
  7. var openAIResponsesImageIntentRoutingBenchmarkSink service.OpenAIEndpointCapability
  8. func BenchmarkOpenAIResponsesImageIntentRouting_LargeToolsBody(b *testing.B) {
  9. body := buildLargeOpenAIResponsesToolsBody(32 << 20)
  10. if service.IsExplicitImageGenerationIntent("/v1/responses", "gpt-5.4", body) {
  11. b.Fatal("large tools body must not have explicit image intent")
  12. }
  13. platform := service.PlatformOpenAI
  14. b.Run("reuse_once", func(b *testing.B) {
  15. b.SetBytes(int64(len(body)))
  16. b.ReportAllocs()
  17. for range b.N {
  18. imageIntent := service.IsExplicitImageGenerationIntent("/v1/responses", "gpt-5.4", body)
  19. openAIResponsesImageIntentRoutingBenchmarkSink = openAIResponsesRequiredCapability(imageIntent, platform)
  20. }
  21. })
  22. b.Run("rescan_twice", func(b *testing.B) {
  23. b.SetBytes(int64(len(body)))
  24. b.ReportAllocs()
  25. for range b.N {
  26. imageIntent := service.IsExplicitImageGenerationIntent("/v1/responses", "gpt-5.4", body)
  27. // 对照优化前路径:路由阶段会再次扫描同一份未修改的 body。
  28. requiredCapability := service.OpenAIEndpointCapabilityChatCompletions
  29. if service.IsExplicitImageGenerationIntent("/v1/responses", "gpt-5.4", body) && platform == service.PlatformOpenAI {
  30. requiredCapability = service.OpenAIEndpointCapabilityResponses
  31. }
  32. if imageIntent && requiredCapability != service.OpenAIEndpointCapabilityResponses {
  33. b.Fatal("explicit image intent must require Responses")
  34. }
  35. openAIResponsesImageIntentRoutingBenchmarkSink = requiredCapability
  36. }
  37. })
  38. }
  39. func buildLargeOpenAIResponsesToolsBody(targetBytes int) []byte {
  40. var builder strings.Builder
  41. builder.Grow(targetBytes + 256)
  42. _, _ = builder.WriteString(`{"model":"gpt-5.4","tools":[{"type":"function","name":"search","description":"`)
  43. _, _ = builder.WriteString(strings.Repeat("x", targetBytes))
  44. _, _ = builder.WriteString(`"}],"tool_choice":"auto","input":"write code"}`)
  45. return []byte(builder.String())
  46. }