perf(后端): 完成性能优化与连接池配置
新增 DB/Redis 连接池配置与校验,并补充单测 网关请求体大小限制与 413 处理 HTTP/req 客户端池化并调整上游连接池默认值 并发槽位改为 ZSET+Lua 与指数退避 用量统计改 SQL 聚合并新增索引迁移 计费缓存写入改工作池并补测试/基准 测试: 在 backend/ 下运行 go test ./...
This commit is contained in:
46
backend/internal/repository/http_upstream_benchmark_test.go
Normal file
46
backend/internal/repository/http_upstream_benchmark_test.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/url"
|
||||
"testing"
|
||||
|
||||
"github.com/Wei-Shaw/sub2api/internal/config"
|
||||
)
|
||||
|
||||
var httpClientSink *http.Client
|
||||
|
||||
// BenchmarkHTTPUpstreamProxyClient 对比重复创建与复用代理客户端的开销。
|
||||
func BenchmarkHTTPUpstreamProxyClient(b *testing.B) {
|
||||
cfg := &config.Config{
|
||||
Gateway: config.GatewayConfig{ResponseHeaderTimeout: 300},
|
||||
}
|
||||
upstream := NewHTTPUpstream(cfg)
|
||||
svc, ok := upstream.(*httpUpstreamService)
|
||||
if !ok {
|
||||
b.Fatalf("类型断言失败,无法获取 httpUpstreamService")
|
||||
}
|
||||
|
||||
proxyURL := "http://127.0.0.1:8080"
|
||||
b.ReportAllocs()
|
||||
|
||||
b.Run("新建", func(b *testing.B) {
|
||||
parsedProxy, err := url.Parse(proxyURL)
|
||||
if err != nil {
|
||||
b.Fatalf("解析代理地址失败: %v", err)
|
||||
}
|
||||
for i := 0; i < b.N; i++ {
|
||||
httpClientSink = &http.Client{
|
||||
Transport: buildUpstreamTransport(cfg, parsedProxy),
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
b.Run("复用", func(b *testing.B) {
|
||||
client := svc.getOrCreateClient(proxyURL)
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
httpClientSink = client
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user