package main_test import ( "net/http" "net/http/httptest" "testing" "git.akyoto.dev/go/router/testdata" ) var ( helloRoute = "/" 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("Hello", func(b *testing.B) { request := httptest.NewRequest("GET", helloRoute, nil) response := &NullResponse{} router := init() addRoute(router, "GET", "/") for range b.N { router.ServeHTTP(response, request) } }) 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) } }) }