Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Router benchmark #206

Open
wants to merge 4 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions misc/router-benchmark/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
## Router benchmark

Ptt-backend/misc/router-benchmark\
`go test -bench=. -benchmem`
```go
goos: linux
goarch: amd64
pkg: github.com/Ptt-official-app/Ptt-backend/misc/router-benchmark
cpu: Intel(R) Core(TM) i7-6700HQ CPU @ 2.60GHz
Benchmark_ServeMux-7 3167156 374.2 ns/op 80 B/op 1 allocs/op
Benchmark_gorillamux-7 760047 1560 ns/op 1312 B/op 10 allocs/op
Benchmark_httprouter-7 3195236 371.7 ns/op 504 B/op 5 allocs/op
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

用 httprouter@latest 版本 5 allocs/op 會變成 0

```

## Lines of code
`cloc routeServeMux.go`
```shell
-------------------------------------------------------------------------------
Language files blank comment code
-------------------------------------------------------------------------------
Go 1 6 9 53
-------------------------------------------------------------------------------
```

`cloc routeGorillamux.go`
```shell
-------------------------------------------------------------------------------
Language files blank comment code
-------------------------------------------------------------------------------
Go 1 2 1 11
-------------------------------------------------------------------------------
```

`cloc routeHttprouter.go`
```shell
-------------------------------------------------------------------------------
Language files blank comment code
-------------------------------------------------------------------------------
Go 1 2 1 11
-------------------------------------------------------------------------------
```
8 changes: 8 additions & 0 deletions misc/router-benchmark/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module github.com/Ptt-official-app/Ptt-backend/misc/router-benchmark

go 1.16

require (
github.com/gorilla/mux v1.8.0
github.com/julienschmidt/httprouter v1.3.0
)
4 changes: 4 additions & 0 deletions misc/router-benchmark/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/julienschmidt/httprouter v1.3.0 h1:U0609e9tgbseu3rBINet9P48AI/D3oJs4dN7jwJOQ1U=
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
14 changes: 14 additions & 0 deletions misc/router-benchmark/routeGorillamux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package router_benchmark

import (
"github.com/gorilla/mux"
"net/http"
)

// getBoards is the handler for `/v1/boards` with GET method
func getBoardArticles_gorillamux(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
boardID := params["boardID"]
_ = boardID
w.WriteHeader(200)
}
14 changes: 14 additions & 0 deletions misc/router-benchmark/routeHttprouter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package router_benchmark

import (
"github.com/julienschmidt/httprouter"
"net/http"
)

// getBoards is the handler for `/v1/boards` with GET method
func getBoardArticles_httprouter(w http.ResponseWriter, r *http.Request) {
params := httprouter.ParamsFromContext(r.Context())
boardID := params.ByName("boardID")
_ = boardID
w.WriteHeader(200)
}
68 changes: 68 additions & 0 deletions misc/router-benchmark/routeServeMux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package router_benchmark

import (
"net/http"
"strings"
)

// getBoards is the handler for `/v1/boards` with GET method
func getBoards(w http.ResponseWriter, r *http.Request) {
boardID, item, filename, _ := parseBoardPath(r.URL.Path)
if boardID == "" {
//delivery.getBoardList(w, r)
w.WriteHeader(http.StatusNotFound)
return
}
// get single board
if item == "information" {
w.WriteHeader(http.StatusNotFound)
return
} else if item == "settings" {
//delivery.getBoardSettings(w, r, boardID)
w.WriteHeader(http.StatusNotFound)
return
} else if item == "articles" {
if filename == "" {
getBoardArticles(w, r, boardID)
} else {
w.WriteHeader(http.StatusNotFound)
}
return
} else if item == "treasures" {
w.WriteHeader(http.StatusNotFound)
return
}

// 404
w.WriteHeader(http.StatusNotFound)
}

// parseBoardPath covert url path from /v1/boards/SYSOP/article to
// {SYSOP, article) or /v1/boards to {,}
func parseBoardPath(path string) (boardID string, item string, filename string, err error) {
pathSegment := strings.Split(path, "/")

if len(pathSegment) >= 6 {
boardID = pathSegment[3]
item = pathSegment[4]
filename = pathSegment[5]
return
} else if len(pathSegment) == 5 {
boardID = pathSegment[3]
item = pathSegment[4]
return
} else if len(pathSegment) == 4 {
boardID = pathSegment[3]
return
} else if len(pathSegment) == 3 {
return
}
return
}

// getBoardArticles handles request with `/v1/boards/SYSOP/articles` and will return
// article list to client
func getBoardArticles(w http.ResponseWriter, _ *http.Request, boardID string) {
_ = boardID
w.WriteHeader(200)
}
43 changes: 43 additions & 0 deletions misc/router-benchmark/routerbenchmark_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package router_benchmark

import (
"github.com/gorilla/mux"
"github.com/julienschmidt/httprouter"
"net/http"
"net/http/httptest"
"testing"
)

func Benchmark_ServeMux(b *testing.B) {
rr := httptest.NewRecorder()
r := http.NewServeMux()
r.HandleFunc("/v1/boards/", getBoards)
runTest(b, r, rr)
}
func Benchmark_gorillamux(b *testing.B) {
rr := httptest.NewRecorder()
r := mux.NewRouter().StrictSlash(true)
r.HandleFunc("/v1/boards/{boardID}/articles", getBoardArticles_gorillamux)
runTest(b, r, rr)
}
func Benchmark_httprouter(b *testing.B) {
rr := httptest.NewRecorder()
r := httprouter.New()
r.HandlerFunc(http.MethodGet, "/v1/boards/:boardID/articles", getBoardArticles_httprouter)
runTest(b, r, rr)
}

func runTest(b *testing.B, r http.Handler, rr *httptest.ResponseRecorder) {
req, err := http.NewRequest("GET", "/v1/boards/SYSOP/articles", nil)
if err != nil {
b.Fatal(err)
}

for i := 0; i < b.N; i++ {
r.ServeHTTP(rr, req)
if rr.Code != http.StatusOK {
b.Errorf("handler returned wrong status code: got %v want %v",
rr.Code, http.StatusOK)
}
}
}