长久以来大家在做go web开发时都会关注router的性能表现。julienschmidt/go-http-routing-benchmark库中囊括了近些年常见常用的router库,并且准备了static、github、google+等众多测试case,来测试router的内存占用以及寻找路由解析参数的性能。

原库的最后一次更新是4年前,由于当时Go标准库中的ServeMux不支持复杂的模式匹配,因此在这个库中,它只是作为一个内存占用的基准数据。在Go 1.22发布后,ServeMux支持了更强的模式匹配,理应将它囊括在所有测试中。此处fork了一个原库做了内容更新sillydong/go-http-routing-benchmark,增加了ServeMux的代码,也将其他库做了一次更新,能够更好的表现最新的性能数据。有兴趣的朋友可以clone到本地运行一下benchmark看。
先说结论,标准库的ServeMux在内存占用和路由匹配两项上的表现均不是最优的,但也不是最差的,属于中等水平,表现优于gorilla/mux很多,也低于gin很多。在性能表现不是那么critical的场景中,ServeMux是一个不错的选择,易用且无额外依赖。

内存占用

加粗了几个比较流行的router和各测试集中前三甲

RouterStaticGitHubGoogle+Parse
HttpServeMux76824 B121408 B7392 B13632 B
Ace30648 B48632 B3664 B6656 B
Aero30304 B488272 B25960 B28368 B
Bear30200 B82080 B7096 B12216 B
Beego98008 B150632 B10256 B19096 B
Bone40480 B100080 B6688 B11440 B
Chi83160 B94888 B8008 B9656 B
CloudyKitRouter30376 B93648 B6728 B11184 B
Denco10776 B37624 B3224 B4080 B
Echo97288 B123320 B11472 B14336 B
Gin34344 B58792 B4544 B7864 B
Gocraft Web55304 B95656 B7480 B12712 B
Goji27216 B46416 B2928 B5248 B
Gojiv2107904 B105648 B7264 B14400 B
Go-Json-Rest137872 B142192 B11360 B14096 B
GoRestful803336 B1254336 B72152 B119920 B
Gorilla Mux599496 B1319696 B68000 B105384 B
GowwwRouter24512 B78640 B5640 B9280 B
HttpRouter21680 B37088 B2760 B5024 B
HttpTreeMux73448 B78800 B7440 B7848 B
Kocha123656 B785632 B128880 B181712 B
LARS30640 B48592 B3656 B6632 B
Macaron38624 B93120 B8632 B13624 B
Martini314360 B474792 B24656 B43760 B
Pat20200 B19400 B1848 B2552 B
Possum87504 B79944 B6624 B8112 B
R2router23256 B46616 B3864 B6920 B
Rivet24608 B42840 B3064 B5680 B
Tango28264 B54840 B5168 B8920 B
TigerTonic78752 B96832 B9392 B9768 B
Traffic555248 B916720 B48176 B78832 B
Vulcan368744 B424960 B25648 B44224 B

路由性能

静态路由

BenchmarkHttpServeMux_StaticAll         5000     706222 ns/op          96 B/op        6 allocs/op
BenchmarkBeego_StaticAll                2000    1408954 ns/op      482433 B/op    14088 allocs/op
BenchmarkDenco_StaticAll              200000      12679 ns/op           0 B/op        0 allocs/op
BenchmarkGocraftWeb_StaticAll          10000     154142 ns/op       51468 B/op      947 allocs/op
BenchmarkGoji_StaticAll                20000      80518 ns/op           0 B/op        0 allocs/op
BenchmarkGoJsonRest_StaticAll           2000     978164 ns/op      180973 B/op     3945 allocs/op
BenchmarkGorillaMux_StaticAll           1000    1763690 ns/op       71804 B/op      956 allocs/op
BenchmarkHttpRouter_StaticAll         100000      15010 ns/op           0 B/op        0 allocs/op
BenchmarkHttpTreeMux_StaticAll        100000      15123 ns/op           0 B/op        0 allocs/op
BenchmarkKocha_StaticAll              100000      23093 ns/op           0 B/op        0 allocs/op
BenchmarkMartini_StaticAll               500    3444278 ns/op      156015 B/op     2351 allocs/op
BenchmarkPat_StaticAll                  1000    1640745 ns/op      549187 B/op    11186 allocs/op
BenchmarkTigerTonic_StaticAll          50000      58264 ns/op        7714 B/op      157 allocs/op
BenchmarkTraffic_StaticAll               500    7230129 ns/op     3763731 B/op    27453 allocs/op

带参数解析

BenchmarkHttpServeMux_Param                5465816           217.8 ns/op          16 B/op           1 allocs/op
BenchmarkAce_Param                        10911700           114.2 ns/op          32 B/op           1 allocs/op
BenchmarkAero_Param                       33411831            35.72 ns/op           0 B/op           0 allocs/op
BenchmarkBear_Param                        2238254           506.8 ns/op         456 B/op           5 allocs/op
BenchmarkBeego_Param                       1528132           786.7 ns/op         352 B/op           3 allocs/op
BenchmarkBone_Param                        1585542           754.6 ns/op         720 B/op           5 allocs/op
BenchmarkChi_Param                         1724193           713.2 ns/op         672 B/op           4 allocs/op
BenchmarkCloudyKitRouter_Param            56165307            20.97 ns/op           0 B/op           0 allocs/op
BenchmarkDenco_Param                      12856096            90.29 ns/op          32 B/op           1 allocs/op
BenchmarkEcho_Param                       25199661            46.97 ns/op           0 B/op           0 allocs/op
BenchmarkGin_Param                        23224945            48.94 ns/op           0 B/op           0 allocs/op
BenchmarkGocraftWeb_Param                  1605573           728.7 ns/op         640 B/op           8 allocs/op
BenchmarkGoji_Param                        3021921           392.1 ns/op         336 B/op           2 allocs/op
BenchmarkGojiv2_Param                      1000000          1004 ns/op        1040 B/op           8 allocs/op
BenchmarkGoJsonRest_Param                  1357318           863.6 ns/op         617 B/op          13 allocs/op
BenchmarkGoRestful_Param                    353190          3069 ns/op        4200 B/op          15 allocs/op
BenchmarkGorillaMux_Param                  1000000          1370 ns/op        1088 B/op           8 allocs/op
BenchmarkGowwwRouter_Param                 4125868           287.3 ns/op         336 B/op           2 allocs/op
BenchmarkHttpRouter_Param                 17156538            68.93 ns/op          32 B/op           1 allocs/op
BenchmarkHttpTreeMux_Param                 3461013           344.3 ns/op         352 B/op           3 allocs/op
BenchmarkKocha_Param                       6755576           180.8 ns/op          56 B/op           3 allocs/op
BenchmarkLARS_Param                       27456474            42.49 ns/op           0 B/op           0 allocs/op
BenchmarkMacaron_Param                      991389          1540 ns/op        1064 B/op          10 allocs/op
BenchmarkMartini_Param                      426780          3021 ns/op        1096 B/op          12 allocs/op
BenchmarkPat_Param                         1412851           840.1 ns/op         488 B/op           9 allocs/op
BenchmarkPossum_Param                      4397672           264.5 ns/op          40 B/op           2 allocs/op
BenchmarkR2router_Param                    3226189           367.9 ns/op         400 B/op           4 allocs/op
BenchmarkRivet_Param                      12100791           102.2 ns/op          48 B/op           1 allocs/op
BenchmarkTango_Param                       2218171           540.0 ns/op         192 B/op           6 allocs/op
BenchmarkTigerTonic_Param                  1000000          1371 ns/op         664 B/op          12 allocs/op
BenchmarkTraffic_Param                      532950          2213 ns/op        1824 B/op          20 allocs/op
BenchmarkVulcan_Param                      4017600           300.7 ns/op          98 B/op           3 allocs/op

BenchmarkHttpServeMux_Param5               1899558           622.2 ns/op         240 B/op           4 allocs/op
BenchmarkAce_Param5                        5213516           235.8 ns/op         160 B/op           1 allocs/op
BenchmarkAero_Param5                      21325314            54.20 ns/op           0 B/op           0 allocs/op
BenchmarkBear_Param5                       1648374           838.9 ns/op         501 B/op           5 allocs/op
BenchmarkBeego_Param5                      1000000          1043 ns/op         352 B/op           3 allocs/op
BenchmarkBone_Param5                       1000000          1103 ns/op         768 B/op           5 allocs/op
BenchmarkChi_Param5                        1000000          1037 ns/op         672 B/op           4 allocs/op
BenchmarkCloudyKitRouter_Param5           13417222            88.95 ns/op           0 B/op           0 allocs/op
BenchmarkDenco_Param5                      5516372           225.8 ns/op         160 B/op           1 allocs/op
BenchmarkEcho_Param5                      11434164           117.4 ns/op           0 B/op           0 allocs/op
BenchmarkGin_Param5                       12216440           101.1 ns/op           0 B/op           0 allocs/op
BenchmarkGocraftWeb_Param5                 1000000          1297 ns/op         912 B/op          11 allocs/op
BenchmarkGoji_Param5                       2165875           602.5 ns/op         336 B/op           2 allocs/op
BenchmarkGojiv2_Param5                     1000000          1501 ns/op        1104 B/op           8 allocs/op
BenchmarkGoJsonRest_Param5                  914564          1932 ns/op        1065 B/op          16 allocs/op
BenchmarkGoRestful_Param5                   299751          4197 ns/op        4312 B/op          15 allocs/op
BenchmarkGorillaMux_Param5                  608173          2531 ns/op        1152 B/op           8 allocs/op
BenchmarkGowwwRouter_Param5                2340406           486.8 ns/op         336 B/op           2 allocs/op
BenchmarkHttpRouter_Param5                 4883850           238.4 ns/op         160 B/op           1 allocs/op
BenchmarkHttpTreeMux_Param5                1000000          1064 ns/op         576 B/op           6 allocs/op
BenchmarkKocha_Param5                      1610242           733.0 ns/op         440 B/op          10 allocs/op
BenchmarkLARS_Param5                      15985417            75.08 ns/op           0 B/op           0 allocs/op
BenchmarkMacaron_Param5                     846705          1827 ns/op        1064 B/op          10 allocs/op
BenchmarkMartini_Param5                     296218          3723 ns/op        1256 B/op          13 allocs/op
BenchmarkPat_Param5                         603826          2320 ns/op         776 B/op          23 allocs/op
BenchmarkPossum_Param5                     4417070           287.4 ns/op          40 B/op           2 allocs/op
BenchmarkR2router_Param5                   2394753           499.5 ns/op         400 B/op           4 allocs/op
BenchmarkRivet_Param5                      3622023           333.3 ns/op         240 B/op           1 allocs/op
BenchmarkTango_Param5                      1641811           734.6 ns/op         312 B/op           6 allocs/op
BenchmarkTigerTonic_Param5                  230798          5374 ns/op        1975 B/op          27 allocs/op
BenchmarkTraffic_Param5                     315151          3718 ns/op        2176 B/op          26 allocs/op
BenchmarkVulcan_Param5                     2902212           415.9 ns/op          98 B/op           3 allocs/op

BenchmarkHttpServeMux_Param20               904251          1792 ns/op        1008 B/op           6 allocs/op
BenchmarkAce_Param20                       1714713           688.1 ns/op         704 B/op           1 allocs/op
BenchmarkAero_Param20                     51319618            23.26 ns/op           0 B/op           0 allocs/op
BenchmarkBear_Param20                       453313          2255 ns/op        1665 B/op           5 allocs/op
BenchmarkBeego_Param20                      486447          2452 ns/op         352 B/op           3 allocs/op
BenchmarkBone_Param20                       386766          3159 ns/op        1935 B/op           5 allocs/op
BenchmarkChi_Param20                        287170          4102 ns/op        2524 B/op           6 allocs/op
BenchmarkCloudyKitRouter_Param20           3026379           380.3 ns/op           0 B/op           0 allocs/op
BenchmarkDenco_Param20                     1634906           743.8 ns/op         704 B/op           1 allocs/op
BenchmarkEcho_Param20                      3685407           322.6 ns/op           0 B/op           0 allocs/op
BenchmarkGin_Param20                       4900233           246.3 ns/op           0 B/op           0 allocs/op
BenchmarkGocraftWeb_Param20                 237270          4828 ns/op        3787 B/op          15 allocs/op
BenchmarkGoji_Param20                       747334          1692 ns/op        1247 B/op           2 allocs/op
BenchmarkGojiv2_Param20                     671704          1724 ns/op        1344 B/op           8 allocs/op
BenchmarkGoJsonRest_Param20                 196104          6358 ns/op        4581 B/op          20 allocs/op
BenchmarkGoRestful_Param20                  158557          7094 ns/op        6660 B/op          17 allocs/op
BenchmarkGorillaMux_Param20                 214329          5394 ns/op        3259 B/op          10 allocs/op
BenchmarkGowwwRouter_Param20               2065264           613.4 ns/op         336 B/op           2 allocs/op
BenchmarkHttpRouter_Param20                1862535           630.7 ns/op         704 B/op           1 allocs/op
BenchmarkHttpTreeMux_Param20                278740          4072 ns/op        3195 B/op          10 allocs/op
BenchmarkKocha_Param20                      515193          2308 ns/op        1872 B/op          27 allocs/op
BenchmarkLARS_Param20                      6114734           213.3 ns/op           0 B/op           0 allocs/op
BenchmarkMacaron_Param20                    290316          4329 ns/op        2916 B/op          12 allocs/op
BenchmarkMartini_Param20                    165742          6670 ns/op        3620 B/op          15 allocs/op
BenchmarkPat_Param20                        125610          9461 ns/op        4071 B/op          72 allocs/op
BenchmarkPossum_Param20                    4433713           273.7 ns/op          40 B/op           2 allocs/op
BenchmarkR2router_Param20                   405236          2790 ns/op        2251 B/op           6 allocs/op
BenchmarkRivet_Param20                     1000000          1121 ns/op        1024 B/op           1 allocs/op
BenchmarkTango_Param20                     1000000          1338 ns/op         872 B/op           6 allocs/op
BenchmarkTigerTonic_Param20                  59702         19677 ns/op        9297 B/op          77 allocs/op
BenchmarkTraffic_Param20                     93975         12701 ns/op        7823 B/op          46 allocs/op
BenchmarkVulcan_Param20                    1748520           718.3 ns/op          98 B/op           3 allocs/op

获取参数写入response

BenchmarkHttpServeMux_ParamWrite           5198677           238.8 ns/op          16 B/op           1 allocs/op
BenchmarkAce_ParamWrite                    6888921           170.1 ns/op          40 B/op           2 allocs/op
BenchmarkAero_ParamWrite                  24208358            44.24 ns/op           0 B/op           0 allocs/op
BenchmarkBear_ParamWrite                   2210568           559.6 ns/op         456 B/op           5 allocs/op
BenchmarkBeego_ParamWrite                  1409678           839.1 ns/op         360 B/op           4 allocs/op
BenchmarkBone_ParamWrite                   1401434           808.6 ns/op         720 B/op           5 allocs/op
BenchmarkChi_ParamWrite                    1598197           746.2 ns/op         672 B/op           4 allocs/op
BenchmarkCloudyKitRouter_ParamWrite       50733813            21.48 ns/op           0 B/op           0 allocs/op
BenchmarkDenco_ParamWrite                 11273503           105.1 ns/op          32 B/op           1 allocs/op
BenchmarkEcho_ParamWrite                  10872991           119.0 ns/op           8 B/op           1 allocs/op
BenchmarkGin_ParamWrite                   15480328            74.31 ns/op           0 B/op           0 allocs/op
BenchmarkGocraftWeb_ParamWrite             1447512           813.3 ns/op         648 B/op           9 allocs/op
BenchmarkGoji_ParamWrite                   2840547           434.0 ns/op         336 B/op           2 allocs/op
BenchmarkGojiv2_ParamWrite                 1000000          1157 ns/op        1072 B/op          10 allocs/op
BenchmarkGoJsonRest_ParamWrite              966004          1448 ns/op        1096 B/op          18 allocs/op
BenchmarkGoRestful_ParamWrite               325435          3290 ns/op        4208 B/op          16 allocs/op
BenchmarkGorillaMux_ParamWrite             1000000          1456 ns/op        1088 B/op           8 allocs/op
BenchmarkGowwwRouter_ParamWrite            1412277           838.2 ns/op         752 B/op           6 allocs/op
BenchmarkHttpRouter_ParamWrite            13338344            84.50 ns/op          32 B/op           1 allocs/op
BenchmarkHttpTreeMux_ParamWrite            3159184           391.2 ns/op         352 B/op           3 allocs/op
BenchmarkKocha_ParamWrite                  5839240           200.1 ns/op          56 B/op           3 allocs/op
BenchmarkLARS_ParamWrite                  17950340            62.50 ns/op           0 B/op           0 allocs/op
BenchmarkMacaron_ParamWrite                 645147          1831 ns/op        1136 B/op          14 allocs/op
BenchmarkMartini_ParamWrite                 296262          3535 ns/op        1168 B/op          16 allocs/op
BenchmarkPat_ParamWrite                    1000000          1342 ns/op         912 B/op          13 allocs/op
BenchmarkPossum_ParamWrite                 4181599           275.3 ns/op          40 B/op           2 allocs/op
BenchmarkR2router_ParamWrite               2800455           440.3 ns/op         400 B/op           4 allocs/op
BenchmarkRivet_ParamWrite                  5687090           211.0 ns/op         112 B/op           2 allocs/op
BenchmarkTango_ParamWrite                  4022582           298.6 ns/op          96 B/op           3 allocs/op
BenchmarkTigerTonic_ParamWrite              654310          2212 ns/op        1104 B/op          17 allocs/op
BenchmarkTraffic_ParamWrite                 398692          3090 ns/op        2248 B/op          24 allocs/op
BenchmarkVulcan_ParamWrite                 3811351           313.2 ns/op          98 B/op           3 allocs/op

更多详细内容可以看更新后的readme.md