benchmarks_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // Copyright 2017 Manu Martinez-Almeida. All rights reserved.
  2. // Use of this source code is governed by a MIT style
  3. // license that can be found in the LICENSE file.
  4. package gin
  5. import (
  6. "html/template"
  7. "net/http"
  8. "os"
  9. "testing"
  10. )
  11. func BenchmarkOneRoute(B *testing.B) {
  12. router := New()
  13. router.GET("/ping", func(c *Context) {})
  14. runRequest(B, router, "GET", "/ping")
  15. }
  16. func BenchmarkRecoveryMiddleware(B *testing.B) {
  17. router := New()
  18. router.Use(Recovery())
  19. router.GET("/", func(c *Context) {})
  20. runRequest(B, router, "GET", "/")
  21. }
  22. func BenchmarkLoggerMiddleware(B *testing.B) {
  23. router := New()
  24. router.Use(LoggerWithWriter(newMockWriter()))
  25. router.GET("/", func(c *Context) {})
  26. runRequest(B, router, "GET", "/")
  27. }
  28. func BenchmarkManyHandlers(B *testing.B) {
  29. router := New()
  30. router.Use(Recovery(), LoggerWithWriter(newMockWriter()))
  31. router.Use(func(c *Context) {})
  32. router.Use(func(c *Context) {})
  33. router.GET("/ping", func(c *Context) {})
  34. runRequest(B, router, "GET", "/ping")
  35. }
  36. func Benchmark5Params(B *testing.B) {
  37. DefaultWriter = os.Stdout
  38. router := New()
  39. router.Use(func(c *Context) {})
  40. router.GET("/param/:param1/:params2/:param3/:param4/:param5", func(c *Context) {})
  41. runRequest(B, router, "GET", "/param/path/to/parameter/john/12345")
  42. }
  43. func BenchmarkOneRouteJSON(B *testing.B) {
  44. router := New()
  45. data := struct {
  46. Status string `json:"status"`
  47. }{"ok"}
  48. router.GET("/json", func(c *Context) {
  49. c.JSON(http.StatusOK, data)
  50. })
  51. runRequest(B, router, "GET", "/json")
  52. }
  53. func BenchmarkOneRouteHTML(B *testing.B) {
  54. router := New()
  55. t := template.Must(template.New("index").Parse(`
  56. <html><body><h1>{{.}}</h1></body></html>`))
  57. router.SetHTMLTemplate(t)
  58. router.GET("/html", func(c *Context) {
  59. c.HTML(http.StatusOK, "index", "hola")
  60. })
  61. runRequest(B, router, "GET", "/html")
  62. }
  63. func BenchmarkOneRouteSet(B *testing.B) {
  64. router := New()
  65. router.GET("/ping", func(c *Context) {
  66. c.Set("key", "value")
  67. })
  68. runRequest(B, router, "GET", "/ping")
  69. }
  70. func BenchmarkOneRouteString(B *testing.B) {
  71. router := New()
  72. router.GET("/text", func(c *Context) {
  73. c.String(http.StatusOK, "this is a plain text")
  74. })
  75. runRequest(B, router, "GET", "/text")
  76. }
  77. func BenchmarkManyRoutesFist(B *testing.B) {
  78. router := New()
  79. router.Any("/ping", func(c *Context) {})
  80. runRequest(B, router, "GET", "/ping")
  81. }
  82. func BenchmarkManyRoutesLast(B *testing.B) {
  83. router := New()
  84. router.Any("/ping", func(c *Context) {})
  85. runRequest(B, router, "OPTIONS", "/ping")
  86. }
  87. func Benchmark404(B *testing.B) {
  88. router := New()
  89. router.Any("/something", func(c *Context) {})
  90. router.NoRoute(func(c *Context) {})
  91. runRequest(B, router, "GET", "/ping")
  92. }
  93. func Benchmark404Many(B *testing.B) {
  94. router := New()
  95. router.GET("/", func(c *Context) {})
  96. router.GET("/path/to/something", func(c *Context) {})
  97. router.GET("/post/:id", func(c *Context) {})
  98. router.GET("/view/:id", func(c *Context) {})
  99. router.GET("/favicon.ico", func(c *Context) {})
  100. router.GET("/robots.txt", func(c *Context) {})
  101. router.GET("/delete/:id", func(c *Context) {})
  102. router.GET("/user/:id/:mode", func(c *Context) {})
  103. router.NoRoute(func(c *Context) {})
  104. runRequest(B, router, "GET", "/viewfake")
  105. }
  106. type mockWriter struct {
  107. headers http.Header
  108. }
  109. func newMockWriter() *mockWriter {
  110. return &mockWriter{
  111. http.Header{},
  112. }
  113. }
  114. func (m *mockWriter) Header() (h http.Header) {
  115. return m.headers
  116. }
  117. func (m *mockWriter) Write(p []byte) (n int, err error) {
  118. return len(p), nil
  119. }
  120. func (m *mockWriter) WriteString(s string) (n int, err error) {
  121. return len(s), nil
  122. }
  123. func (m *mockWriter) WriteHeader(int) {}
  124. func runRequest(B *testing.B, r *Engine, method, path string) {
  125. // create fake request
  126. req, err := http.NewRequest(method, path, nil)
  127. if err != nil {
  128. panic(err)
  129. }
  130. w := newMockWriter()
  131. B.ReportAllocs()
  132. B.ResetTimer()
  133. for i := 0; i < B.N; i++ {
  134. r.ServeHTTP(w, req)
  135. }
  136. }