因爲golang提供了完善的net/http標準庫,基於該標準庫實現一個web框架的難度相比其餘語言低了很多,因此go web框架簡直就是百花齊放。從老牌的revel和beego,到新出的gin,和iris等,並且還有一些相似於chi這種router。我的通常小項目,尤爲是中間件須要暴露一些http接口的,基本就使用chi便可。
本次測試主要是gin iris echo 這三個框架。側重在於高性能,從併發和json序列化和反序列化兩個方面來測評,畢竟後臺項目側重的也就是這兩個方面。git
爲了選擇符合重IO的框架,現設定以下場景的demo,demo的具體要求以下:github
gin:golang
package main import ( "log" "net/http" "time" "github.com/gin-gonic/gin" ) // Agent ... type Agent struct { AgentID string `json:"agent_id"` QueuedAt string `json:"queued_at"` QueuedBy string `json:"queued_by"` } // Details ... type Details struct { EventID string `json:"event_id"` Endpoint string Metric string Content string Priority int Status string } // Test1 ... type Test1 struct { Agent Agent Details Details Description string EventType string `json:"event_type"` ServiceKey string `json:"service_key"` } // Test2 test2 type Test2 struct { Data []*Test1 } func main() { r := gin.New() // Global middleware // Logger middleware will write the logs to gin.DefaultWriter even if you set with GIN_MODE=release. // By default gin.DefaultWriter = os.Stdout r.Use(gin.Logger()) r.GET("/ping", func(c *gin.Context) { c.JSON(200, gin.H{ "message": "pong", }) }) r.POST("/v1/test", func(c *gin.Context) { var test Test1 if err := c.BindJSON(&test); err == nil { log.Println("========================start io=====================") time.Sleep(time.Duration(1) * time.Second) log.Println("=========================end io=======================") c.JSON(http.StatusOK, test) } else { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) } }) r.POST("/v2/test", func(c *gin.Context) { var test Test2 if err := c.BindJSON(&test); err == nil { log.Println("========================start io=====================") time.Sleep(time.Duration(1) * time.Second) log.Println("=========================end io=======================") c.JSON(http.StatusOK, test) } else { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) } }) r.POST("/v3/test", func(c *gin.Context) { var test Test2 if err := c.BindJSON(&test); err == nil { log.Println("========================start io=====================") time.Sleep(time.Duration(1) * time.Second) log.Println("=========================end io=======================") c.JSON(http.StatusOK, test) } else { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) } }) r.POST("/v4/test", func(c *gin.Context) { var test Test2 if err := c.BindJSON(&test); err == nil { log.Println("========================start io=====================") time.Sleep(time.Duration(1) * time.Second) log.Println("=========================end io=======================") c.JSON(http.StatusOK, test) } else { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) } }) r.Run() // listen and serve on 0.0.0.0:8080 }
iris:web
package main import ( "time" "github.com/kataras/iris" "github.com/kataras/iris/middleware/logger" ) // Agent ... type Agent struct { AgentID string `json:"agent_id"` QueuedAt string `json:"queued_at"` QueuedBy string `json:"queued_by"` } // Details ... type Details struct { EventID string `json:"event_id"` Endpoint string Metric string Content string Priority int Status string } // Test1 ... type Test1 struct { Agent Agent Details Details Description string EventType string `json:"event_type"` ServiceKey string `json:"service_key"` } // Test2 test2 type Test2 struct { Data []*Test1 } func main() { app := iris.New() app.Use(logger.New()) app.Get("/ping", func(c iris.Context) { c.WriteString("pong") }) app.Post("/v1/test", func(c iris.Context) { var test Test1 if err := c.ReadJSON(&test); err == nil { app.Logger().Println("========================start io=====================") time.Sleep(time.Duration(1) * time.Second) app.Logger().Println("=========================end io=======================") c.JSON(test) } else { c.WriteString("failure") } }) app.Post("/v2/test", func(c iris.Context) { var test Test2 if err := c.ReadJSON(&test); err == nil { app.Logger().Println("========================start io=====================") time.Sleep(time.Duration(1) * time.Second) app.Logger().Println("=========================end io=======================") c.JSON(test) } else { c.WriteString("failure") } }) app.Post("/v3/test", func(c iris.Context) { var test Test2 if err := c.ReadJSON(&test); err == nil { app.Logger().Println("========================start io=====================") time.Sleep(time.Duration(1) * time.Second) app.Logger().Println("=========================end io=======================") c.JSON(test) } else { c.WriteString("failure") } }) app.Post("/v4/test", func(c iris.Context) { var test Test2 if err := c.ReadJSON(&test); err == nil { app.Logger().Println("========================start io=====================") time.Sleep(time.Duration(1) * time.Second) app.Logger().Println("=========================end io=======================") c.JSON(test) } else { c.WriteString("failure") } }) // Start the server using a network address. app.Run( iris.Addr(":8080"), // disables updates: iris.WithoutVersionChecker, // skip err server closed when CTRL/CMD+C pressed: iris.WithoutServerError(iris.ErrServerClosed), // enables faster json serialization and more: iris.WithOptimizations) }
echo:json
package main import ( "log" "net/http" "time" "github.com/labstack/echo" "github.com/labstack/echo/middleware" ) // Agent ... type Agent struct { AgentID string `json:"agent_id"` QueuedAt string `json:"queued_at"` QueuedBy string `json:"queued_by"` } // Details ... type Details struct { EventID string `json:"event_id"` Endpoint string Metric string Content string Priority int Status string } // Test1 ... type Test1 struct { Agent Agent Details Details Description string EventType string `json:"event_type"` ServiceKey string `json:"service_key"` } // Test2 test2 type Test2 struct { Data []*Test1 } func main() { // Echo instance app := echo.New() // Middleware app.Use(middleware.Logger()) // Routes app.GET("/ping", func(c echo.Context) error { return c.String(200, "pong") }) app.POST("/v1/test", func(c echo.Context) error { var test Test1 if err := c.Bind(&test); err != nil { return err } log.Println("========================start io=====================") time.Sleep(time.Duration(1) * time.Second) log.Println("=========================end io=======================") return c.JSON(http.StatusOK, test) }) app.POST("/v2/test", func(c echo.Context) error { var test Test2 if err := c.Bind(&test); err != nil { return err } log.Println("========================start io=====================") time.Sleep(time.Duration(1) * time.Second) log.Println("=========================end io=======================") return c.JSON(http.StatusOK, test) }) app.POST("/v3/test", func(c echo.Context) error { var test Test2 if err := c.Bind(&test); err != nil { return err } log.Println("========================start io=====================") time.Sleep(time.Duration(1) * time.Second) log.Println("=========================end io=======================") return c.JSON(http.StatusOK, test) }) app.POST("/v4/test", func(c echo.Context) error { var test Test2 if err := c.Bind(&test); err != nil { return err } log.Println("========================start io=====================") time.Sleep(time.Duration(1) * time.Second) log.Println("=========================end io=======================") return c.JSON(http.StatusOK, test) }) // Start server app.Logger.Fatal(app.Start(":8080")) }
因爲要測試5種body樣本,4種場景,4個框架,所以把重點數據篩選出來(吞吐量、錯誤率和99%Line,重要性依次遞減),結果都繪製了圖形,方便比對查看。網絡
綜合以上各個測試結果能夠看出,gin以及iris都是很是優秀的框架,gin的優點比其餘稍微大點,iris次之,而echo相應差一點。
本次測試只是簡單測試了一下3個框架的併發和json相關。對比結果,不包括生態和工具的完善度等等。若是測試有什麼不完善的地方,歡迎交流。
另外歡迎你們試用和star另一個web框架baa,爲了避嫌我沒有貼出baa的數據,性能測試處於gin以後和iris之間。併發