lolly/internal/netutil/netutil_bench_test.go
xfy d1da187acc test(http3,logging,netutil,resolver,stream): 添加性能基准测试
为核心模块添加 benchmark 测试:
- http3: Handler 包装、请求/响应转换、Body 读取
- logging: JSON/模板访问日志、变量展开
- netutil: TCPKeepAlive 配置解析
- resolver: DNS 解析性能
- stream: 健康过滤、UDP 会话、负载均衡

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-14 10:49:38 +08:00

169 lines
3.9 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Package netutil 提供网络工具功能的基准测试。
//
// 该文件测试网络工具模块的性能,包括:
// - 客户端 IP 提取性能
// - IP 解析为 net.IP 性能
// - 端口移除性能
// - 端口检查性能
//
// 作者xfy
package netutil
import (
"net"
"testing"
"github.com/valyala/fasthttp"
)
// createBenchCtx 创建用于基准测试的 fasthttp 上下文。
func createBenchCtx() *fasthttp.RequestCtx {
ctx := &fasthttp.RequestCtx{}
ctx.Init(&fasthttp.Request{}, nil, nil)
return ctx
}
// BenchmarkExtractClientIP 测试 ExtractClientIP 函数的性能。
// 覆盖不同场景X-Forwarded-For 单 IP、多 IP、X-Real-IP、RemoteAddr 回退。
func BenchmarkExtractClientIP(b *testing.B) {
b.Run("X-Forwarded-For single IP", func(b *testing.B) {
ctx := createBenchCtx()
ctx.Request.Header.Set("X-Forwarded-For", "192.168.1.100")
b.ResetTimer()
for i := 0; i < b.N; i++ {
ExtractClientIP(ctx)
}
})
b.Run("X-Forwarded-For multiple IPs", func(b *testing.B) {
ctx := createBenchCtx()
ctx.Request.Header.Set("X-Forwarded-For", "192.168.1.100, 10.0.0.1, 172.16.0.1")
b.ResetTimer()
for i := 0; i < b.N; i++ {
ExtractClientIP(ctx)
}
})
b.Run("X-Real-IP only", func(b *testing.B) {
ctx := createBenchCtx()
ctx.Request.Header.Set("X-Real-IP", "192.168.1.200")
b.ResetTimer()
for i := 0; i < b.N; i++ {
ExtractClientIP(ctx)
}
})
b.Run("RemoteAddr fallback", func(b *testing.B) {
ctx := createBenchCtx()
b.ResetTimer()
for i := 0; i < b.N; i++ {
ExtractClientIP(ctx)
}
})
}
// BenchmarkExtractClientIPNet 测试 ExtractClientIPNet 函数的性能。
// 覆盖不同场景X-Forwarded-For、X-Real-IP、RemoteAddr 回退。
func BenchmarkExtractClientIPNet(b *testing.B) {
b.Run("X-Forwarded-For single IP", func(b *testing.B) {
ctx := createBenchCtx()
ctx.Request.Header.Set("X-Forwarded-For", "192.168.1.100")
b.ResetTimer()
for i := 0; i < b.N; i++ {
ExtractClientIPNet(ctx)
}
})
b.Run("X-Real-IP only", func(b *testing.B) {
ctx := createBenchCtx()
ctx.Request.Header.Set("X-Real-IP", "192.168.1.200")
b.ResetTimer()
for i := 0; i < b.N; i++ {
ExtractClientIPNet(ctx)
}
})
b.Run("RemoteAddr fallback", func(b *testing.B) {
ctx := createBenchCtx()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = ExtractClientIPNet(ctx)
}
})
}
// BenchmarkStripPort 测试 StripPort 函数的性能。
// 覆盖不同场景IPv4 带端口、IPv6 带端口、无端口、空字符串。
func BenchmarkStripPort(b *testing.B) {
b.Run("IPv4 with port", func(b *testing.B) {
host := "example.com:8080"
b.ResetTimer()
for i := 0; i < b.N; i++ {
StripPort(host)
}
})
b.Run("IPv6 with port", func(b *testing.B) {
host := "[2001:db8::1]:8443"
b.ResetTimer()
for i := 0; i < b.N; i++ {
StripPort(host)
}
})
b.Run("no port", func(b *testing.B) {
host := "example.com"
b.ResetTimer()
for i := 0; i < b.N; i++ {
StripPort(host)
}
})
b.Run("empty string", func(b *testing.B) {
host := ""
b.ResetTimer()
for i := 0; i < b.N; i++ {
StripPort(host)
}
})
}
// BenchmarkHasPort 测试 HasPort 函数的性能。
// 覆盖不同场景IPv4 带端口、IPv6 带端口、无端口、空字符串。
func BenchmarkHasPort(b *testing.B) {
b.Run("IPv4 with port", func(b *testing.B) {
host := "example.com:8080"
b.ResetTimer()
for i := 0; i < b.N; i++ {
HasPort(host)
}
})
b.Run("IPv6 with port", func(b *testing.B) {
host := "[2001:db8::1]:443"
b.ResetTimer()
for i := 0; i < b.N; i++ {
HasPort(host)
}
})
b.Run("no port", func(b *testing.B) {
host := "example.com"
b.ResetTimer()
for i := 0; i < b.N; i++ {
HasPort(host)
}
})
b.Run("empty string", func(b *testing.B) {
host := ""
b.ResetTimer()
for i := 0; i < b.N; i++ {
HasPort(host)
}
})
}
// 确保 net 包被使用(避免未使用导入警告)
var _ = net.IPv4zero