AFAIRE
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
1.4 KiB

/*
------------------------------------------------------------------------------------------------------------------------
####### kong ####### Copyright (c) 2021-2022 losyme ################################################ MIT License #######
------------------------------------------------------------------------------------------------------------------------
*/
package router
import (
"net/http"
"testing"
"forge.chapril.org/losyme/kong/context"
)
func BenchmarkOneRoute(B *testing.B) {
router := New(context.RendererTypeJSON)
router.Get("/ping", func(c *context.Context) error {
return nil
})
runRequest(B, router, "GET", "/ping")
}
type mockWriter struct {
headers http.Header
}
func newMockWriter() *mockWriter {
return &mockWriter{
http.Header{},
}
}
func (m *mockWriter) Header() (h http.Header) {
return m.headers
}
func (m *mockWriter) Write(p []byte) (n int, err error) {
return len(p), nil
}
func (m *mockWriter) WriteString(s string) (n int, err error) {
return len(s), nil
}
func (m *mockWriter) WriteHeader(int) {}
func runRequest(B *testing.B, r *Router, method, path string) {
req, err := http.NewRequest(method, path, nil)
if err != nil {
panic(err)
}
rw := newMockWriter()
B.ReportAllocs()
B.ResetTimer()
sr := r.SubRouter
for i := 0; i < B.N; i++ {
sr.ServeHTTP(rw, req)
}
}
/*
######################################################################################################## @(°_°)@ #######
*/