mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-12-06 16:04:24 +00:00
插件化改造前置:把'绝不回归'变成机器可验证的硬约束。 - 透传特征化:anthropic 逐字节/SSE 逐事件、openai、gemini 路径与错误透传 - 计费不变量:5m/1h 双档缓存价、倍率叠加顺序、overages、端到端金额与配额增量 - 调度不变量:粘性会话、failover 10/3/3 完整循环、并发槽配平、等待队列 - 热路径基准基线 + scripts/bench-baseline.sh(allocs 严格/ns 15%/基准缺失 FAIL) - Makefile test-invariants 目标 + CI invariants job;修复失效的 test-e2e 脚本引用 - .gitignore:放行 backend/scripts 与 docs/plugin-architecture(既有白名单模式) 注:本分支按整体门禁验证(部分测试夹具引用后续提交的签名,中间提交不保证独立编译)。
84 lines
3.2 KiB
Go
84 lines
3.2 KiB
Go
//go:build unit
|
|
|
|
// Phase-0 TASK-005 热路径基准:覆盖 anthropic API Key 透传账号的完整
|
|
// Forward 路径(非流式 + 流式 SSE)。与 scripts/bench-baseline.sh 配套使用,
|
|
// 基线存于 testdata/bench/baseline.txt;对比策略:allocs/op 严格(不允许增加)、
|
|
// ns/op 宽松(容忍 15% 抖动)。复用 TASK-002 特征化测试的夹具(passChar* 前缀)。
|
|
package service
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"log"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/Wei-Shaw/sub2api/internal/config"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func benchFwdRun(b *testing.B, header http.Header, upstreamBody string, reqBody string, stream bool) {
|
|
b.Helper()
|
|
gin.SetMode(gin.TestMode)
|
|
// 透传分支每请求有一条 stdlib log,静音以降低基准 I/O 噪声
|
|
// (日志成本在 baseline 与 compare 两侧同等消除,不影响对比有效性)。
|
|
prevLogOut := log.Writer()
|
|
log.SetOutput(io.Discard)
|
|
b.Cleanup(func() { log.SetOutput(prevLogOut) })
|
|
cfg := &config.Config{Gateway: config.GatewayConfig{MaxLineSize: defaultMaxLineSize}}
|
|
account := passCharAnthropicAccount(9100)
|
|
|
|
b.ReportAllocs()
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
upstream := &anthropicHTTPUpstreamRecorder{
|
|
resp: &http.Response{
|
|
StatusCode: http.StatusOK,
|
|
Header: header.Clone(),
|
|
Body: io.NopCloser(strings.NewReader(upstreamBody)),
|
|
},
|
|
}
|
|
svc := passCharGatewayService(cfg, upstream)
|
|
rec := httptest.NewRecorder()
|
|
c, _ := gin.CreateTestContext(rec)
|
|
c.Request = httptest.NewRequest(http.MethodPost, "/v1/messages", nil)
|
|
parsed := &ParsedRequest{
|
|
Body: NewRequestBodyRef([]byte(reqBody)),
|
|
Model: "claude-sonnet-4-20250514",
|
|
Stream: stream,
|
|
}
|
|
if _, err := svc.Forward(context.Background(), c, account, parsed); err != nil {
|
|
b.Fatal(err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func BenchmarkGatewayForward_AnthropicNonStreamPassthrough(b *testing.B) {
|
|
upstreamJSON := `{"id":"msg_bench_01","type":"message","content":[{"type":"text","text":"hello"}],` +
|
|
`"usage":{"input_tokens":12,"output_tokens":7,"cache_read_input_tokens":3}}`
|
|
benchFwdRun(b,
|
|
http.Header{"Content-Type": []string{"application/json"}},
|
|
upstreamJSON,
|
|
`{"model":"claude-sonnet-4-20250514","messages":[{"role":"user","content":"hi"}]}`,
|
|
false,
|
|
)
|
|
}
|
|
|
|
func BenchmarkGatewayForward_AnthropicStreamPassthrough(b *testing.B) {
|
|
events := []string{
|
|
"event: message_start\ndata: {\"type\":\"message_start\",\"message\":{\"id\":\"msg_bench_02\",\"usage\":{\"input_tokens\":9,\"cached_tokens\":3}}}",
|
|
"event: content_block_delta\ndata: {\"type\":\"content_block_delta\",\"index\":0,\"delta\":{\"type\":\"text_delta\",\"text\":\"Hel\"}}",
|
|
"event: content_block_delta\ndata: {\"type\":\"content_block_delta\",\"index\":0,\"delta\":{\"type\":\"text_delta\",\"text\":\"lo\"}}",
|
|
"event: message_delta\ndata: {\"type\":\"message_delta\",\"delta\":{\"stop_reason\":\"end_turn\"},\"usage\":{\"output_tokens\":5}}",
|
|
"event: message_stop\ndata: {\"type\":\"message_stop\"}",
|
|
}
|
|
benchFwdRun(b,
|
|
http.Header{"Content-Type": []string{"text/event-stream"}},
|
|
strings.Join(events, "\n\n")+"\n\n",
|
|
`{"model":"claude-sonnet-4-20250514","stream":true,"messages":[{"role":"user","content":"hi"}]}`,
|
|
true,
|
|
)
|
|
}
|