lolly/internal/netutil/netutil_bench_test.go
xfy 2734b04d8f refactor: remove 16.8k lines of dead code across all internal packages
- Delete unused files: tempfile subsystem, matcher variants, server/internal
- Remove 200+ unused functions across proxy, ssl, lua, http2/3, stream, variable
- Fix proxy test type errors (backgroundRefresh ctx→Request)
- Move bench/tools mock backend into internal/testutil
- Remove corresponding test functions for all deleted code
2026-06-03 16:15:43 +08:00

133 lines
3.0 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 b.Loop() {
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 b.Loop() {
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 b.Loop() {
ExtractClientIP(ctx)
}
})
b.Run("RemoteAddr fallback", func(b *testing.B) {
ctx := createBenchCtx()
b.ResetTimer()
for b.Loop() {
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 b.Loop() {
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 b.Loop() {
ExtractClientIPNet(ctx)
}
})
b.Run("RemoteAddr fallback", func(b *testing.B) {
ctx := createBenchCtx()
b.ResetTimer()
for b.Loop() {
_ = 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 b.Loop() {
StripPort(host)
}
})
b.Run("IPv6 with port", func(b *testing.B) {
host := "[2001:db8::1]:8443"
b.ResetTimer()
for b.Loop() {
StripPort(host)
}
})
b.Run("no port", func(b *testing.B) {
host := "example.com"
b.ResetTimer()
for b.Loop() {
StripPort(host)
}
})
b.Run("empty string", func(b *testing.B) {
host := ""
b.ResetTimer()
for b.Loop() {
StripPort(host)
}
})
}
// 确保 net 包被使用(避免未使用导入警告)
var _ = net.IPv4zero