lolly/internal/matcher/bench_test.go
xfy 53eaec57ad feat(matcher): 添加 nginx 风格 location 匹配引擎
实现 nginx 兼容的 location 匹配系统,支持:
- 精确匹配 (=) - Hash Map O(1)
- 前缀优先匹配 (^~) - Radix Tree
- 正则匹配 (~, ~*) - 按配置顺序
- 普通前缀匹配 - Radix Tree 最长匹配
- 命名 location (@name) - 内部重定向

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-17 09:26:22 +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)
}
}
}
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)
}
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")
}
}