V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
The Go Programming Language
http://golang.org/
Go Playground
Go Projects
Revel Web Framework
guonaihong
V2EX  ›  Go 编程语言

实现一个 http router 库玩玩。。。

  •  
  •   guonaihong ·
    guonaihong · 2020-11-20 09:19:12 +08:00 · 2179 次点击
    这是一个创建于 1465 天前的主题,其中的信息可能已经有所发展或是发生改变。

    brouter

    项目一开始时只是一个新的尝试,看能否性能比 httprouter 更快。

    项目地址

    https://github.com/antlabs/brouter

    demo

    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))
    }
    
    

    测试结果

    • httprouter 1.3
    • brouter 0.0.1
    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
    
    • httprouter fe77dd05ab5a80f54110cccf1b7d8681c2648323(在测试这版本时候遇到过 panic)
    • brouter 0.0.1
    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
    
    

    测试代码位置

    https://github.com/junelabs/brouter-benchmark

    8 条回复    2020-11-23 09:29:25 +08:00
    guonaihong
        1
    guonaihong  
    OP
       2020-11-20 12:54:41 +08:00
    发了这么会儿,都没人找我吹牛的。。。
    miniliuke
        2
    miniliuke  
       2020-11-20 12:57:50 +08:00
    @guonaihong 因为你没有整活
    guonaihong
        3
    guonaihong  
    OP
       2020-11-20 12:59:21 +08:00
    @miniliuke 啥是整活?
    eudore
        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
    guonaihong
        5
    guonaihong  
    OP
       2020-11-20 15:25:38 +08:00
    @eudore 哈哈,为了性能放弃了一些可读性。
    guonaihong
        6
    guonaihong  
    OP
       2020-11-20 15:34:41 +08:00
    @eudore httprouter 找儿子节点的方式会生成更少的汇编代码,所以效率很高。一开始我用查表的方式,都慢一点。是不是很违背直觉。。。
    eudore
        7
    eudore  
       2020-11-20 19:07:02 +08:00
    @guonaihong 首先高性能路由器使用 radix 和 trie 是标配,没有的就是低性能例如 mux 。

    在 httprouter 中如果节点是下面的路由规则越多,他的权重越高,那么这个节点在 childs 里面是顺序考前,这种规则在只访问一次的情况下,静态路由会强变量性能弱。

    而 httprouter 代码真心丑,函数复杂的贼高,还要没有匹配优先级。
    guonaihong
        8
    guonaihong  
    OP
       2020-11-23 09:29:25 +08:00
    @eudore 哈哈,同感,httprouter 的代码真是丑爆了。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2639 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 36ms · UTC 04:27 · PVG 12:27 · LAX 20:27 · JFK 23:27
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.