41 lines
1.0 KiB
Go
Raw Permalink Normal View History

2024-03-13 21:33:41 +00:00
package main_test
import (
"net/http"
"net/http/httptest"
"testing"
"git.akyoto.dev/go/router/testdata"
)
var (
githubRoute = "/repos/:owner/:repo"
payloadString = "Hello"
payload = []byte(payloadString)
)
// NullResponse implements the http.ResponseWriter interface with
// empty methods to better understand memory usage in benchmarks.
type NullResponse struct{}
func (r *NullResponse) Header() http.Header { return http.Header{} }
func (r *NullResponse) Write([]byte) (int, error) { return 0, nil }
func (r *NullResponse) WriteHeader(int) {}
// bench is the core benchmarking function.
func bench[T http.Handler](b *testing.B, init func() T, addRoute func(T, string, string)) {
b.Run("GitHub", func(b *testing.B) {
request := httptest.NewRequest("GET", githubRoute, nil)
response := &NullResponse{}
router := init()
for _, route := range testdata.Routes("testdata/github.txt") {
addRoute(router, route.Method, route.Path)
}
for range b.N {
router.ServeHTTP(response, request)
}
})
}