Browse Source

Also benchmark a single route, middleware

Carl Jackson 12 years ago
parent
commit
e1ff3ade4d
1 changed files with 44 additions and 5 deletions
  1. +44
    -5
      web/bench_test.go

+ 44
- 5
web/bench_test.go View File

@ -6,13 +6,8 @@ import (
mrand "math/rand" mrand "math/rand"
"net/http" "net/http"
"testing" "testing"
"time"
) )
func init() {
mrand.Seed(time.Now().Unix())
}
/* /*
The core benchmarks here are based on cypriss's mux benchmarks, which can be The core benchmarks here are based on cypriss's mux benchmarks, which can be
found here: found here:
@ -40,6 +35,13 @@ func (_ nilResponse) Header() http.Header {
func (_ nilResponse) WriteHeader(code int) { func (_ nilResponse) WriteHeader(code int) {
} }
func trivialMiddleware(h http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
h.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
}
var w nilResponse var w nilResponse
func addRoutes(m *Mux, prefix string) { func addRoutes(m *Mux, prefix string) {
@ -100,6 +102,33 @@ func benchN(b *testing.B, n int) {
} }
} }
func benchM(b *testing.B, n int) {
m := New()
m.Get("/", nilRouter{})
for i := 0; i < n; i++ {
m.Use(trivialMiddleware)
}
r, _ := http.NewRequest("GET", "/", nil)
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
m.ServeHTTP(w, r)
}
}
func BenchmarkStatic(b *testing.B) {
m := New()
m.Get("/", nilRouter{})
r, _ := http.NewRequest("GET", "/", nil)
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
m.ServeHTTP(w, r)
}
}
func BenchmarkRoute5(b *testing.B) { func BenchmarkRoute5(b *testing.B) {
benchN(b, 1) benchN(b, 1)
} }
@ -112,3 +141,13 @@ func BenchmarkRoute500(b *testing.B) {
func BenchmarkRoute5000(b *testing.B) { func BenchmarkRoute5000(b *testing.B) {
benchN(b, 1000) benchN(b, 1000)
} }
func BenchmarkMiddleware1(b *testing.B) {
benchM(b, 1)
}
func BenchmarkMiddleware10(b *testing.B) {
benchM(b, 10)
}
func BenchmarkMiddleware100(b *testing.B) {
benchM(b, 100)
}

Loading…
Cancel
Save