64 lines
1.1 KiB
Go
Raw Normal View History

2024-03-22 14:08:24 +00:00
package web_test
2024-03-12 19:35:19 +00:00
import (
"net/http/httptest"
2024-03-14 11:52:03 +00:00
"strings"
2024-03-12 19:35:19 +00:00
"testing"
2024-03-13 19:18:01 +00:00
"git.akyoto.dev/go/router/testdata"
2024-03-22 14:08:24 +00:00
"git.akyoto.dev/go/web"
2024-03-12 19:35:19 +00:00
)
2024-03-14 11:52:03 +00:00
func BenchmarkStatic(b *testing.B) {
paths := []string{
"/",
"/hello",
"/hello/world",
}
2024-03-22 14:08:24 +00:00
s := web.NewServer()
2024-03-12 19:35:19 +00:00
2024-03-14 11:52:03 +00:00
for _, path := range paths {
2024-03-22 14:08:24 +00:00
s.Get(path, func(ctx web.Context) error {
2024-03-14 11:52:03 +00:00
return ctx.String("Hello")
})
}
for _, path := range paths {
b.Run(strings.TrimPrefix(path, "/"), func(b *testing.B) {
request := httptest.NewRequest("GET", path, nil)
response := &NullResponse{}
2024-03-12 19:35:19 +00:00
2024-03-14 11:52:03 +00:00
for range b.N {
s.ServeHTTP(response, request)
}
})
2024-03-12 19:35:19 +00:00
}
}
func BenchmarkGitHub(b *testing.B) {
2024-03-14 11:52:03 +00:00
paths := []string{
"/gists/:id",
"/repos/:a/:b",
}
2024-03-22 14:08:24 +00:00
s := web.NewServer()
2024-03-12 19:35:19 +00:00
2024-03-13 19:18:01 +00:00
for _, route := range testdata.Routes("testdata/github.txt") {
2024-03-22 14:08:24 +00:00
s.Router().Add(route.Method, route.Path, func(ctx web.Context) error {
2024-03-14 11:52:03 +00:00
return ctx.String("Hello")
2024-03-12 19:35:19 +00:00
})
}
2024-03-14 11:52:03 +00:00
for _, path := range paths {
b.Run(strings.TrimPrefix(path, "/"), func(b *testing.B) {
request := httptest.NewRequest("GET", path, nil)
response := &NullResponse{}
for range b.N {
s.ServeHTTP(response, request)
}
})
2024-03-12 19:35:19 +00:00
}
}