github.com/hxx258456/ccgo@v0.0.5-0.20230213014102-48b35f46f66f/mux/bench_test.go (about)

     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  
     5  package mux
     6  
     7  import (
     8  	"testing"
     9  
    10  	http "github.com/hxx258456/ccgo/gmhttp"
    11  	"github.com/hxx258456/ccgo/gmhttp/httptest"
    12  )
    13  
    14  func BenchmarkMux(b *testing.B) {
    15  	router := new(Router)
    16  	handler := func(w http.ResponseWriter, r *http.Request) {}
    17  	router.HandleFunc("/v1/{v1}", handler)
    18  
    19  	request, _ := http.NewRequest("GET", "/v1/anything", nil)
    20  	for i := 0; i < b.N; i++ {
    21  		router.ServeHTTP(nil, request)
    22  	}
    23  }
    24  
    25  func BenchmarkMuxAlternativeInRegexp(b *testing.B) {
    26  	router := new(Router)
    27  	handler := func(w http.ResponseWriter, r *http.Request) {}
    28  	router.HandleFunc("/v1/{v1:(?:a|b)}", handler)
    29  
    30  	requestA, _ := http.NewRequest("GET", "/v1/a", nil)
    31  	requestB, _ := http.NewRequest("GET", "/v1/b", nil)
    32  	for i := 0; i < b.N; i++ {
    33  		router.ServeHTTP(nil, requestA)
    34  		router.ServeHTTP(nil, requestB)
    35  	}
    36  }
    37  
    38  func BenchmarkManyPathVariables(b *testing.B) {
    39  	router := new(Router)
    40  	handler := func(w http.ResponseWriter, r *http.Request) {}
    41  	router.HandleFunc("/v1/{v1}/{v2}/{v3}/{v4}/{v5}", handler)
    42  
    43  	matchingRequest, _ := http.NewRequest("GET", "/v1/1/2/3/4/5", nil)
    44  	notMatchingRequest, _ := http.NewRequest("GET", "/v1/1/2/3/4", nil)
    45  	recorder := httptest.NewRecorder()
    46  	for i := 0; i < b.N; i++ {
    47  		router.ServeHTTP(nil, matchingRequest)
    48  		router.ServeHTTP(recorder, notMatchingRequest)
    49  	}
    50  }