web框架性能对比wego与gin

web框架性能对比wego与gin

进行三种情况下的性能:

  • 输出简单的文本
  • 输出简单的json
  • 处理带有query参数的get请求

gin相关的代码

package main
import (
	"fmt"
	"github.com/gin-gonic/gin"
)
func handler_text(c *gin.Context)  {
	c.String(200, "hello world")
}
func handler_param(c *gin.Context) {
	name, _ := c.GetQuery("name")
	age, _ := c.GetQuery("age")
	c.String(200, "%s:%s", name, age)
}
func handler_json(c *gin.Context)  {
	type User struct {
		Name 	string
		Age 	int
	}
	user := User{Name:"lisi", Age:12}
	c.JSON(200, user)
}
func main()  {
	web := gin.New()
	web.Use(gin.Recovery())

	web.GET("/text", handler_text)
	web.GET("/json", handler_json)
	web.GET("/param", handler_param)

	fmt.Println("gin...")
	err := web.Run(":8081")
	if err != nil{
		fmt.Println(err)
	}
}

说明:wego内置Crash处理机制,因此在gin中也加入了gin.Recovery()

wego相关的代码

package main

import (
	"fmt"
	"github.com/haming123/wego"
)
func handler_text(c *wego.WebContext)  {
	c.WriteText(200, "hello world")
}
func handler_param(c *wego.WebContext) {
	name := c.QueryParam.GetString("name")
	age := c.QueryParam.GetString("age")
	c.WriteTextF(200, "%s:%s", name.Value, age.Value)
}
func handler_json(c *wego.WebContext)  {
	type User struct {
		Name 	string
		Age 	int
	}
	user := User{Name:"lisi", Age:12}
	c.WriteJSON(200, user)
}
func main()  {
	web, err := wego.NewWeb()
	if err != nil{
		fmt.Println(err)
		return
	}

	web.Config.ShowUrlLog = false
	web.GET("/text", handler_text)
	web.GET("/json", handler_json)
	web.GET("/param", handler_param)

	fmt.Println("wego...")
	err = web.Run(":8081")
	if err != nil {
		fmt.Println(err)
	}
}

性能比较结果

输出简单文本

ab -c 20 -n 20000 “http://127.0.0.1:8081/text”

  • gin
Requests per second:    18975.73 [#/sec] (mean)
Time per request:       1.054 [ms] (mean)
  • wego
Requests per second:    23528.61 [#/sec] (mean)
Time per request:       0.850 [ms] (mean)
输出简单josn

ab -c 20 -n 20000 “http://127.0.0.1:8081/json”

  • gin
Requests per second:    21963.59 [#/sec] (mean)
Time per request:       0.911 [ms] (mean)
  • wego
Requests per second:    21649.05 [#/sec] (mean)
Time per request:       0.924 [ms] (mean)
带有查询条件的文本输出

ab -c 20 -n 20000 “http://127.0.0.1:8081/param?name=demo&age=12”

  • gin
Requests per second:    17959.09 [#/sec] (mean)
Time per request:       1.114 [ms] (mean)
  • wego
Requests per second:    23399.28 [#/sec] (mean)
Time per request:       0.855 [ms] (mean)

你可能感兴趣的:(web框架,go,golang,开发语言)