go的gin框架的性能测试

最近可能想用用gin框架,刚好在studygolang网站上看到一篇文章,一个小伙测试gin的性能。所以想看看性能。

我想把php,原生的golang的http包,gin框架一起在本地做个测试。

环境是mac电脑。2核cpu,nginx启动两个子进程

测试都是通过wrk在10个并发100个请求压测30s内。

php使用的是nginx 1.6 php7.1,当然fpm可以调优。

<?php

echo "OK";

看看php的表现
wrk -c 1000 -t 10 -d 30 http://localhost/t_php.php
10 threads and 1000 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 26.97ms 78.99ms 1.45s 97.25%
Req/Sec 645.90 0.92k 4.13k 85.83%
16979 requests in 30.07s, 3.92MB read
Socket errors: connect 759, read 2609, write 0, timeout 241
Non-2xx or 3xx responses: 2887
Requests/sec: 564.62
Transfer/sec: 133.64KB

package main

import (
    "fmt"
    "log"
    "net/http"
)

func sayHello(w http.ResponseWriter, r *http.Request) {

    fmt.Fprintf(w, "OK")
}

func main() {
    http.HandleFunc("/", sayHello)
    err := http.ListenAndServe(":9091", nil)
    if err != nil {
        log.Fatal("ListenAndServe faild")
    }

}

看看golang http包的表现
wrk -c 1000 -t 10 -d 30 http://127.0.0.1:9091/
Running 30s test @ http://127.0.0.1:9091/
10 threads and 1000 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 4.38ms 4.99ms 125.73ms 94.79%
Req/Sec 7.66k 8.01k 42.53k 81.87%
1605625 requests in 30.10s, 180.69MB read
Socket errors: connect 757, read 0, write 0, timeout 0
Requests/sec: 53343.65
Transfer/sec: 6.00MB

package main

import (
    "time"

    "gopkg.in/gin-gonic/gin.v1"
    "net/http"
)

func main() {
    route := gin.Default()
    route.GET("/t_gin", func(c *gin.Context) {

        //time.Sleep(time.Millisecond * 1)

        c.String(http.StatusOK, "OK")
    })
    s := &http.Server{
        Addr:           ":1234",
        Handler:        route,
        ReadTimeout:    1 * time.Second,
        WriteTimeout:   1 * time.Second,
        MaxHeaderBytes: 1 << 20,
    }
    s.ListenAndServe()
}

使用gin的表现
wrk -c 1000 -t 10 -d 30 http://127.0.0.1:1234/t_gin
Running 30s test @ http://127.0.0.1:1234/t_gin
10 threads and 1000 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 10.28ms 22.14ms 549.70ms 96.86%
Req/Sec 8.08k 2.42k 22.08k 74.49%
717119 requests in 30.10s, 80.70MB read
Socket errors: connect 757, read 5, write 0, timeout 0
Requests/sec: 23825.29
Transfer/sec: 2.68MB

php原生的500多(而且还有超时),go的http包是50000多,go-gin是20000多

咱们改进测试条件,sleep 1毫秒试试。可以把这个sleep当做是读nosql缓存,或者做一次简单的db查询。

对于php

<?php
echo "OK";
usleep(1000);

wrk -c 1000 -t 10 -d 30 http://localhost/t_php.php
Running 30s test @ http://localhost/t_php.php
10 threads and 1000 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 29.99ms 107.48ms 1.55s 98.26%
Req/Sec 559.69 522.81 2.47k 72.73%
16925 requests in 30.08s, 3.67MB read
Socket errors: connect 759, read 379, write 0, timeout 234
Non-2xx or 3xx responses: 1003
Requests/sec: 562.69
Transfer/sec: 124.82KB

测试go的http包

package main

import (
    "fmt"
    "log"
    "net/http"
    "time"
)

func sayHello(w http.ResponseWriter, r *http.Request) {
    time.Sleep(time.Millisecond * 1)
    fmt.Fprintf(w, "OK")
}

func main() {
    http.HandleFunc("/", sayHello)
    err := http.ListenAndServe(":9091", nil)
    if err != nil {
        log.Fatal("ListenAndServe faild")
    }

}

wrk -c 1000 -t 10 -d 30 http://127.0.0.1:9091/
Running 30s test @ http://127.0.0.1:9091/
10 threads and 1000 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 5.20ms 3.35ms 94.99ms 86.35%
Req/Sec 9.09k 6.22k 24.26k 55.90%
1358150 requests in 30.08s, 152.84MB read
Socket errors: connect 757, read 0, write 0, timeout 0
Requests/sec: 45156.97
Transfer/sec: 5.08MB

使用gin,把上面的注释去掉

http://127.0.0.1:1234/t_gin
10 threads and 1000 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 10.03ms 16.90ms 439.73ms 96.60%
Req/Sec 7.99k 2.77k 17.01k 61.30%
708713 requests in 30.08s, 79.75MB read
Socket errors: connect 757, read 1, write 0, timeout 0
Requests/sec: 23563.86
Transfer/sec: 2.65MB

其实基本没啥变化。

当然测试有偏差,lnmp架构其实可以做更多的优化。

在这个场景下go性能大概是php的很多倍,但是开发效率来说,php比go高很多。

在业务逻辑多变,需要快速迭代的场景我们可以使用php,
在需要性能的时候可以使用golang,不用担心crash哦
不允许延时的场景咱们可以使用c++。

目前对于golang的使用的不是特别多,也没有踩很多坑,希望能多学学,规避golang的一些坑~~

猜你喜欢

转载自blog.csdn.net/cabing2005/article/details/78884138