Files
sub2api/backend/internal/service/image_generation_intent_benchmark_test.go
T
李建琦 6d655c9903
Release / update-version (push) Has been cancelled
Release / build-frontend (push) Has been cancelled
Release / release (push) Has been cancelled
Release / sync-version-file (push) Has been cancelled
CI / shell (push) Canceled after 0s
CI / test (push) Canceled after 0s
CI / frontend (push) Canceled after 0s
CI / golangci-lint (push) Canceled after 0s
Security Scan / backend-security (push) Canceled after 0s
Security Scan / frontend-security (push) Canceled after 0s
Sub2API v1.0 - AI API 网关(二开初始版本,基于上游 Wei-Shaw/sub2api)
2026-08-21 18:30:13 +08:00

60 lines
1.7 KiB
Go

package service
import (
"strings"
"testing"
)
var imageGenerationIntentBenchmarkResult bool
func BenchmarkIsImageGenerationIntent(b *testing.B) {
largeInput := strings.Repeat("x", 1<<20)
benchmarks := []struct {
name string
body []byte
want bool
}{
{
name: "1MiBInputNoImage",
body: []byte(`{"model":"gpt-5.5","tools":[],"input":"` + largeInput + `","tool_choice":"auto"}`),
want: false,
},
{
name: "1MiBInputLeadingImageTool",
body: []byte(`{"model":"gpt-5.5","tools":[{"type":"image_generation"}],"input":"` + largeInput + `"}`),
want: true,
},
{
name: "1MiBInputTrailingToolChoice",
body: []byte(`{"model":"gpt-5.5","tools":[],"input":"` + largeInput + `","tool_choice":{"type":"image_generation"}}`),
want: true,
},
{
name: "Invalid1MiBJSON",
body: []byte(`{"model":"gpt-5.5","input":"` + largeInput),
want: false,
},
{
name: "DuplicateKeysFirstWins",
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"}}`),
want: false,
},
}
for _, benchmark := range benchmarks {
b.Run(benchmark.name, func(b *testing.B) {
if got := IsImageGenerationIntent("/v1/responses", "gpt-5.5", benchmark.body); got != benchmark.want {
b.Fatalf("IsImageGenerationIntent() = %v, want %v", got, benchmark.want)
}
b.ReportAllocs()
b.SetBytes(int64(len(benchmark.body)))
b.ResetTimer()
var result bool
for i := 0; i < b.N; i++ {
result = IsImageGenerationIntent("/v1/responses", "gpt-5.5", benchmark.body)
}
imageGenerationIntentBenchmarkResult = result
})
}
}