因为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之间。并发