1package httpsnoop 2 3import ( 4 "net/http" 5 "net/http/httptest" 6 "testing" 7) 8 9func BenchmarkBaseline(b *testing.B) { 10 benchmark(b, false) 11} 12 13func BenchmarkCaptureMetrics(b *testing.B) { 14 benchmark(b, true) 15} 16 17func BenchmarkWrap(b *testing.B) { 18 b.StopTimer() 19 doneCh := make(chan struct{}, 1) 20 h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 21 b.StartTimer() 22 for i := 0; i < b.N; i++ { 23 Wrap(w, Hooks{}) 24 } 25 doneCh <- struct{}{} 26 }) 27 s := httptest.NewServer(h) 28 defer s.Close() 29 if _, err := http.Get(s.URL); err != nil { 30 b.Fatal(err) 31 } 32 <-doneCh 33} 34 35func benchmark(b *testing.B, captureMetrics bool) { 36 b.StopTimer() 37 dummyH := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}) 38 h := dummyH 39 if captureMetrics { 40 h = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 41 CaptureMetrics(dummyH, w, r) 42 }) 43 } 44 s := httptest.NewServer(h) 45 defer s.Close() 46 b.StartTimer() 47 for i := 0; i < b.N; i++ { 48 _, err := http.Get(s.URL) 49 if err != nil { 50 b.Fatal(err) 51 } 52 } 53} 54