github.com/guyezi/gofrontend@v0.0.0-20200228202240-7a62a49e62c0/libgo/go/net/http/server_test.go (about)

     1  // Copyright 2018 The Go 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  // Server unit tests
     6  
     7  package http
     8  
     9  import (
    10  	"fmt"
    11  	"testing"
    12  )
    13  
    14  func BenchmarkServerMatch(b *testing.B) {
    15  	fn := func(w ResponseWriter, r *Request) {
    16  		fmt.Fprintf(w, "OK")
    17  	}
    18  	mux := NewServeMux()
    19  	mux.HandleFunc("/", fn)
    20  	mux.HandleFunc("/index", fn)
    21  	mux.HandleFunc("/home", fn)
    22  	mux.HandleFunc("/about", fn)
    23  	mux.HandleFunc("/contact", fn)
    24  	mux.HandleFunc("/robots.txt", fn)
    25  	mux.HandleFunc("/products/", fn)
    26  	mux.HandleFunc("/products/1", fn)
    27  	mux.HandleFunc("/products/2", fn)
    28  	mux.HandleFunc("/products/3", fn)
    29  	mux.HandleFunc("/products/3/image.jpg", fn)
    30  	mux.HandleFunc("/admin", fn)
    31  	mux.HandleFunc("/admin/products/", fn)
    32  	mux.HandleFunc("/admin/products/create", fn)
    33  	mux.HandleFunc("/admin/products/update", fn)
    34  	mux.HandleFunc("/admin/products/delete", fn)
    35  
    36  	paths := []string{"/", "/notfound", "/admin/", "/admin/foo", "/contact", "/products",
    37  		"/products/", "/products/3/image.jpg"}
    38  	b.StartTimer()
    39  	for i := 0; i < b.N; i++ {
    40  		if h, p := mux.match(paths[i%len(paths)]); h != nil && p == "" {
    41  			b.Error("impossible")
    42  		}
    43  	}
    44  	b.StopTimer()
    45  }