本文使用
Zhihu On VSCodego-restful框架:
main_go-restful.go
```golang
package main
import (
"github.com/emicklei/go-restful"
"net/http"
)
func main(){
ws := new(restful.WebService)
ws.Route(ws.GET("/").To(hello))
restful.Add(ws)
http.ListenAndServe(":8080",nil)
}
func hello(req *restful.Request,resp *restful.Response){
resp.Write([]byte(""))
}
http返回报文:
$curl -i http://localhost:8080/
HTTP/1.1 200 OK
Date: Mon, 21 Oct 2019 03:54:27 GMT
Content-Length: 0
压测结果:
$wrk -d 100s -c 1024 -t 8 http://localhost:8080/
Running 2m test @ http://localhost:8080/
8 threads and 1024 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 19.72ms 10.57ms 331.94ms 87.67%
Req/Sec 6.52k 1.24k 23.75k 80.42%
5180908 requests in 1.67m, 370.57MB read
Socket errors: connect 0, read 844, write 3, timeout 0
Requests/sec: 51757.61
Transfer/sec: 3.70MB
资源占用:
go_restful
### go-echo框架:
#### main_go-echo.go
```golang
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, "")
})
e.Logger.Fatal(e.Start(":8080"))
}
http返回报文:
$ curl -i http://localhost:8080/
HTTP/1.1 200 OK
Content-Type: text/plain; charset=UTF-8
Date: Mon, 21 Oct 2019 04:09:24 GMT
Content-Length: 0
压测结果:
$ wrk -d 100s -c 1024 -t 8 http://localhost:8080/
Running 2m test @ http://localhost:8080/
8 threads and 1024 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 17.32ms 8.19ms 252.60ms 90.70%
Req/Sec 7.52k 1.35k 39.96k 80.55%
5974370 requests in 1.67m, 660.92MB read
Socket errors: connect 0, read 431, write 67, timeout 0
Requests/sec: 59686.09
Transfer/sec: 6.60MB
资源占用:
go_echo
go-iris框架:
main_go-iris.go
package main
import(
"time"
"github.com/kataras/iris"
"github.com/kataras/iris/cache"
)
func main(){
app := iris.New()
app.Logger().SetLevel("error")
app.Get("/",cache.Handler(10*time.Second),writeMarkdown)
app.Run(iris.Addr(":8080"))
}
func writeMarkdown(ctx iris.Context){
ctx.Markdown([]byte(""))
}
http返回报文:
$ curl -i http://localhost:8080/
HTTP/1.1 200 OK
Content-Type: text/html; charset=UTF-8
Date: Mon, 21 Oct 2019 04:11:59 GMT
Content-Length: 0
压测结果:
$ wrk -d 100s -c 1024 -t 8 http://localhost:8080/
Running 2m test @ http://localhost:8080/
8 threads and 1024 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 22.03ms 7.99ms 140.47ms 84.58%
Req/Sec 5.79k 775.23 19.31k 80.35%
4608572 requests in 1.67m, 505.43MB read
Socket errors: connect 0, read 726, write 22, timeout 0
Requests/sec: 46041.23
Transfer/sec: 5.05MB
资源占用:
go_iris
go-gin框架
main_go-gin.go
package main
import (
"fmt"
"net/http"
"log"
"github.com/julienschmidt/httprouter"
)
func Index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
fmt.Fprint(w, "")
}
func main() {
router := httprouter.New()
router.GET("/", Index)
log.Fatal(http.ListenAndServe(":8080", router))
}
http返回报文:
$ curl -i http://localhost:8080/
HTTP/1.1 200 OK
Date: Mon, 21 Oct 2019 04:15:33 GMT
Content-Length: 0
压测结果:
$ wrk -d 100s -c 1024 -t 8 http://localhost:8080/
Running 2m test @ http://localhost:8080/
8 threads and 1024 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 16.71ms 7.72ms 268.45ms 87.79%
Req/Sec 7.71k 1.58k 21.27k 82.12%
6130281 requests in 1.67m, 438.47MB read
Socket errors: connect 0, read 693, write 36, timeout 0
Requests/sec: 61243.74
Transfer/sec: 4.38MB
资源占用:
go_gin
go-chi框架:
main_go-chi.go
package main
import (
"net/http"
"github.com/go-chi/chi"
)
func main() {
r := chi.NewRouter()
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(""))
})
http.ListenAndServe(":8080", r)
}
http返回报文:
$ curl -i http://localhost:8080/
HTTP/1.1 200 OK
Date: Mon, 21 Oct 2019 04:18:42 GMT
Content-Length: 0
压测结果:
$ wrk -d 100s -c 1024 -t 8 http://localhost:8080/
Running 2m test @ http://localhost:8080/
8 threads and 1024 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 17.17ms 8.47ms 253.47ms 90.07%
Req/Sec 7.65k 1.42k 26.08k 79.76%
6071695 requests in 1.67m, 434.28MB read
Socket errors: connect 0, read 110, write 2, timeout 0
Requests/sec: 60658.49
Transfer/sec: 4.34MB