gin/benchmarks_test.go

161 lines
3.8 KiB
Go
Raw Permalink Normal View History

2017-06-12 09:04:52 +03:00
// Copyright 2017 Manu Martinez-Almeida. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
2015-05-30 15:43:09 +03:00
package gin
import (
2015-05-31 16:55:10 +03:00
"html/template"
2015-05-30 15:43:09 +03:00
"net/http"
2016-04-15 02:35:22 +03:00
"os"
2015-05-30 15:43:09 +03:00
"testing"
)
2015-06-07 14:51:35 +03:00
func BenchmarkOneRoute(B *testing.B) {
router := New()
router.GET("/ping", func(c *Context) {})
runRequest(B, router, "GET", "/ping")
2015-06-04 13:50:52 +03:00
}
2015-06-07 14:51:35 +03:00
func BenchmarkRecoveryMiddleware(B *testing.B) {
router := New()
router.Use(Recovery())
router.GET("/", func(c *Context) {})
runRequest(B, router, "GET", "/")
2015-05-30 15:43:09 +03:00
}
2015-06-07 14:51:35 +03:00
func BenchmarkLoggerMiddleware(B *testing.B) {
2015-05-30 15:43:09 +03:00
router := New()
2015-06-07 14:51:35 +03:00
router.Use(LoggerWithWriter(newMockWriter()))
router.GET("/", func(c *Context) {})
runRequest(B, router, "GET", "/")
2015-05-30 15:43:09 +03:00
}
func BenchmarkManyHandlers(B *testing.B) {
router := New()
2015-06-07 14:51:35 +03:00
router.Use(Recovery(), LoggerWithWriter(newMockWriter()))
router.Use(func(c *Context) {})
2015-05-30 15:43:09 +03:00
router.Use(func(c *Context) {})
router.GET("/ping", func(c *Context) {})
runRequest(B, router, "GET", "/ping")
}
func Benchmark5Params(B *testing.B) {
2016-04-15 02:35:22 +03:00
DefaultWriter = os.Stdout
2015-05-30 15:43:09 +03:00
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()
2015-06-04 13:50:52 +03:00
data := struct {
Status string `json:"status"`
}{"ok"}
2015-05-30 15:43:09 +03:00
router.GET("/json", func(c *Context) {
c.JSON(http.StatusOK, data)
2015-05-30 15:43:09 +03:00
})
runRequest(B, router, "GET", "/json")
}
2015-05-31 16:55:10 +03:00
func BenchmarkOneRouteHTML(B *testing.B) {
router := New()
t := template.Must(template.New("index").Parse(`
<html><body><h1>{{.}}</h1></body></html>`))
router.SetHTMLTemplate(t)
router.GET("/html", func(c *Context) {
c.HTML(http.StatusOK, "index", "hola")
2015-05-31 16:55:10 +03:00
})
runRequest(B, router, "GET", "/html")
}
2015-06-07 14:51:35 +03:00
func BenchmarkOneRouteSet(B *testing.B) {
router := New()
router.GET("/ping", func(c *Context) {
c.Set("key", "value")
})
runRequest(B, router, "GET", "/ping")
}
2015-05-30 15:43:09 +03:00
func BenchmarkOneRouteString(B *testing.B) {
router := New()
router.GET("/text", func(c *Context) {
c.String(http.StatusOK, "this is a plain text")
2015-05-30 15:43:09 +03:00
})
runRequest(B, router, "GET", "/text")
}
2015-06-04 13:50:52 +03:00
func BenchmarkManyRoutesFist(B *testing.B) {
router := New()
router.Any("/ping", func(c *Context) {})
runRequest(B, router, "GET", "/ping")
}
func BenchmarkManyRoutesLast(B *testing.B) {
2015-05-30 15:43:09 +03:00
router := New()
router.Any("/ping", func(c *Context) {})
2015-06-04 13:50:52 +03:00
runRequest(B, router, "OPTIONS", "/ping")
2015-05-30 15:43:09 +03:00
}
func Benchmark404(B *testing.B) {
router := New()
router.Any("/something", func(c *Context) {})
router.NoRoute(func(c *Context) {})
runRequest(B, router, "GET", "/ping")
}
func Benchmark404Many(B *testing.B) {
router := New()
router.GET("/", func(c *Context) {})
router.GET("/path/to/something", func(c *Context) {})
router.GET("/post/:id", func(c *Context) {})
router.GET("/view/:id", func(c *Context) {})
router.GET("/favicon.ico", func(c *Context) {})
router.GET("/robots.txt", func(c *Context) {})
router.GET("/delete/:id", func(c *Context) {})
router.GET("/user/:id/:mode", func(c *Context) {})
router.NoRoute(func(c *Context) {})
runRequest(B, router, "GET", "/viewfake")
}
2015-06-07 14:51:35 +03:00
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 *Engine, method, path string) {
// create fake request
req, err := http.NewRequest(method, path, nil)
if err != nil {
panic(err)
}
w := newMockWriter()
B.ReportAllocs()
B.ResetTimer()
for i := 0; i < B.N; i++ {
r.ServeHTTP(w, req)
}
}