From dded099609a82b86ca7ac510f3489ff9088992ca Mon Sep 17 00:00:00 2001 From: Manu Mtz-Almeida Date: Sun, 7 Jun 2015 13:51:35 +0200 Subject: [PATCH] Cosmetic changes --- CHANGELOG.md | 4 ++ benchmarks_test.go | 105 +++++++++++++++++++++++++++------------------ 2 files changed, 67 insertions(+), 42 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index faf43db..938084c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,10 +6,14 @@ - [PERFORMANCE] Much faster 404 routing - [PERFORMANCE] Allocation optimizations - [PERFORMANCE] Faster root tree lookup +- [PERFORMANCE] Zero overhead, String() and JSON() rendering. +- [PERFORMANCE] Faster ClientIP parsing - [PERFORMANCE] Much faster SSE implementation - [NEW] Benchmarks suite - [NEW] Bind validation can be disabled and replaced with custom validators. +- [NEW] More flexible HTML render - [FIX] Binding multipart form +- [FIX] Integration tests - [FIX] Crash when binding non struct object in Context. - [FIX] RunTLS() implementation - [FIX] Logger() unit tests diff --git a/benchmarks_test.go b/benchmarks_test.go index c65ad59..8a1c91a 100644 --- a/benchmarks_test.go +++ b/benchmarks_test.go @@ -6,55 +6,30 @@ import ( "testing" ) -func newMockWriter() *mockWriter { - return &mockWriter{ - http.Header{}, - } -} - -type mockWriter struct { - 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) { - // 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) - } -} - func BenchmarkOneRoute(B *testing.B) { router := New() router.GET("/ping", func(c *Context) {}) runRequest(B, router, "GET", "/ping") } -func BenchmarkManyHandlers(B *testing.B) { - DefaultWriter = newMockWriter() - //router := Default() +func BenchmarkRecoveryMiddleware(B *testing.B) { router := New() - router.Use(Recovery(), Logger()) + 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") @@ -93,6 +68,14 @@ func BenchmarkOneRouteHTML(B *testing.B) { 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) { @@ -134,3 +117,41 @@ func Benchmark404Many(B *testing.B) { 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) + } +}