| 2019 | } |
| 2020 | |
| 2021 | func BenchmarkMux(b *testing.B) { |
| 2022 | h1 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}) |
| 2023 | h2 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}) |
| 2024 | h3 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}) |
| 2025 | h4 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}) |
| 2026 | h5 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}) |
| 2027 | h6 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}) |
| 2028 | |
| 2029 | mx := NewRouter() |
| 2030 | mx.Get("/", h1) |
| 2031 | mx.Get("/hi", h2) |
| 2032 | mx.Post("/hi-post", h2) // used to benchmark 405 responses |
| 2033 | mx.Get("/sup/{id}/and/{this}", h3) |
| 2034 | mx.Get("/sup/{id}/{bar:foo}/{this}", h3) |
| 2035 | |
| 2036 | mx.Route("/sharing/{x}/{hash}", func(mx Router) { |
| 2037 | mx.Get("/", h4) // subrouter-1 |
| 2038 | mx.Get("/{network}", h5) // subrouter-1 |
| 2039 | mx.Get("/twitter", h5) |
| 2040 | mx.Route("/direct", func(mx Router) { |
| 2041 | mx.Get("/", h6) // subrouter-2 |
| 2042 | mx.Get("/download", h6) |
| 2043 | }) |
| 2044 | }) |
| 2045 | |
| 2046 | routes := []string{ |
| 2047 | "/", |
| 2048 | "/hi", |
| 2049 | "/hi-post", |
| 2050 | "/sup/123/and/this", |
| 2051 | "/sup/123/foo/this", |
| 2052 | "/sharing/z/aBc", // subrouter-1 |
| 2053 | "/sharing/z/aBc/twitter", // subrouter-1 |
| 2054 | "/sharing/z/aBc/direct", // subrouter-2 |
| 2055 | "/sharing/z/aBc/direct/download", // subrouter-2 |
| 2056 | } |
| 2057 | |
| 2058 | for _, path := range routes { |
| 2059 | b.Run("route:"+path, func(b *testing.B) { |
| 2060 | w := httptest.NewRecorder() |
| 2061 | r, _ := http.NewRequest("GET", path, nil) |
| 2062 | |
| 2063 | b.ReportAllocs() |
| 2064 | b.ResetTimer() |
| 2065 | |
| 2066 | for i := 0; i < b.N; i++ { |
| 2067 | mx.ServeHTTP(w, r) |
| 2068 | } |
| 2069 | }) |
| 2070 | } |
| 2071 | } |