gateway_service_benchmark_test.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. package service
  2. import (
  3. "strconv"
  4. "strings"
  5. "testing"
  6. "github.com/Wei-Shaw/sub2api/internal/domain"
  7. )
  8. var (
  9. benchmarkStringSink string
  10. benchmarkIntSink int
  11. )
  12. // BenchmarkGenerateSessionHash_Metadata 关注 JSON 解析与正则匹配开销。
  13. func BenchmarkGenerateSessionHash_Metadata(b *testing.B) {
  14. svc := &GatewayService{}
  15. body := []byte(`{"metadata":{"user_id":"session_123e4567-e89b-12d3-a456-426614174000"},"messages":[{"content":"hello"}]}`)
  16. b.ReportAllocs()
  17. for i := 0; i < b.N; i++ {
  18. parsed, err := ParseGatewayRequest(NewRequestBodyRef(body), "")
  19. if err != nil {
  20. b.Fatalf("解析请求失败: %v", err)
  21. }
  22. benchmarkStringSink = svc.GenerateSessionHash(parsed)
  23. }
  24. }
  25. func BenchmarkParseGatewayRequest_LargeAnthropicMessages(b *testing.B) {
  26. for _, size := range benchmarkBodySizes() {
  27. b.Run(size.name, func(b *testing.B) {
  28. body := buildLargeAnthropicMessagesBody(size.bytes, false)
  29. b.SetBytes(int64(len(body)))
  30. b.ReportAllocs()
  31. b.ResetTimer()
  32. for i := 0; i < b.N; i++ {
  33. parsed, err := ParseGatewayRequest(NewRequestBodyRef(body), domain.PlatformAnthropic)
  34. if err != nil {
  35. b.Fatalf("解析 Anthropic 请求失败: %v", err)
  36. }
  37. benchmarkIntSink = len(parsed.MessagesRaw())
  38. }
  39. })
  40. }
  41. }
  42. func BenchmarkParseGatewayRequest_LargeGeminiContents(b *testing.B) {
  43. for _, size := range benchmarkBodySizes() {
  44. b.Run(size.name, func(b *testing.B) {
  45. body := buildLargeGeminiContentsBody(size.bytes)
  46. b.SetBytes(int64(len(body)))
  47. b.ReportAllocs()
  48. b.ResetTimer()
  49. for i := 0; i < b.N; i++ {
  50. parsed, err := ParseGatewayRequest(NewRequestBodyRef(body), domain.PlatformGemini)
  51. if err != nil {
  52. b.Fatalf("解析 Gemini 请求失败: %v", err)
  53. }
  54. benchmarkIntSink = len(parsed.MessagesRaw())
  55. }
  56. })
  57. }
  58. }
  59. func BenchmarkGenerateSessionHash_LargeAnthropicMessages(b *testing.B) {
  60. svc := &GatewayService{}
  61. for _, size := range benchmarkBodySizes() {
  62. b.Run(size.name, func(b *testing.B) {
  63. body := buildLargeAnthropicMessagesBody(size.bytes, true)
  64. parsed, err := ParseGatewayRequest(NewRequestBodyRef(body), domain.PlatformAnthropic)
  65. if err != nil {
  66. b.Fatalf("解析请求失败: %v", err)
  67. }
  68. b.SetBytes(int64(len(body)))
  69. b.ReportAllocs()
  70. b.ResetTimer()
  71. for i := 0; i < b.N; i++ {
  72. benchmarkStringSink = svc.GenerateSessionHash(parsed)
  73. }
  74. })
  75. }
  76. }
  77. func BenchmarkOpenAIResponses_LargeInputMeta(b *testing.B) {
  78. for _, size := range benchmarkBodySizes() {
  79. b.Run(size.name, func(b *testing.B) {
  80. body := buildLargeOpenAIResponsesBody(size.bytes)
  81. b.SetBytes(int64(len(body)))
  82. b.ReportAllocs()
  83. b.ResetTimer()
  84. for i := 0; i < b.N; i++ {
  85. model, stream, promptCacheKey := extractOpenAIRequestMetaFromBody(body)
  86. benchmarkStringSink = model + promptCacheKey
  87. if stream {
  88. benchmarkIntSink++
  89. }
  90. }
  91. })
  92. }
  93. }
  94. func BenchmarkOpenAIResponses_LargeInputDecodeMap(b *testing.B) {
  95. for _, size := range benchmarkBodySizes() {
  96. b.Run(size.name, func(b *testing.B) {
  97. body := buildLargeOpenAIResponsesBody(size.bytes)
  98. b.SetBytes(int64(len(body)))
  99. b.ReportAllocs()
  100. b.ResetTimer()
  101. for i := 0; i < b.N; i++ {
  102. reqBody, err := getOpenAIRequestBodyMap(nil, body)
  103. if err != nil {
  104. b.Fatalf("解析 OpenAI 请求失败: %v", err)
  105. }
  106. benchmarkIntSink = len(reqBody)
  107. }
  108. })
  109. }
  110. }
  111. func BenchmarkOpenAIResponses_LargeInputRawPatch(b *testing.B) {
  112. for _, size := range benchmarkBodySizes() {
  113. b.Run(size.name, func(b *testing.B) {
  114. body := buildLargeOpenAIResponsesBody(size.bytes)
  115. b.SetBytes(int64(len(body)))
  116. b.ReportAllocs()
  117. b.ResetTimer()
  118. for i := 0; i < b.N; i++ {
  119. view := newOpenAIRequestView(body)
  120. view.MarkPatchSet("instructions", "You are a helpful coding assistant.")
  121. view.MarkPatchSet("reasoning.effort", "none")
  122. patched, err := view.ApplyPatches()
  123. if err != nil {
  124. b.Fatalf("应用 OpenAI raw patch 失败: %v", err)
  125. }
  126. benchmarkIntSink = len(patched)
  127. }
  128. })
  129. }
  130. }
  131. func BenchmarkOpenAIResponses_LargeInputImageBillingRaw(b *testing.B) {
  132. for _, size := range benchmarkBodySizes() {
  133. b.Run(size.name, func(b *testing.B) {
  134. body := buildLargeOpenAIResponsesImageToolBody(size.bytes)
  135. b.SetBytes(int64(len(body)))
  136. b.ReportAllocs()
  137. b.ResetTimer()
  138. for i := 0; i < b.N; i++ {
  139. cfg, err := resolveOpenAIResponsesImageBillingConfigDetailedFromBody(body, "gpt-5.4")
  140. if err != nil {
  141. b.Fatalf("解析 OpenAI 图片计费配置失败: %v", err)
  142. }
  143. benchmarkStringSink = cfg.Model + cfg.SizeTier + cfg.InputSize
  144. }
  145. })
  146. }
  147. }
  148. func BenchmarkOpenAIResponses_LargeInputEmptyBase64Guard(b *testing.B) {
  149. for _, size := range benchmarkBodySizes() {
  150. b.Run(size.name, func(b *testing.B) {
  151. body := buildLargeOpenAIResponsesBody(size.bytes)
  152. b.SetBytes(int64(len(body)))
  153. b.ReportAllocs()
  154. b.ResetTimer()
  155. for i := 0; i < b.N; i++ {
  156. if openAIRequestBodyMayContainEmptyBase64InputImage(body) {
  157. benchmarkIntSink++
  158. }
  159. }
  160. })
  161. }
  162. }
  163. func BenchmarkOpenAIResponses_LargeInputFunctionCallValidation(b *testing.B) {
  164. for _, size := range benchmarkBodySizes() {
  165. b.Run(size.name, func(b *testing.B) {
  166. body := buildLargeOpenAIResponsesToolContinuationBody(size.bytes)
  167. b.SetBytes(int64(len(body)))
  168. b.ReportAllocs()
  169. b.ResetTimer()
  170. for i := 0; i < b.N; i++ {
  171. validation := ValidateFunctionCallOutputContextBytes(body)
  172. if !validation.HasFunctionCallOutput || !validation.HasItemReferenceForAllCallIDs {
  173. b.Fatalf("工具续链校验结果异常: %+v", validation)
  174. }
  175. benchmarkIntSink++
  176. }
  177. })
  178. }
  179. }
  180. // BenchmarkExtractCacheableContent_System 关注字符串拼接路径的性能。
  181. func BenchmarkExtractCacheableContent_System(b *testing.B) {
  182. svc := &GatewayService{}
  183. req := buildSystemCacheableRequest(12)
  184. b.ReportAllocs()
  185. for i := 0; i < b.N; i++ {
  186. benchmarkStringSink = svc.extractCacheableContent(req)
  187. }
  188. }
  189. func benchmarkBodySizes() []struct {
  190. name string
  191. bytes int
  192. } {
  193. return []struct {
  194. name string
  195. bytes int
  196. }{
  197. {name: "4MB", bytes: 4 << 20},
  198. {name: "8MB", bytes: 8 << 20},
  199. {name: "16MB", bytes: 16 << 20},
  200. {name: "32MB", bytes: 32 << 20},
  201. }
  202. }
  203. func buildSystemCacheableRequest(parts int) *ParsedRequest {
  204. var builder strings.Builder
  205. _, _ = builder.WriteString(`{"system":[`)
  206. for i := 0; i < parts; i++ {
  207. if i > 0 {
  208. _ = builder.WriteByte(',')
  209. }
  210. _, _ = builder.WriteString(`{"text":"system_part_`)
  211. _, _ = builder.WriteString(strconv.Itoa(i))
  212. _, _ = builder.WriteString(`","cache_control":{"type":"ephemeral"}}`)
  213. }
  214. _, _ = builder.WriteString(`]}`)
  215. parsed, err := ParseGatewayRequest(NewRequestBodyRef([]byte(builder.String())), "")
  216. if err != nil {
  217. panic(err)
  218. }
  219. return parsed
  220. }
  221. func buildLargeAnthropicMessagesBody(targetBytes int, includeCacheControl bool) []byte {
  222. var builder strings.Builder
  223. builder.Grow(targetBytes + 1024)
  224. _, _ = builder.WriteString(`{"model":"claude-sonnet-4-5","stream":true,"system":[{"type":"text","text":"system seed"}],"messages":[`)
  225. for i := 0; builder.Len() < targetBytes; i++ {
  226. if i > 0 {
  227. _ = builder.WriteByte(',')
  228. }
  229. _, _ = builder.WriteString(`{"role":"user","content":[{"type":"text","text":"`)
  230. _, _ = builder.WriteString(strings.Repeat("anthropic payload ", 64))
  231. _, _ = builder.WriteString(strconv.Itoa(i))
  232. _ = builder.WriteByte('"')
  233. if includeCacheControl && i%32 == 0 {
  234. _, _ = builder.WriteString(`,"cache_control":{"type":"ephemeral"}`)
  235. }
  236. _, _ = builder.WriteString(`}]}`)
  237. }
  238. _, _ = builder.WriteString(`]}`)
  239. return []byte(builder.String())
  240. }
  241. func buildLargeGeminiContentsBody(targetBytes int) []byte {
  242. var builder strings.Builder
  243. builder.Grow(targetBytes + 1024)
  244. _, _ = builder.WriteString(`{"model":"gemini-2.5-pro","systemInstruction":{"parts":[{"text":"system seed"}]},"contents":[`)
  245. for i := 0; builder.Len() < targetBytes; i++ {
  246. if i > 0 {
  247. _ = builder.WriteByte(',')
  248. }
  249. _, _ = builder.WriteString(`{"role":"user","parts":[{"text":"`)
  250. _, _ = builder.WriteString(strings.Repeat("gemini payload ", 64))
  251. _, _ = builder.WriteString(strconv.Itoa(i))
  252. _, _ = builder.WriteString(`"}]}`)
  253. }
  254. _, _ = builder.WriteString(`]}`)
  255. return []byte(builder.String())
  256. }
  257. func buildLargeOpenAIResponsesBody(targetBytes int) []byte {
  258. var builder strings.Builder
  259. builder.Grow(targetBytes + 1024)
  260. _, _ = builder.WriteString(`{"model":"gpt-5.4","stream":true,"prompt_cache_key":"session-benchmark","input":[`)
  261. for i := 0; builder.Len() < targetBytes; i++ {
  262. if i > 0 {
  263. _ = builder.WriteByte(',')
  264. }
  265. _, _ = builder.WriteString(`{"type":"message","role":"user","content":[{"type":"input_text","text":"`)
  266. _, _ = builder.WriteString(strings.Repeat("openai responses payload ", 48))
  267. _, _ = builder.WriteString(strconv.Itoa(i))
  268. _, _ = builder.WriteString(`"}]}`)
  269. }
  270. _, _ = builder.WriteString(`],"tools":[{"type":"function","name":"lookup","parameters":{"type":"object","properties":{"query":{"type":"string"}}}}]}`)
  271. return []byte(builder.String())
  272. }
  273. func buildLargeOpenAIResponsesToolContinuationBody(targetBytes int) []byte {
  274. var builder strings.Builder
  275. builder.Grow(targetBytes + 1024)
  276. _, _ = builder.WriteString(`{"model":"gpt-5.4","stream":true,"previous_response_id":"resp_benchmark","input":[`)
  277. for i := 0; builder.Len() < targetBytes; i++ {
  278. if i > 0 {
  279. _ = builder.WriteByte(',')
  280. }
  281. callID := "call_" + strconv.Itoa(i)
  282. _, _ = builder.WriteString(`{"type":"item_reference","id":"`)
  283. _, _ = builder.WriteString(callID)
  284. _, _ = builder.WriteString(`"},{"type":"function_call_output","call_id":"`)
  285. _, _ = builder.WriteString(callID)
  286. _, _ = builder.WriteString(`","output":"`)
  287. _, _ = builder.WriteString(strings.Repeat("tool output payload ", 48))
  288. _, _ = builder.WriteString(strconv.Itoa(i))
  289. _, _ = builder.WriteString(`"}`)
  290. }
  291. _, _ = builder.WriteString(`]}`)
  292. return []byte(builder.String())
  293. }
  294. func buildLargeOpenAIResponsesImageToolBody(targetBytes int) []byte {
  295. var builder strings.Builder
  296. builder.Grow(targetBytes + 1024)
  297. _, _ = builder.WriteString(`{"model":"gpt-5.4","stream":false,"tools":[{"type":"image_generation","model":"gpt-image-2","size":"2048x1152"}],"input":[`)
  298. for i := 0; builder.Len() < targetBytes; i++ {
  299. if i > 0 {
  300. _ = builder.WriteByte(',')
  301. }
  302. _, _ = builder.WriteString(`{"type":"message","role":"user","content":[{"type":"input_text","text":"`)
  303. _, _ = builder.WriteString(strings.Repeat("openai image billing payload ", 48))
  304. _, _ = builder.WriteString(strconv.Itoa(i))
  305. _, _ = builder.WriteString(`"}]}`)
  306. }
  307. _, _ = builder.WriteString(`]}`)
  308. return []byte(builder.String())
  309. }