项目一开始时只是一个新的尝试,看能否性能比 httprouter 更快。
https://github.com/antlabs/brouter
package main
import (
"fmt"
"net/http"
"log"
"github.com/antlabs/brouter"
)
func Index(w http.ResponseWriter, r *http.Request, _ brouter.Params) {
fmt.Fprint(w, "Welcome!\n")
}
func Hello(w http.ResponseWriter, r *http.Request, ps brouter.Params) {
fmt.Fprintf(w, "hello, %s!\n", ps.ByName("name"))
}
func main() {
router := brouter.New()
router.GET("/", Index)
router.GET("/hello/:name", Hello)
log.Fatal( http.ListenAndServe(":8080", router))
}
GithubAPI Routes: 180
GithubAPI2 Routes: 203
BeegoMuxRouter: 97024 Bytes
BoneRouter: 86368 Bytes
ChiRouter: 64584 Bytes
HttpRouter: 35360 Bytes
BRouter: 51096 Bytes
trie-mux: 121736 Bytes
MuxRouter: 1373064 Bytes
GoRouter1: 76528 Bytes
GoRouter2: 84624 Bytes
#Static Routes: 157
HttpRouter: 21680 Bytes
BRouter: 35208 Bytes
goos: linux
goarch: amd64
pkg: test
BenchmarkBeegoMuxRouterWithGithubAPI-4 10000 111713 ns/op 116352 B/op 900 allocs/op
BenchmarkBoneRouterWithGithubAPI-4 721 1546779 ns/op 562878 B/op 6807 allocs/op
BenchmarkTrieMuxRouterWithGithubAPI-4 19060 64217 ns/op 57024 B/op 468 allocs/op
BenchmarkBRouterWithGithubAPI-4 70467 16971 ns/op 0 B/op 0 allocs/op
BenchmarkHttpRouterWithGithubAPI-4 45830 26609 ns/op 11744 B/op 144 allocs/op
BenchmarkGoRouter1WithGithubAPI-4 24042 49632 ns/op 11920 B/op 360 allocs/op
BenchmarkGoRouter2WithGithubAPI2-4 21088 57408 ns/op 13832 B/op 406 allocs/op
BenchmarkChiRouterWithGithubAPI2-4 8469 128427 ns/op 106000 B/op 1110 allocs/op
BenchmarkMuxRouterWithGithubAPI2-4 358 3414899 ns/op 59373 B/op 992 allocs/op
BenchmarkHttpRouter_StaticAll-4 120054 9994 ns/op 0 B/op 0 allocs/op
BenchmarkBRouter_StaticAll-4 111614 10838 ns/op 0 B/op 0 allocs/op
PASS
ok test 15.950s
GithubAPI Routes: 180
GithubAPI2 Routes: 203
BeegoMuxRouter: 97232 Bytes
BoneRouter: 88296 Bytes
ChiRouter: 64592 Bytes
HttpRouter: 33480 Bytes
BRouter: 51096 Bytes
trie-mux: 123448 Bytes
MuxRouter: 1373064 Bytes
GoRouter1: 76320 Bytes
GoRouter2: 85040 Bytes
#Static Routes: 157
HttpRouter: 21712 Bytes
BRouter: 35200 Bytes
goos: linux
goarch: amd64
pkg: test
BenchmarkBeegoMuxRouterWithGithubAPI-4 10000 111217 ns/op 116352 B/op 900 allocs/op
BenchmarkBoneRouterWithGithubAPI-4 780 1554833 ns/op 562871 B/op 6807 allocs/op
BenchmarkTrieMuxRouterWithGithubAPI-4 19020 62882 ns/op 57024 B/op 468 allocs/op
BenchmarkBRouterWithGithubAPI-4 69400 17426 ns/op 0 B/op 0 allocs/op
BenchmarkHttpRouterWithGithubAPI-4 75178 15907 ns/op 0 B/op 0 allocs/op
BenchmarkGoRouter1WithGithubAPI-4 24426 49969 ns/op 11920 B/op 360 allocs/op
BenchmarkGoRouter2WithGithubAPI2-4 21087 56849 ns/op 13832 B/op 406 allocs/op
BenchmarkChiRouterWithGithubAPI2-4 8490 129326 ns/op 105974 B/op 1110 allocs/op
BenchmarkMuxRouterWithGithubAPI2-4 349 3383843 ns/op 59369 B/op 992 allocs/op
BenchmarkHttpRouter_StaticAll-4 120390 10094 ns/op 0 B/op 0 allocs/op
BenchmarkBRouter_StaticAll-4 113125 10633 ns/op 0 B/op 0 allocs/op
PASS
ok test 15.887s
1
guonaihong OP 发了这么会儿,都没人找我吹牛的。。。
|
2
miniliuke 2020-11-20 12:57:50 +08:00
@guonaihong 因为你没有整活
|
3
guonaihong OP @miniliuke 啥是整活?
|
4
eudore 2020-11-20 14:20:41 +08:00
感觉我的比你简单点,我也贴下我的路由器和性能测试,性能是 httprouter 的 70-90%内存更少功能更多,节点匹配顺序测试和 httprouter 不同,mux 这个库感觉性能低,但是没想到性能差几百倍了。
https://github.com/eudore/eudore/blob/master/routerstd.go https://github.com/eudore/web-framework-benchmark |
5
guonaihong OP @eudore 哈哈,为了性能放弃了一些可读性。
|
6
guonaihong OP @eudore httprouter 找儿子节点的方式会生成更少的汇编代码,所以效率很高。一开始我用查表的方式,都慢一点。是不是很违背直觉。。。
|
7
eudore 2020-11-20 19:07:02 +08:00
@guonaihong 首先高性能路由器使用 radix 和 trie 是标配,没有的就是低性能例如 mux 。
在 httprouter 中如果节点是下面的路由规则越多,他的权重越高,那么这个节点在 childs 里面是顺序考前,这种规则在只访问一次的情况下,静态路由会强变量性能弱。 而 httprouter 代码真心丑,函数复杂的贼高,还要没有匹配优先级。 |
8
guonaihong OP @eudore 哈哈,同感,httprouter 的代码真是丑爆了。
|