多个go的web框架性能测试

****

结论:1. beego星最多,估计坑最少; 2.iris和echo qps最高,可以到1.5w; 3.大多数go框架qps都能到1w以上。

****

1.beego
  1.1 官网: https://beego.me/ 16.1k星
  1.2 安装: go get github.com/astaxie/beego
  1.3 新建一个web项目,必须在go/src下执行:bee new beewebdemo,然后在beewebdemo下运行 bee run,会进行自动编译,然后启动服务,在浏览器打开localhost:8080能看到初始页面
  1.4 新建一个api项目,必须在go/src下执行:bee api beewebdemo,然后在beewebdemo下运行 bee run,会进行自动编译,然后启动服务,在浏览器打开localhost:8080能看到初始页面
  1.5 新建一个简单的压测项目,a.go,代码如下:
----------
package main

import (
    "github.com/astaxie/beego"
)

type MainController struct {
    beego.Controller
}

func (this *MainController) Get() {
    this.Data["json"] = map[string]string{"ObjectId": "123"}
    this.ServeJSON()
}

func main() {
    beego.Router("/", &MainController{})
    beego.Run()
}
----------
  编译:go build -o a a.go
  压测:siege -c 100 -r 1000 -b http://127.0.0.1:8080

2. Gin
  2.1 官网: https://github.com/gin-gonic/gin 18k星
  2.2 安装:go get -u github.com/gin-gonic/gin
  2.3 简单代码压测
---------
package main

import "github.com/gin-gonic/gin"

func main() {
  gin.SetMode(gin.ReleaseMode)
    r := gin.Default()
    r.GET("/", func(c *gin.Context) {
        c.JSON(200, gin.H{
            "message": "pong",
        })
    })
    r.Run() // listen and serve on 0.0.0.0:8080
}
---------
    编译:go build -o a a.go
    压测:
    siege -c 100 -r 100 -b http://127.0.0.1:8080  qps可以上1万
    siege -c 100 -r 1000 -b http://127.0.0.1:8080  [error] socket: 2115135232 address is unavailable.: Cannot assign requested address  99%,qps 7k
  2.4 等价代码
-------
package main

import "github.com/gin-gonic/gin"

func getting(c *gin.Context){
  c.JSON(200, gin.H{
            "message": "pong",
  })
}

func main() {
  gin.SetMode(gin.ReleaseMode)
    r := gin.Default()
    r.GET("/", getting)
    r.Run() // listen and serve on 0.0.0.0:8080
}
-------
    
3.iris
  3.1 官网:https://github.com/kataras/iris 11k星
  3.2 安装:go get -u github.com/kataras/iris
  3.3 简单压测代码
------
package main

import (
    "github.com/kataras/iris"
)

type User struct {
    Firstname string `json:"firstname"`
    Age       int    `json:"age"`
}

func main() {
    app := iris.New()

    app.Get("/", func(ctx iris.Context) {
        peter := User{
            Firstname: "John",
            Age:       25,
        }
        ctx.JSON(peter)
      })
    
    app.Run(iris.Addr(":8080"))
}
------
  编译:go build -o a a.go
    压测:
    siege -c 100 -r 1000 -b http://127.0.0.1:8080  qps可以上1.5万,好。

4. revel
  4.1 官网:https://github.com/revel/revel 10k星
  4.2 安装:go get -u github.com/revel/cmd/revel
  4.3 有点类似beego,结构略复杂。对如何返回json这块没有demo,且在文档里没有足够说明,暂时先不考虑。

5. echo
  5.1 官网 https://github.com/labstack/echo  11k星
  5.2 安装:go get -u github.com/labstack/echo/...
  5.3 文档也不错
  5.4 简单压测代码
------
package main

import (
    "net/http"    
    "github.com/labstack/echo"
)

func main() {
    e := echo.New()
    e.GET("/", func(c echo.Context) error {
        return c.String(http.StatusOK, "Hello, World!")
    })
    e.Logger.Fatal(e.Start(":8080"))
}
------
  编译:go build -o a a.go
  压测:
  siege -c 100 -r 1000 -b http://127.0.0.1:8080  qps可以上1.5万,好。

6.faygo
  6.1 官网:https://github.com/henrylee2cn/faygo 1.2k星
  6.2 安装:go get -u -v github.com/henrylee2cn/faygo
  6.3 faygo的代码用法别扭,不推荐使用。且压测性能一般。
  6.3 简单压测代码
------
package main

import (
    "time"
    "github.com/henrylee2cn/faygo"
)

type Index struct {
    Id        int      `param:"<in:path> <required> <desc:ID> <range: 0:10>"`
    Title     string   `param:"<in:query> <nonzero>"`
    Paragraph []string `param:"<in:query> <name:p> <len: 1:10> <regexp: ^[\\w]*$>"`
    Cookie    string   `param:"<in:cookie> <name:faygoID>"`
}

func (i *Index) Serve(ctx *faygo.Context) error {
    if ctx.CookieParam("faygoID") == "" {
        ctx.SetCookie("faygoID", time.Now().String())
    }
    return ctx.JSON(200, i)
}

func main() {
    app := faygo.New("myapp", "0.1")
    app.GET("/index/:id", new(Index))
    faygo.Run()

------
  编译和压测跟其他相同。qps在1万左右。


7. tango
  7.1 官网: https://github.com/lunny/tango  732星,暂不考虑。

8. web.go
  8.1 官网: https://github.com/hoisie/web 3k星
  8.2 安装:go get github.com/hoisie/web
  8.3 评价:没有找到json api的相关说明,文档太少,example不够多,不推荐使用。
  8.4 简单压测代码
----
package main
    
import (
    "github.com/hoisie/web"
)
    
func hello(val string) string { return "hello " + val } 
    
func main() {
    web.Get("/(.*)", hello)
    web.Run("0.0.0.0:9999")
}
----
  qps在1.1万左右。

9. martini
  9.1 官网:https://github.com/go-martini/martini 10k星
  官方不再维护,不测试了。

10.beauty https://github.com/yang-f/beauty  41星

11.goku https://github.com/QLeelulu/goku 267星

12.Macaron 
  12.1 官网:https://github.com/go-macaron/macaron 2.5k星
  12.2 安装:
  12.3 文档比较全。但没有找到json相关的example或者说明。暂不考虑。
  12.4 简单压测代码
----
package main

import "gopkg.in/macaron.v1"

func main() {
    m := macaron.Classic()
    m.Get("/", func() string {
        return "Hello world!"
    })
    m.Run()
}
----
  qps在1万左右。


 

猜你喜欢

转载自blog.csdn.net/u011539200/article/details/81116124