1// Copyright 2012 The Gorilla Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package mux
6
7import (
8	"net/http"
9	"net/http/httptest"
10	"testing"
11)
12
13func BenchmarkMux(b *testing.B) {
14	router := new(Router)
15	handler := func(w http.ResponseWriter, r *http.Request) {}
16	router.HandleFunc("/v1/{v1}", handler)
17
18	request, _ := http.NewRequest("GET", "/v1/anything", nil)
19	for i := 0; i < b.N; i++ {
20		router.ServeHTTP(nil, request)
21	}
22}
23
24func BenchmarkMuxAlternativeInRegexp(b *testing.B) {
25	router := new(Router)
26	handler := func(w http.ResponseWriter, r *http.Request) {}
27	router.HandleFunc("/v1/{v1:(?:a|b)}", handler)
28
29	requestA, _ := http.NewRequest("GET", "/v1/a", nil)
30	requestB, _ := http.NewRequest("GET", "/v1/b", nil)
31	for i := 0; i < b.N; i++ {
32		router.ServeHTTP(nil, requestA)
33		router.ServeHTTP(nil, requestB)
34	}
35}
36
37func BenchmarkManyPathVariables(b *testing.B) {
38	router := new(Router)
39	handler := func(w http.ResponseWriter, r *http.Request) {}
40	router.HandleFunc("/v1/{v1}/{v2}/{v3}/{v4}/{v5}", handler)
41
42	matchingRequest, _ := http.NewRequest("GET", "/v1/1/2/3/4/5", nil)
43	notMatchingRequest, _ := http.NewRequest("GET", "/v1/1/2/3/4", nil)
44	recorder := httptest.NewRecorder()
45	for i := 0; i < b.N; i++ {
46		router.ServeHTTP(nil, matchingRequest)
47		router.ServeHTTP(recorder, notMatchingRequest)
48	}
49}
50