123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- // 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.
- package gin
- import (
- "html/template"
- "net/http"
- "os"
- "testing"
- )
- func BenchmarkOneRoute(B *testing.B) {
- router := New()
- router.GET("/ping", func(c *Context) {})
- runRequest(B, router, "GET", "/ping")
- }
- func BenchmarkRecoveryMiddleware(B *testing.B) {
- router := New()
- router.Use(Recovery())
- router.GET("/", func(c *Context) {})
- runRequest(B, router, "GET", "/")
- }
- func BenchmarkLoggerMiddleware(B *testing.B) {
- router := New()
- router.Use(LoggerWithWriter(newMockWriter()))
- router.GET("/", func(c *Context) {})
- runRequest(B, router, "GET", "/")
- }
- func BenchmarkManyHandlers(B *testing.B) {
- router := New()
- router.Use(Recovery(), LoggerWithWriter(newMockWriter()))
- router.Use(func(c *Context) {})
- router.Use(func(c *Context) {})
- router.GET("/ping", func(c *Context) {})
- runRequest(B, router, "GET", "/ping")
- }
- func Benchmark5Params(B *testing.B) {
- DefaultWriter = os.Stdout
- 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 := struct {
- Status string `json:"status"`
- }{"ok"}
- router.GET("/json", func(c *Context) {
- c.JSON(http.StatusOK, data)
- })
- runRequest(B, router, "GET", "/json")
- }
- 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")
- })
- runRequest(B, router, "GET", "/html")
- }
- func BenchmarkOneRouteSet(B *testing.B) {
- router := New()
- router.GET("/ping", func(c *Context) {
- c.Set("key", "value")
- })
- runRequest(B, router, "GET", "/ping")
- }
- func BenchmarkOneRouteString(B *testing.B) {
- router := New()
- router.GET("/text", func(c *Context) {
- c.String(http.StatusOK, "this is a plain text")
- })
- runRequest(B, router, "GET", "/text")
- }
- func BenchmarkManyRoutesFist(B *testing.B) {
- router := New()
- router.Any("/ping", func(c *Context) {})
- runRequest(B, router, "GET", "/ping")
- }
- func BenchmarkManyRoutesLast(B *testing.B) {
- router := New()
- router.Any("/ping", func(c *Context) {})
- runRequest(B, router, "OPTIONS", "/ping")
- }
- 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")
- }
- 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)
- }
- }
|