Пример #1
0
func BenchmarkMiddleware5Afterware1(b *testing.B) {
	kami.Reset()
	numbers := []int{1, 2, 3, 4, 5}
	for _, n := range numbers {
		n := n // wtf
		kami.Use("/", func(ctx context.Context, w http.ResponseWriter, r *http.Request) context.Context {
			return context.WithValue(ctx, n, n)
		})
	}
	kami.After("/", func(ctx context.Context, w http.ResponseWriter, r *http.Request) context.Context {
		for _, n := range numbers {
			if ctx.Value(n) != n {
				panic(n)
			}
		}
		return ctx
	})
	kami.Get("/test", func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
		for _, n := range numbers {
			if ctx.Value(n) != n {
				w.WriteHeader(http.StatusServiceUnavailable)
				return
			}
		}
	})
	req, _ := http.NewRequest("GET", "/test", nil)
	b.ResetTimer()
	for n := 0; n < b.N; n++ {
		resp := httptest.NewRecorder()
		kami.Handler().ServeHTTP(resp, req)
	}
}
Пример #2
0
func routeBench(b *testing.B, route string) {
	kami.Reset()
	kami.Use("/Z/", noopMW)
	kami.After("/Z/", noopMW)
	kami.Get(route, noop)
	req, _ := http.NewRequest("GET", route, nil)
	b.ResetTimer()
	for n := 0; n < b.N; n++ {
		resp := httptest.NewRecorder()
		kami.Handler().ServeHTTP(resp, req)
	}
}
Пример #3
0
// This tests just the URL walking middleware engine.
func BenchmarkMiddlewareAfterwareMiss(b *testing.B) {
	kami.Reset()
	kami.Use("/dog/", func(ctx context.Context, w http.ResponseWriter, r *http.Request) context.Context {
		return nil
	})
	kami.After("/dog/", func(ctx context.Context, w http.ResponseWriter, r *http.Request) context.Context {
		return nil
	})
	kami.Get("/a/bbb/cc/d/e", func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(http.StatusOK)
	})
	req, _ := http.NewRequest("GET", "/a/bbb/cc/d/e", nil)
	b.ResetTimer()
	for n := 0; n < b.N; n++ {
		resp := httptest.NewRecorder()
		kami.Handler().ServeHTTP(resp, req)
	}
}