语言范围

https://www.techempower.com/benchmarks/

image.png

测试机器

MacBook Pro M1 16G

综合推荐

muxie

https://github.com/kataras/muxie

./wrk -t12 -c100 -d10s --latency http://127.0.0.1:8080
Running 10s test @ http://127.0.0.1:8080
  12 threads and 100 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency     1.67ms    4.09ms 112.98ms   92.94%
    Req/Sec    14.62k     5.77k   48.40k    78.08%
  Latency Distribution
     50%  376.00us
     75%    1.60ms
     90%    4.55ms
     99%   15.99ms
  1748079 requests in 10.04s, 255.07MB read
Requests/sec: 174190.64
Transfer/sec:     25.42MB
package main

import (
    "fmt"
    "net/http"

    "github.com/kataras/muxie"
)

func main() {
    mux := muxie.NewMux()

    // if it is true the /about/ will be permantly redirected to /about and served from the aboutHandler.
    // mux.PathCorrection = true

    mux.HandleFunc("/", indexHandler)
    mux.HandleFunc("/index", indexHandler)
    mux.HandleFunc("/about", aboutHandler)

    fmt.Println(`Server started at http://localhost:8080
Open your browser or any other HTTP Client and navigate to:
http://localhost:8080
http://localhost:8080/index and
http://localhost:8080/about`)

    http.ListenAndServe(":8080", mux)
}

func indexHandler(w http.ResponseWriter, r *http.Request) {
    w.Header().Set("Content-Type", "text/html;charset=utf8")
    fmt.Fprintf(w, "This is the <strong>%s</strong>", "index page")
}

func aboutHandler(w http.ResponseWriter, r *http.Request) {
    w.Write([]byte("Simple example to show how easy is to add routes with static paths.\nVisit the 'parameterized' example folder for more...\n"))
}

Iris

https://www.iris-go.com/docs/#/

./wrk -t12 -c100 -d10s --latency http://127.0.0.1:8080
Running 10s test @ http://127.0.0.1:8080
  12 threads and 100 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency     2.09ms    4.04ms  61.27ms   88.47%
    Req/Sec    14.48k     6.02k   56.29k    72.25%
  Latency Distribution
     50%  382.00us
     75%    1.78ms
     90%    7.04ms
     99%   19.12ms
  1735812 requests in 10.07s, 218.51MB read
Requests/sec: 172324.10
Transfer/sec:     21.69MB
package main

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

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

    app.Post("/send", func(ctx iris.Context) {
        var data string
        ctx.ReadBody(&data)
        cluster := ctx.URLParam("cluster")
        ctx.JSON(iris.Map{
            "data":    data,
            "cluster": cluster,
        })
    })

    // http://localhost:8080
    // Identical to: app.Run(iris.Addr(":8080"))
    app.Listen(":8080")
}

beego

中文支持好,文档详细

https://beego.me/docs/intro/

./wrk -t12 -c100 -d10s --latency http://127.0.0.1:8080
Running 10s test @ http://127.0.0.1:8080
  12 threads and 100 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency     3.58ms    5.03ms  66.11ms   87.39%
    Req/Sec     4.46k     1.51k   53.29k    99.58%
  Latency Distribution
     50%    1.06ms
     75%    5.44ms
     90%    9.81ms
     99%   22.88ms
  533640 requests in 10.10s, 1.06GB read
Requests/sec:  52832.92
Transfer/sec:    107.07MB
package main

import "github.com/beego/beego/v2/server/web"

func main() {
    web.Run()
}

高并发推荐(稳定性)

gin

https://gin-gonic.com/zh-cn/docs/quickstart/

./wrk -t12 -c100 -d10s --latency http://127.0.0.1:8080
Running 10s test @ http://127.0.0.1:8080
  12 threads and 100 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency     1.69ms    3.51ms  91.10ms   98.33%
    Req/Sec     5.83k     1.23k    8.95k    72.89%
  Latency Distribution
     50%    1.28ms
     75%    1.87ms
     90%    2.71ms
     99%    6.64ms
  699816 requests in 10.10s, 84.76MB read
Requests/sec:  69289.38
Transfer/sec:      8.39MB
package main

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

func main() {
    r := gin.Default()
    r.GET("/ping", func(c *gin.Context) {
        c.JSON(200, gin.H{
            "message": "pong",
        })
    })
    r.Run() // 监听并在 0.0.0.0:8080 上启动服务
}

chi

https://github.com/go-chi/chi

./wrk -t12 -c100 -d10s --latency http://127.0.0.1:3000
Running 10s test @ http://127.0.0.1:3000
  12 threads and 100 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency     2.60ms    2.41ms  25.77ms   88.40%
    Req/Sec     3.83k   451.59     6.65k    81.11%
  Latency Distribution
     50%    1.77ms
     75%    1.98ms
     90%    5.22ms
     99%   11.96ms
  460172 requests in 10.10s, 53.98MB read
Requests/sec:  45563.07
Transfer/sec:      5.34MB
package main

import (
    "net/http"

    "github.com/go-chi/chi/v5"
    "github.com/go-chi/chi/v5/middleware"
)

func main() {
    r := chi.NewRouter()
    r.Use(middleware.Logger)
    r.Get("/", func(w http.ResponseWriter, r *http.Request) {
        w.Write([]byte("welcome"))
    })
    http.ListenAndServe(":3000", r)
}

低并发推荐(高性能)

atreugo

https://github.com/savsgio/atreugo

./wrk -t12 -c100 -d10s --latency http://127.0.0.1:8000
Running 10s test @ http://127.0.0.1:8000
  12 threads and 100 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency    15.49ms   37.37ms 379.85ms   89.26%
    Req/Sec    22.83k    23.20k  173.29k    86.42%
  Latency Distribution
     50%  138.00us
     75%    6.88ms
     90%   57.14ms
     99%  192.59ms
  2502028 requests in 10.09s, 345.99MB read
Requests/sec: 247961.55
Transfer/sec:     34.29MB
package main

import (
    "github.com/savsgio/atreugo/v11"
)

func main() {
    config := atreugo.Config{
        Addr: "0.0.0.0:8000",
    }
    server := atreugo.New(config)

    server.GET("/", func(ctx *atreugo.RequestCtx) error {
        return ctx.TextResponse("Hello World")
    })

    server.GET("/echo/{path:*}", func(ctx *atreugo.RequestCtx) error {
        return ctx.TextResponse("Echo message: " + ctx.UserValue("path").(string))
    })

    v1 := server.NewGroupPath("/v1")
    v1.GET("/", func(ctx *atreugo.RequestCtx) error {
        return ctx.TextResponse("Hello V1 Group")
    })

    if err := server.ListenAndServe(); err != nil {
        panic(err)
    }
}

fasthttprouter

https://github.com/buaazp/fasthttprouter

./wrk -t12 -c100 -d10s --latency http://127.0.0.1:8080
Running 10s test @ http://127.0.0.1:8080
  12 threads and 100 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency    26.30ms   57.38ms 685.72ms   87.77%
    Req/Sec    27.03k    31.57k  180.92k    86.67%
  Latency Distribution
     50%  122.00us
     75%   19.03ms
     90%   98.92ms
     99%  255.59ms
  2766708 requests in 10.08s, 377.31MB read
Requests/sec: 274361.80
Transfer/sec:     37.42MB

gearbox

https://github.com/gogearbox/gearbox

./wrk -t12 -c100 -d10s --latency http://127.0.0.1:3000
Running 10s test @ http://127.0.0.1:3000
  12 threads and 100 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency    10.69ms   24.74ms 262.64ms   89.29%
    Req/Sec    19.79k    18.73k  163.80k    80.78%
  Latency Distribution
     50%  177.00us
     75%    6.68ms
     90%   38.03ms
     99%  118.55ms
  2271181 requests in 10.08s, 285.91MB read
Requests/sec: 225358.63
Transfer/sec:     28.37MB
package main

import (
    "github.com/gogearbox/gearbox"
)

func main() {
    // Setup gearbox
    gb := gearbox.New()

    // Define your handlers
    gb.Get("/hello", func(ctx gearbox.Context) {
        ctx.SendString("Hello World!")
    })

    // Start service
    gb.Start(":3000")
}
如果觉得我的文章对你有用,请随意赞赏