package gin import ( "bytes" "html/template" "net/http" "net/http/httptest" "testing" ) type FakeWriter struct{} func (_ FakeWriter) Write(d []byte) (int, error) { return 0, nil } func (_ FakeWriter) WriteString(d string) (int, error) { return 0, nil } func runRequest(B *testing.B, r *Engine, method, path string) { // create fake request req, err := http.NewRequest(method, path, nil) if err != nil { panic(err) } w := httptest.NewRecorder() B.ReportAllocs() B.ResetTimer() for i := 0; i < B.N; i++ { r.ServeHTTP(w, req) } } func BenchmarkOneRoute(B *testing.B) { router := New() router.GET("/ping", func(c *Context) {}) runRequest(B, router, "GET", "/ping") } func BenchmarkManyHandlers(B *testing.B) { DefaultWriter = FakeWriter{} //router := Default() router := New() router.Use(Recovery(), Logger()) router.Use(func(c *Context) {}) router.GET("/ping", func(c *Context) {}) runRequest(B, router, "GET", "/ping") } func Benchmark5Params(B *testing.B) { DefaultWriter = new(bytes.Buffer) router := New() router.Use(func(c *Context) {}) router.GET("/param/:param1/:params2/:param3/:param4/:param5", func(c *Context) {}) runRequest(B, router, "GET", "/param/path/to/parameter/john/12345") } func BenchmarkOneRouteJSON(B *testing.B) { router := New() data := H{ "status": "ok", } router.GET("/json", func(c *Context) { c.JSON(200, data) }) runRequest(B, router, "GET", "/json") } var htmlContentType = []string{"text/html; charset=utf-8"} func BenchmarkOneRouteHTML(B *testing.B) { router := New() t := template.Must(template.New("index").Parse(`