Improves performance tests
This commit is contained in:
parent
971c44c8a8
commit
7186200309
@ -1,31 +1,42 @@
|
|||||||
package gin
|
package gin
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"html/template"
|
"html/template"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
type FakeWriter struct{}
|
func newMockWriter() *mockWriter {
|
||||||
|
return &mockWriter{
|
||||||
func (_ FakeWriter) Write(d []byte) (int, error) {
|
http.Header{},
|
||||||
return 0, nil
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (_ FakeWriter) WriteString(d string) (int, error) {
|
type mockWriter struct {
|
||||||
return 0, nil
|
headers 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) {
|
func runRequest(B *testing.B, r *Engine, method, path string) {
|
||||||
// create fake request
|
// create fake request
|
||||||
req, err := http.NewRequest(method, path, nil)
|
req, err := http.NewRequest(method, path, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
w := httptest.NewRecorder()
|
w := newMockWriter()
|
||||||
|
|
||||||
B.ReportAllocs()
|
B.ReportAllocs()
|
||||||
B.ResetTimer()
|
B.ResetTimer()
|
||||||
for i := 0; i < B.N; i++ {
|
for i := 0; i < B.N; i++ {
|
||||||
@ -40,7 +51,7 @@ func BenchmarkOneRoute(B *testing.B) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkManyHandlers(B *testing.B) {
|
func BenchmarkManyHandlers(B *testing.B) {
|
||||||
DefaultWriter = FakeWriter{}
|
DefaultWriter = newMockWriter()
|
||||||
//router := Default()
|
//router := Default()
|
||||||
router := New()
|
router := New()
|
||||||
router.Use(Recovery(), Logger())
|
router.Use(Recovery(), Logger())
|
||||||
@ -50,7 +61,7 @@ func BenchmarkManyHandlers(B *testing.B) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Benchmark5Params(B *testing.B) {
|
func Benchmark5Params(B *testing.B) {
|
||||||
DefaultWriter = new(bytes.Buffer)
|
DefaultWriter = newMockWriter()
|
||||||
router := New()
|
router := New()
|
||||||
router.Use(func(c *Context) {})
|
router.Use(func(c *Context) {})
|
||||||
router.GET("/param/:param1/:params2/:param3/:param4/:param5", func(c *Context) {})
|
router.GET("/param/:param1/:params2/:param3/:param4/:param5", func(c *Context) {})
|
||||||
@ -59,9 +70,9 @@ func Benchmark5Params(B *testing.B) {
|
|||||||
|
|
||||||
func BenchmarkOneRouteJSON(B *testing.B) {
|
func BenchmarkOneRouteJSON(B *testing.B) {
|
||||||
router := New()
|
router := New()
|
||||||
data := H{
|
data := struct {
|
||||||
"status": "ok",
|
Status string `json:"status"`
|
||||||
}
|
}{"ok"}
|
||||||
router.GET("/json", func(c *Context) {
|
router.GET("/json", func(c *Context) {
|
||||||
c.JSON(200, data)
|
c.JSON(200, data)
|
||||||
})
|
})
|
||||||
@ -77,9 +88,6 @@ func BenchmarkOneRouteHTML(B *testing.B) {
|
|||||||
router.SetHTMLTemplate(t)
|
router.SetHTMLTemplate(t)
|
||||||
|
|
||||||
router.GET("/html", func(c *Context) {
|
router.GET("/html", func(c *Context) {
|
||||||
//c.Writer.Header()["Content-Type"] = htmlContentType
|
|
||||||
//t.ExecuteTemplate(c.Writer, "index", "hola")
|
|
||||||
|
|
||||||
c.HTML(200, "index", "hola")
|
c.HTML(200, "index", "hola")
|
||||||
})
|
})
|
||||||
runRequest(B, router, "GET", "/html")
|
runRequest(B, router, "GET", "/html")
|
||||||
@ -93,10 +101,16 @@ func BenchmarkOneRouteString(B *testing.B) {
|
|||||||
runRequest(B, router, "GET", "/text")
|
runRequest(B, router, "GET", "/text")
|
||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkManyRoutes(B *testing.B) {
|
func BenchmarkManyRoutesFist(B *testing.B) {
|
||||||
router := New()
|
router := New()
|
||||||
router.Any("/ping", func(c *Context) {})
|
router.Any("/ping", func(c *Context) {})
|
||||||
runRequest(B, router, "PUT", "/ping")
|
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) {
|
func Benchmark404(B *testing.B) {
|
||||||
|
@ -298,7 +298,7 @@ func githubConfigRouter(router *Engine) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestGithubAPI(t *testing.T) {
|
func TestGithubAPI(t *testing.T) {
|
||||||
DefaultWriter = FakeWriter{}
|
DefaultWriter = newMockWriter()
|
||||||
router := Default()
|
router := Default()
|
||||||
githubConfigRouter(router)
|
githubConfigRouter(router)
|
||||||
|
|
||||||
@ -357,7 +357,7 @@ func BenchmarkGithub(b *testing.B) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkParallelGithub(b *testing.B) {
|
func BenchmarkParallelGithub(b *testing.B) {
|
||||||
DefaultWriter = FakeWriter{}
|
DefaultWriter = newMockWriter()
|
||||||
router := New()
|
router := New()
|
||||||
githubConfigRouter(router)
|
githubConfigRouter(router)
|
||||||
|
|
||||||
@ -373,7 +373,7 @@ func BenchmarkParallelGithub(b *testing.B) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkParallelGithubDefault(b *testing.B) {
|
func BenchmarkParallelGithubDefault(b *testing.B) {
|
||||||
DefaultWriter = FakeWriter{}
|
DefaultWriter = newMockWriter()
|
||||||
router := Default()
|
router := Default()
|
||||||
githubConfigRouter(router)
|
githubConfigRouter(router)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user