Browse Source

More benchmarks

Manu Mtz-Almeida 10 years ago
parent
commit
aa9078bc73
2 changed files with 22 additions and 7 deletions
  1. 19 4
      benchmarks_test.go
  2. 3 3
      routergroup.go

+ 19 - 4
benchmarks_test.go

@@ -2,7 +2,7 @@ package gin
 
 
 import (
 import (
 	"bytes"
 	"bytes"
-	"encoding/json"
+	"html/template"
 	"net/http"
 	"net/http"
 	"net/http/httptest"
 	"net/http/httptest"
 	"testing"
 	"testing"
@@ -63,13 +63,28 @@ func BenchmarkOneRouteJSON(B *testing.B) {
 		"status": "ok",
 		"status": "ok",
 	}
 	}
 	router.GET("/json", func(c *Context) {
 	router.GET("/json", func(c *Context) {
-		//c.JSON(200, data)
-		c.Writer.WriteHeader(200)
-		json.NewEncoder(c.Writer).Encode(data)
+		c.JSON(200, data)
 	})
 	})
 	runRequest(B, router, "GET", "/json")
 	runRequest(B, router, "GET", "/json")
 }
 }
 
 
+var htmlContentType = []string{"text/html; charset=utf-8"}
+
+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.Writer.Header()["Content-Type"] = htmlContentType
+		//t.ExecuteTemplate(c.Writer, "index", "hola")
+
+		c.HTML(200, "index", "hola")
+	})
+	runRequest(B, router, "GET", "/html")
+}
+
 func BenchmarkOneRouteString(B *testing.B) {
 func BenchmarkOneRouteString(B *testing.B) {
 	router := New()
 	router := New()
 	router.GET("/text", func(c *Context) {
 	router.GET("/text", func(c *Context) {

+ 3 - 3
routergroup.go

@@ -127,11 +127,11 @@ func (group *RouterGroup) StaticFS(relativePath string, fs http.FileSystem) {
 		panic("URL parameters can not be used when serving a static folder")
 		panic("URL parameters can not be used when serving a static folder")
 	}
 	}
 	handler := group.createStaticHandler(relativePath, fs)
 	handler := group.createStaticHandler(relativePath, fs)
-	relativePath = path.Join(relativePath, "/*filepath")
+	urlPattern := path.Join(relativePath, "/*filepath")
 
 
 	// Register GET and HEAD handlers
 	// Register GET and HEAD handlers
-	group.GET(relativePath, handler)
-	group.HEAD(relativePath, handler)
+	group.GET(urlPattern, handler)
+	group.HEAD(urlPattern, handler)
 }
 }
 
 
 func (group *RouterGroup) createStaticHandler(relativePath string, fs http.FileSystem) HandlerFunc {
 func (group *RouterGroup) createStaticHandler(relativePath string, fs http.FileSystem) HandlerFunc {