lolly/internal/matcher/bench_test.go
xfy ca03c121d3 refactor(matcher): 提取 LocationType 常量并优化结构体字段布局
- 添加 LocationType 常量定义替代硬编码字符串
- 优化 MatchResult、ExactMatcher、NamedMatcher 结构体字段顺序
- RadixTree.Insert 添加 locationType 参数用于调试追踪
- 更新测试代码适配新接口

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-04-17 10:11:45 +08:00

77 lines
1.6 KiB
Go

package matcher
import (
"testing"
"github.com/valyala/fasthttp"
)
func BenchmarkRadixTree_Insert(b *testing.B) {
tree := NewRadixTree()
handler := func(ctx *fasthttp.RequestCtx) {}
paths := []string{
"/", "/api", "/api/v1", "/api/v2",
"/static", "/static/css", "/static/js",
"/user", "/user/profile", "/user/settings",
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
for _, p := range paths {
tree.Insert(p, handler, i, "prefix")
}
}
}
func BenchmarkRadixTree_Find(b *testing.B) {
tree := NewRadixTree()
handler := func(ctx *fasthttp.RequestCtx) {}
paths := []string{"/", "/api", "/api/v1", "/api/v2/users/123"}
for i, p := range paths {
tree.Insert(p, handler, i+1, "prefix")
}
tree.MarkInitialized()
b.ResetTimer()
for i := 0; i < b.N; i++ {
tree.FindLongestPrefix("/api/v2/users/123/details")
}
}
func BenchmarkExactMatcher_Match(b *testing.B) {
handler := func(ctx *fasthttp.RequestCtx) {}
m := NewExactMatcher("/api/users", handler, 1)
b.ResetTimer()
for i := 0; i < b.N; i++ {
m.Match("/api/users")
}
}
func BenchmarkRegexMatcher_Match(b *testing.B) {
m := MustRegexMatcher(`^/api/v[0-9]+/users/[0-9]+$`, nil, 3, false)
b.ResetTimer()
for i := 0; i < b.N; i++ {
m.Match("/api/v1/users/123")
}
}
func BenchmarkLocationEngine_Match(b *testing.B) {
engine := NewLocationEngine()
handler := func(ctx *fasthttp.RequestCtx) {}
engine.AddExact("/api", handler)
engine.AddPrefixPriority("/api/", handler)
engine.AddRegex(`\.php$`, handler, false)
engine.AddPrefix("/", handler)
engine.MarkInitialized()
b.ResetTimer()
for i := 0; i < b.N; i++ {
engine.Match("/api/users/123")
}
}