]> Cypherpunks repositories - gostls13.git/commitdiff
net/http: improve speed of default mux
authorKenny Grant <kennygrant@gmail.com>
Sun, 12 Mar 2017 10:02:47 +0000 (10:02 +0000)
committerBrad Fitzpatrick <bradfitz@golang.org>
Wed, 22 Mar 2017 23:33:19 +0000 (23:33 +0000)
The DefaultServeMux included in net/http uses a map to store routes,
but iterates all keys for every request to allow longer paths.

This change checks the map for an exact match first.

To check performance was better, BenchmarkServeMux has been added -
this adds >100 routes and checks the matches.

Exact matches are faster and more predictable on this benchmark
and on most existing package benchmarks.

https://perf.golang.org/search?q=upload:20170312.1

ServeMux-4  2.02ms ± 2% 0.04ms ± 2%  −98.08%  (p=0.004 n=5+6)

https://perf.golang.org/search?q=upload:20170312.2

ReadRequestChrome-4 184MB/s  ± 8% 186MB/s  ± 1% ~
ReadRequestCurl-4 45.0MB/s ± 1% 46.2MB/s ± 1% +2.71%
Read...Apachebench-4 45.8MB/s ±13% 48.7MB/s ± 1% ~
ReadRequestSiege-4 63.6MB/s ± 5% 69.2MB/s ± 1% +8.75%
ReadRequestWrk-4 30.9MB/s ± 9% 34.4MB/s ± 2% +11.25%

Change-Id: I8afafcb956f07197419d545a9f1c03ecaa307384
Reviewed-on: https://go-review.googlesource.com/38057
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/net/http/serve_test.go
src/net/http/server.go

index a421fb07093f4fd543e6c22fda943a0844a86ecb..be429e57256106f98b8006e4fe64bfbad6a61d1a 100644 (file)
@@ -460,6 +460,47 @@ func TestMuxRedirectLeadingSlashes(t *testing.T) {
        }
 }
 
+func BenchmarkServeMux(b *testing.B) {
+
+       type test struct {
+               path string
+               code int
+               req  *Request
+       }
+
+       // Build example handlers and requests
+       var tests []test
+       endpoints := []string{"search", "dir", "file", "change", "count", "s"}
+       for _, e := range endpoints {
+               for i := 200; i < 230; i++ {
+                       p := fmt.Sprintf("/%s/%d/", e, i)
+                       tests = append(tests, test{
+                               path: p,
+                               code: i,
+                               req:  &Request{Method: "GET", Host: "localhost", URL: &url.URL{Path: p}},
+                       })
+               }
+       }
+       mux := NewServeMux()
+       for _, tt := range tests {
+               mux.Handle(tt.path, serve(tt.code))
+       }
+
+       rw := httptest.NewRecorder()
+       b.ReportAllocs()
+       b.ResetTimer()
+       for i := 0; i < b.N; i++ {
+               for _, tt := range tests {
+                       *rw = httptest.ResponseRecorder{}
+                       h, pattern := mux.Handler(tt.req)
+                       h.ServeHTTP(rw, tt.req)
+                       if pattern != tt.path || rw.Code != tt.code {
+                               b.Fatalf("got %d, %q, want %d, %q", rw.Code, pattern, tt.code, tt.path)
+                       }
+               }
+       }
+}
+
 func TestServerTimeouts(t *testing.T) {
        setParallel(t)
        defer afterTest(t)
index c7710358add6c9c988736c512fbef692e694eef5..3276f0e9754eee6c0df81b541cecc43ace7fb418 100644 (file)
@@ -2167,9 +2167,16 @@ func cleanPath(p string) string {
        return np
 }
 
-// Find a handler on a handler map given a path string
-// Most-specific (longest) pattern wins
+// Find a handler on a handler map given a path string.
+// Most-specific (longest) pattern wins.
 func (mux *ServeMux) match(path string) (h Handler, pattern string) {
+       // Check for exact match first.
+       v, ok := mux.m[path]
+       if ok {
+               return v.h, v.pattern
+       }
+
+       // Check for longest valid match.
        var n = 0
        for k, v := range mux.m {
                if !pathMatch(k, path) {