image_generation_intent_benchmark_test.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package service
  2. import (
  3. "strings"
  4. "testing"
  5. )
  6. var imageGenerationIntentBenchmarkResult bool
  7. func BenchmarkIsImageGenerationIntent(b *testing.B) {
  8. largeInput := strings.Repeat("x", 1<<20)
  9. benchmarks := []struct {
  10. name string
  11. body []byte
  12. want bool
  13. }{
  14. {
  15. name: "1MiBInputNoImage",
  16. body: []byte(`{"model":"gpt-5.5","tools":[],"input":"` + largeInput + `","tool_choice":"auto"}`),
  17. want: false,
  18. },
  19. {
  20. name: "1MiBInputLeadingImageTool",
  21. body: []byte(`{"model":"gpt-5.5","tools":[{"type":"image_generation"}],"input":"` + largeInput + `"}`),
  22. want: true,
  23. },
  24. {
  25. name: "1MiBInputTrailingToolChoice",
  26. body: []byte(`{"model":"gpt-5.5","tools":[],"input":"` + largeInput + `","tool_choice":{"type":"image_generation"}}`),
  27. want: true,
  28. },
  29. {
  30. name: "Invalid1MiBJSON",
  31. body: []byte(`{"model":"gpt-5.5","input":"` + largeInput),
  32. want: false,
  33. },
  34. {
  35. name: "DuplicateKeysFirstWins",
  36. body: []byte(`{"model":"gpt-5.5","model":"gpt-image-2","tools":[],"tools":[{"type":"image_generation"}],"input":"` + largeInput + `","tool_choice":"auto","tool_choice":{"type":"image_generation"}}`),
  37. want: false,
  38. },
  39. }
  40. for _, benchmark := range benchmarks {
  41. b.Run(benchmark.name, func(b *testing.B) {
  42. if got := IsImageGenerationIntent("/v1/responses", "gpt-5.5", benchmark.body); got != benchmark.want {
  43. b.Fatalf("IsImageGenerationIntent() = %v, want %v", got, benchmark.want)
  44. }
  45. b.ReportAllocs()
  46. b.SetBytes(int64(len(benchmark.body)))
  47. b.ResetTimer()
  48. var result bool
  49. for i := 0; i < b.N; i++ {
  50. result = IsImageGenerationIntent("/v1/responses", "gpt-5.5", benchmark.body)
  51. }
  52. imageGenerationIntentBenchmarkResult = result
  53. })
  54. }
  55. }