为go-http-routing-benchmark增加了Go 1.22新ServeMux的测试内容
长久以来大家在做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和各测试集中前三甲
Router | Static | GitHub | Google+ | Parse |
---|---|---|---|---|
HttpServeMux | 76824 B | 121408 B | 7392 B | 13632 B |
Ace | 30648 B | 48632 B | 3664 B | 6656 B |
Aero | 30304 B | 488272 B | 25960 B | 28368 B |
Bear | 30200 B | 82080 B | 7096 B | 12216 B |
Beego | 98008 B | 150632 B | 10256 B | 19096 B |
Bone | 40480 B | 100080 B | 6688 B | 11440 B |
Chi | 83160 B | 94888 B | 8008 B | 9656 B |
CloudyKitRouter | 30376 B | 93648 B | 6728 B | 11184 B |
Denco | 10776 B | 37624 B | 3224 B | 4080 B |
Echo | 97288 B | 123320 B | 11472 B | 14336 B |
Gin | 34344 B | 58792 B | 4544 B | 7864 B |
Gocraft Web | 55304 B | 95656 B | 7480 B | 12712 B |
Goji | 27216 B | 46416 B | 2928 B | 5248 B |
Gojiv2 | 107904 B | 105648 B | 7264 B | 14400 B |
Go-Json-Rest | 137872 B | 142192 B | 11360 B | 14096 B |
GoRestful | 803336 B | 1254336 B | 72152 B | 119920 B |
Gorilla Mux | 599496 B | 1319696 B | 68000 B | 105384 B |
GowwwRouter | 24512 B | 78640 B | 5640 B | 9280 B |
HttpRouter | 21680 B | 37088 B | 2760 B | 5024 B |
HttpTreeMux | 73448 B | 78800 B | 7440 B | 7848 B |
Kocha | 123656 B | 785632 B | 128880 B | 181712 B |
LARS | 30640 B | 48592 B | 3656 B | 6632 B |
Macaron | 38624 B | 93120 B | 8632 B | 13624 B |
Martini | 314360 B | 474792 B | 24656 B | 43760 B |
Pat | 20200 B | 19400 B | 1848 B | 2552 B |
Possum | 87504 B | 79944 B | 6624 B | 8112 B |
R2router | 23256 B | 46616 B | 3864 B | 6920 B |
Rivet | 24608 B | 42840 B | 3064 B | 5680 B |
Tango | 28264 B | 54840 B | 5168 B | 8920 B |
TigerTonic | 78752 B | 96832 B | 9392 B | 9768 B |
Traffic | 555248 B | 916720 B | 48176 B | 78832 B |
Vulcan | 368744 B | 424960 B | 25648 B | 44224 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
相关文章
上一篇: Go 1.22 新ServeMux的Proposal没有下一篇