Skip to content

Instantly share code, notes, and snippets.

@pkieltyka
Last active February 9, 2024 18:51
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save pkieltyka/123032f12052520aaccab752bd3e78cc to your computer and use it in GitHub Desktop.
Save pkieltyka/123032f12052520aaccab752bd3e78cc to your computer and use it in GitHub Desktop.
##
## HTTP Router benchmarks -- Nov 29, 2020 with Go 1.15.5 on Linux AMD 3950x
##
## This benchmark suite is based on https://github.com/julienschmidt/go-http-routing-benchmark
## using the most up-to-date version of each pkg as of today. Each router has their own
## pros and cons, so consider the designs of each router to suit your application.
##
## *NOTE*: the memory reports below by the go benchmark tool look quite wrong, as there must
## be a bug somewhere in the go bench tool with the Go version I'm running. I will re-run
## with future versions and report back. However, in general you'll want to look at the "ns/op"
## metric, as memory to construct a routing tree isn't going to be a bottleneck in any router.
##
➜ go-http-routing-benchmark git:(master) ✗ go test -bench="Chi" .
#GithubAPI Routes: 203
Chi: 90848 Bytes
#GPlusAPI Routes: 13
Chi: 8024 Bytes
#ParseAPI Routes: 26
Chi: 9744 Bytes
#Static Routes: 157
Chi: 82432 Bytes
goos: linux
goarch: amd64
pkg: github.com/pkieltyka/go-http-routing-benchmark
BenchmarkChi_Param 3075895 384 ns/op 400 B/op 2 allocs/op
BenchmarkChi_Param5 2116603 566 ns/op 400 B/op 2 allocs/op
BenchmarkChi_Param20 964117 1227 ns/op 400 B/op 2 allocs/op
BenchmarkChi_ParamWrite 2863413 420 ns/op 400 B/op 2 allocs/op
BenchmarkChi_GithubStatic 3045488 395 ns/op 400 B/op 2 allocs/op
BenchmarkChi_GithubParam 2204115 540 ns/op 400 B/op 2 allocs/op
BenchmarkChi_GithubAll 10000 113811 ns/op 81203 B/op 406 allocs/op
BenchmarkChi_GPlusStatic 3337485 359 ns/op 400 B/op 2 allocs/op
BenchmarkChi_GPlusParam 2825853 423 ns/op 400 B/op 2 allocs/op
BenchmarkChi_GPlus2Params 2471697 483 ns/op 400 B/op 2 allocs/op
BenchmarkChi_GPlusAll 194220 5950 ns/op 5200 B/op 26 allocs/op
BenchmarkChi_ParseStatic 3365324 356 ns/op 400 B/op 2 allocs/op
BenchmarkChi_ParseParam 2976614 404 ns/op 400 B/op 2 allocs/op
BenchmarkChi_Parse2Params 2638084 439 ns/op 400 B/op 2 allocs/op
BenchmarkChi_ParseAll 109567 11295 ns/op 10400 B/op 52 allocs/op
BenchmarkChi_StaticAll 16846 71308 ns/op 62802 B/op 314 allocs/op
PASS
ok github.com/pkieltyka/go-http-routing-benchmark 24.887s
[peter@pak ~/Dev/go/src/github.com/pkieltyka/go-http-routing-benchmark]$ go test -bench="Chi|HttpRouter|Goji|Martini|Gorilla|Gocraft|Gin" .
#GithubAPI Routes: 203
Chi: 90848 Bytes
Gin: 22320 Bytes
GocraftWeb: 58256 Bytes
Goji: 46480 Bytes
GorillaMux: 1323192 Bytes
HttpRouter: 18446744073709551520 Bytes
Martini: 485688 Bytes
#GPlusAPI Routes: 13
Chi: 8056 Bytes
Gin: 18446744073709519072 Bytes
GocraftWeb: 18446744073709521936 Bytes
Goji: 2992 Bytes
GorillaMux: 66584 Bytes
HttpRouter: 18446744073709517184 Bytes
Martini: 24344 Bytes
#ParseAPI Routes: 26
Chi: 9776 Bytes
Gin: 18446744073709522416 Bytes
GocraftWeb: 18446744073709527240 Bytes
Goji: 5312 Bytes
GorillaMux: 106256 Bytes
HttpRouter: 18446744073709519448 Bytes
Martini: 46312 Bytes
#Static Routes: 157
Chi: 82464 Bytes
Gin: 18446744073709548672 Bytes
GocraftWeb: 18336 Bytes
Goji: 27280 Bytes
GorillaMux: 586192 Bytes
HttpRouter: 18446744073709535472 Bytes
Martini: 310912 Bytes
goos: linux
goarch: amd64
pkg: github.com/pkieltyka/go-http-routing-benchmark
BenchmarkChi_Param 2238188 539 ns/op 400 B/op 2 allocs/op
BenchmarkGin_Param 25298308 47.6 ns/op 0 B/op 0 allocs/op
BenchmarkGocraftWeb_Param 1436232 828 ns/op 648 B/op 8 allocs/op
BenchmarkGoji_Param 3012026 398 ns/op 336 B/op 2 allocs/op
BenchmarkGorillaMux_Param 737485 1665 ns/op 1312 B/op 10 allocs/op
BenchmarkHttpRouter_Param 32143945 37.3 ns/op 0 B/op 0 allocs/op
BenchmarkMartini_Param 390961 3056 ns/op 1072 B/op 10 allocs/op
BenchmarkChi_Param5 1630903 736 ns/op 400 B/op 2 allocs/op
BenchmarkGin_Param5 15405165 76.3 ns/op 0 B/op 0 allocs/op
BenchmarkGocraftWeb_Param5 1000000 1262 ns/op 920 B/op 11 allocs/op
BenchmarkGoji_Param5 2192293 541 ns/op 336 B/op 2 allocs/op
BenchmarkGorillaMux_Param5 509416 2426 ns/op 1376 B/op 10 allocs/op
BenchmarkHttpRouter_Param5 17660745 66.9 ns/op 0 B/op 0 allocs/op
BenchmarkMartini_Param5 337567 3587 ns/op 1232 B/op 11 allocs/op
BenchmarkChi_Param20 819500 1455 ns/op 400 B/op 2 allocs/op
BenchmarkGin_Param20 6373621 187 ns/op 0 B/op 0 allocs/op
BenchmarkGocraftWeb_Param20 264315 4576 ns/op 3795 B/op 15 allocs/op
BenchmarkGoji_Param20 776336 1589 ns/op 1247 B/op 2 allocs/op
BenchmarkGorillaMux_Param20 233949 5205 ns/op 3483 B/op 12 allocs/op
BenchmarkHttpRouter_Param20 6918880 174 ns/op 0 B/op 0 allocs/op
BenchmarkMartini_Param20 179708 6745 ns/op 3596 B/op 13 allocs/op
BenchmarkChi_ParamWrite 2129954 563 ns/op 400 B/op 2 allocs/op
BenchmarkGin_ParamWrite 11556357 104 ns/op 0 B/op 0 allocs/op
BenchmarkGocraftWeb_ParamWrite 1320537 907 ns/op 656 B/op 9 allocs/op
BenchmarkGoji_ParamWrite 2786125 431 ns/op 336 B/op 2 allocs/op
BenchmarkGorillaMux_ParamWrite 709005 1722 ns/op 1312 B/op 10 allocs/op
BenchmarkHttpRouter_ParamWrite 16573574 75.0 ns/op 0 B/op 0 allocs/op
BenchmarkMartini_ParamWrite 349449 3453 ns/op 1176 B/op 14 allocs/op
BenchmarkChi_GithubStatic 2354187 511 ns/op 400 B/op 2 allocs/op
BenchmarkGin_GithubStatic 21571580 55.7 ns/op 0 B/op 0 allocs/op
BenchmarkGocraftWeb_GithubStatic 2268847 542 ns/op 296 B/op 5 allocs/op
BenchmarkGoji_GithubStatic 7435953 163 ns/op 0 B/op 0 allocs/op
BenchmarkGorillaMux_GithubStatic 319627 3784 ns/op 1008 B/op 9 allocs/op
BenchmarkHttpRouter_GithubStatic 38682040 30.9 ns/op 0 B/op 0 allocs/op
BenchmarkMartini_GithubStatic 224286 5584 ns/op 768 B/op 9 allocs/op
BenchmarkChi_GithubParam 1729479 702 ns/op 400 B/op 2 allocs/op
BenchmarkGin_GithubParam 13247941 90.7 ns/op 0 B/op 0 allocs/op
BenchmarkGocraftWeb_GithubParam 1000000 1018 ns/op 712 B/op 9 allocs/op
BenchmarkGoji_GithubParam 1884424 643 ns/op 336 B/op 2 allocs/op
BenchmarkGorillaMux_GithubParam 207460 5664 ns/op 1328 B/op 10 allocs/op
BenchmarkHttpRouter_GithubParam 14765768 82.1 ns/op 0 B/op 0 allocs/op
BenchmarkMartini_GithubParam 169917 7178 ns/op 1152 B/op 11 allocs/op
BenchmarkChi_GithubAll 9198 145802 ns/op 81201 B/op 406 allocs/op
BenchmarkGin_GithubAll 64491 18319 ns/op 0 B/op 0 allocs/op
BenchmarkGocraftWeb_GithubAll 5851 205074 ns/op 131656 B/op 1686 allocs/op
BenchmarkGoji_GithubAll 3951 299385 ns/op 56113 B/op 334 allocs/op
BenchmarkGorillaMux_GithubAll 441 2647630 ns/op 258151 B/op 1994 allocs/op
BenchmarkHttpRouter_GithubAll 79896 14614 ns/op 0 B/op 0 allocs/op
BenchmarkMartini_GithubAll 478 2487206 ns/op 226562 B/op 2325 allocs/op
BenchmarkChi_GPlusStatic 2482555 485 ns/op 400 B/op 2 allocs/op
BenchmarkGin_GPlusStatic 26257400 45.6 ns/op 0 B/op 0 allocs/op
BenchmarkGocraftWeb_GPlusStatic 2469060 486 ns/op 280 B/op 5 allocs/op
BenchmarkGoji_GPlusStatic 9687280 122 ns/op 0 B/op 0 allocs/op
BenchmarkGorillaMux_GPlusStatic 1000000 1213 ns/op 1008 B/op 9 allocs/op
BenchmarkHttpRouter_GPlusStatic 59711812 20.2 ns/op 0 B/op 0 allocs/op
BenchmarkMartini_GPlusStatic 460586 2720 ns/op 768 B/op 9 allocs/op
BenchmarkChi_GPlusParam 2094721 573 ns/op 400 B/op 2 allocs/op
BenchmarkGin_GPlusParam 19765256 60.8 ns/op 0 B/op 0 allocs/op
BenchmarkGocraftWeb_GPlusParam 1447848 830 ns/op 648 B/op 8 allocs/op
BenchmarkGoji_GPlusParam 2741642 439 ns/op 336 B/op 2 allocs/op
BenchmarkGorillaMux_GPlusParam 546668 2231 ns/op 1312 B/op 10 allocs/op
BenchmarkHttpRouter_GPlusParam 22074862 54.7 ns/op 0 B/op 0 allocs/op
BenchmarkMartini_GPlusParam 366627 3252 ns/op 1072 B/op 10 allocs/op
BenchmarkChi_GPlus2Params 1855843 645 ns/op 400 B/op 2 allocs/op
BenchmarkGin_GPlus2Params 15891129 75.3 ns/op 0 B/op 0 allocs/op
BenchmarkGocraftWeb_GPlus2Params 1207384 995 ns/op 712 B/op 9 allocs/op
BenchmarkGoji_GPlus2Params 1849291 651 ns/op 336 B/op 2 allocs/op
BenchmarkGorillaMux_GPlus2Params 271723 4459 ns/op 1328 B/op 10 allocs/op
BenchmarkHttpRouter_GPlus2Params 17192130 68.5 ns/op 0 B/op 0 allocs/op
BenchmarkMartini_GPlus2Params 178384 6789 ns/op 1200 B/op 13 allocs/op
BenchmarkChi_GPlusAll 147946 8153 ns/op 5200 B/op 26 allocs/op
BenchmarkGin_GPlusAll 1386764 864 ns/op 0 B/op 0 allocs/op
BenchmarkGocraftWeb_GPlusAll 105315 11489 ns/op 8040 B/op 103 allocs/op
BenchmarkGoji_GPlusAll 188845 6476 ns/op 3696 B/op 22 allocs/op
BenchmarkGorillaMux_GPlusAll 34441 34722 ns/op 16528 B/op 128 allocs/op
BenchmarkHttpRouter_GPlusAll 1735436 688 ns/op 0 B/op 0 allocs/op
BenchmarkMartini_GPlusAll 22372 53754 ns/op 14017 B/op 145 allocs/op
BenchmarkChi_ParseStatic 2452401 488 ns/op 400 B/op 2 allocs/op
BenchmarkGin_ParseStatic 25390861 46.4 ns/op 0 B/op 0 allocs/op
BenchmarkGocraftWeb_ParseStatic 2242942 534 ns/op 296 B/op 5 allocs/op
BenchmarkGoji_ParseStatic 7799596 154 ns/op 0 B/op 0 allocs/op
BenchmarkGorillaMux_ParseStatic 827152 1482 ns/op 1008 B/op 9 allocs/op
BenchmarkHttpRouter_ParseStatic 57204733 20.9 ns/op 0 B/op 0 allocs/op
BenchmarkMartini_ParseStatic 426754 2900 ns/op 768 B/op 9 allocs/op
BenchmarkChi_ParseParam 2146852 558 ns/op 400 B/op 2 allocs/op
BenchmarkGin_ParseParam 23554419 50.1 ns/op 0 B/op 0 allocs/op
BenchmarkGocraftWeb_ParseParam 1400986 857 ns/op 664 B/op 8 allocs/op
BenchmarkGoji_ParseParam 2432464 492 ns/op 336 B/op 2 allocs/op
BenchmarkGorillaMux_ParseParam 721842 1726 ns/op 1312 B/op 10 allocs/op
BenchmarkHttpRouter_ParseParam 29388786 40.7 ns/op 0 B/op 0 allocs/op
BenchmarkMartini_ParseParam 387662 3089 ns/op 1072 B/op 10 allocs/op
BenchmarkChi_Parse2Params 1950382 613 ns/op 400 B/op 2 allocs/op
BenchmarkGin_Parse2Params 19959118 59.9 ns/op 0 B/op 0 allocs/op
BenchmarkGocraftWeb_Parse2Params 1000000 1017 ns/op 712 B/op 9 allocs/op
BenchmarkGoji_Parse2Params 2369763 492 ns/op 336 B/op 2 allocs/op
BenchmarkGorillaMux_Parse2Params 591013 2079 ns/op 1328 B/op 10 allocs/op
BenchmarkHttpRouter_Parse2Params 24001773 49.3 ns/op 0 B/op 0 allocs/op
BenchmarkMartini_Parse2Params 356697 3287 ns/op 1152 B/op 11 allocs/op
BenchmarkChi_ParseAll 77850 15395 ns/op 10400 B/op 52 allocs/op
BenchmarkGin_ParseAll 769875 1530 ns/op 0 B/op 0 allocs/op
BenchmarkGocraftWeb_ParseAll 58406 20793 ns/op 13728 B/op 181 allocs/op
BenchmarkGoji_ParseAll 114027 10611 ns/op 5376 B/op 32 allocs/op
BenchmarkGorillaMux_ParseAll 17644 68067 ns/op 31120 B/op 250 allocs/op
BenchmarkHttpRouter_ParseAll 1000000 1071 ns/op 0 B/op 0 allocs/op
BenchmarkMartini_ParseAll 13372 89757 ns/op 25073 B/op 253 allocs/op
BenchmarkChi_StaticAll 12892 93797 ns/op 62801 B/op 314 allocs/op
BenchmarkGin_StaticAll 91494 13017 ns/op 0 B/op 0 allocs/op
BenchmarkGocraftWeb_StaticAll 13209 91072 ns/op 46440 B/op 785 allocs/op
BenchmarkGoji_StaticAll 34119 35224 ns/op 0 B/op 0 allocs/op
BenchmarkGorillaMux_StaticAll 1602 695297 ns/op 158356 B/op 1421 allocs/op
BenchmarkHttpRouter_StaticAll 163388 7109 ns/op 0 B/op 0 allocs/op
PASS
ok github.com/pkieltyka/go-http-routing-benchmark 162.257s
@gerred
Copy link

gerred commented Dec 7, 2020

@pkieltyka HttpRouter memory consumption seems off? Same with Gocraftweb and Gin in some benchmarks.

@pkieltyka
Copy link
Author

@gerred lol yes, I agree looks super whacky, but that is what the go toolchain reported on my system. Looks like a bug somewhere in the benchmark tool. Feel free to post your results if you run it, I'll eventually post updated results and hopefully the memory measure reports normally.

@SohelAhmedJoni
Copy link

SohelAhmedJoni commented Feb 9, 2024

⚠️This comment is not thoroughly verified or tested

The results of the benchmark show that HttpRouter is the fastest router in most cases. It also has the lowest memory usage and allocations.

Gin is the second fastest router, but it has slightly higher memory usage and allocations than HttpRouter.

Chi is the third fastest router, but it has significantly higher memory usage and allocations than HttpRouter or Gin.

The other routers are significantly slower than HttpRouter, Gin, and Chi.

The benchmark also shows that the performance of the routers can vary depending on the type of route being tested. For example, HttpRouter is the fastest router for static routes, but it is not the fastest router for routes with parameters.

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment