60 lines
1.2 KiB
Go
Raw Permalink Normal View History

2023-07-09 17:46:17 +02:00
package router_test
import (
"testing"
2025-02-25 16:38:39 +01:00
"git.urbach.dev/go/router"
"git.urbach.dev/go/router/testdata"
2023-07-09 17:46:17 +02:00
)
2023-09-02 09:19:11 +02:00
func BenchmarkBlog(b *testing.B) {
2024-03-13 13:04:03 +01:00
routes := testdata.Routes("testdata/blog.txt")
2023-09-02 09:19:11 +02:00
r := router.New[string]()
2023-07-09 17:46:17 +02:00
for _, route := range routes {
2024-03-13 13:04:03 +01:00
r.Add(route.Method, route.Path, "")
2023-07-09 17:46:17 +02:00
}
2023-09-02 09:19:11 +02:00
b.Run("Len1-Param0", func(b *testing.B) {
for i := 0; i < b.N; i++ {
r.LookupNoAlloc("GET", "/", noop)
}
})
b.Run("Len1-Param1", func(b *testing.B) {
for i := 0; i < b.N; i++ {
r.LookupNoAlloc("GET", "/:id", noop)
}
})
2023-07-09 17:46:17 +02:00
}
2023-09-02 09:19:11 +02:00
func BenchmarkGitHub(b *testing.B) {
2024-03-13 13:04:03 +01:00
routes := testdata.Routes("testdata/github.txt")
2023-09-02 09:19:11 +02:00
r := router.New[string]()
2023-07-09 17:46:17 +02:00
for _, route := range routes {
2024-03-13 13:04:03 +01:00
r.Add(route.Method, route.Path, "")
2023-07-09 17:46:17 +02:00
}
2023-09-02 09:19:11 +02:00
b.Run("Len7-Param0", func(b *testing.B) {
for i := 0; i < b.N; i++ {
r.LookupNoAlloc("GET", "/issues", noop)
}
})
b.Run("Len7-Param1", func(b *testing.B) {
for i := 0; i < b.N; i++ {
r.LookupNoAlloc("GET", "/gists/:id", noop)
}
})
b.Run("Len7-Param2", func(b *testing.B) {
for i := 0; i < b.N; i++ {
r.LookupNoAlloc("GET", "/repos/:owner/:repo/issues", noop)
}
})
2023-07-09 17:46:17 +02:00
}
2024-03-13 13:04:03 +01:00
// noop serves as an empty addParameter function.
func noop(string, string) {}