Last active
August 29, 2015 14:22
-
-
Save daved/6566099ba472c81fc18a to your computer and use it in GitHub Desktop.
framework detangling (handler wrappers)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package chain_test | |
import ( | |
"net/http" | |
"net/http/httptest" | |
"testing" | |
"github.com/codemodus/chain" | |
"golang.org/x/net/context" | |
"github.com/kressin/middleware" | |
) | |
type reqCtxTestKey int | |
const ( | |
keyTestString reqCtxTestKey = iota | |
) | |
func setTestString(ctx context.Context, s string) context.Context { | |
return context.WithValue(ctx, keyTestString, s) | |
} | |
func getTestString(ctx context.Context) (string, bool) { | |
s, ok := ctx.Value(keyTestString).(string) | |
return s, ok | |
} | |
func emptyCtxTestHandlerWrapper(n chain.Handler) chain.Handler { | |
return chain.HandlerFunc(func(ctx context.Context, w http.ResponseWriter, r *http.Request) { | |
// uncomment to test functionality - swap out testing.B for testing.T and change func names | |
/*cs, _ := getTestString(ctx) | |
ctx = setTestString(ctx, cs + "t ")*/ | |
n.ServeHTTPContext(ctx, w, r) | |
/*s, ok := getTestString(ctx) | |
if ok { | |
_, _ = w.Write([]byte(s)) | |
}*/ | |
}) | |
} | |
func emptyCtxMiddleware(n middleware.ContextHandler) middleware.ContextHandler { | |
return middleware.ContextHandlerFunc(func(ctx context.Context, w http.ResponseWriter, r *http.Request) { | |
// uncomment to test functionality - swap out testing.B for testing.T and change func names | |
/*cs, _ := getTestString(ctx) | |
ctx = setTestString(ctx, cs + "t ")*/ | |
n.ServeHTTPC(ctx, w, r) | |
/*s, ok := getTestString(ctx) | |
if ok { | |
_, _ = w.Write([]byte(s)) | |
}*/ | |
}) | |
} | |
func nilTestHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) { | |
return | |
} | |
func wrapMiddleware(n middleware.ContextHandler) http.Handler { | |
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | |
n.ServeHTTPC(context.Background(), w, r) | |
}) | |
} | |
func BenchmarkCMChain10(b *testing.B) { | |
c0 := chain.New(emptyCtxTestHandlerWrapper, | |
emptyCtxTestHandlerWrapper, emptyCtxTestHandlerWrapper, emptyCtxTestHandlerWrapper, | |
emptyCtxTestHandlerWrapper, emptyCtxTestHandlerWrapper, emptyCtxTestHandlerWrapper, | |
emptyCtxTestHandlerWrapper) | |
c0 = c0.Append(emptyCtxTestHandlerWrapper).Append(emptyCtxTestHandlerWrapper) | |
m := http.NewServeMux() | |
m.Handle("/", c0.EndFn(nilTestHandler)) | |
s := httptest.NewServer(m) | |
b.ResetTimer() | |
for n := 0; n < b.N; n++ { | |
re0, err := http.Get(s.URL + "/") | |
if err != nil { | |
b.Error(err) | |
} | |
_ = re0.Body.Close() | |
} | |
} | |
func BenchmarkXNest10(b *testing.B) { | |
h := &adapter{ | |
ctx: context.Background(), | |
h: emptyCtxTestHandlerWrapper(emptyCtxTestHandlerWrapper( | |
emptyCtxTestHandlerWrapper(emptyCtxTestHandlerWrapper( | |
emptyCtxTestHandlerWrapper(emptyCtxTestHandlerWrapper( | |
emptyCtxTestHandlerWrapper(emptyCtxTestHandlerWrapper( | |
emptyCtxTestHandlerWrapper(emptyCtxTestHandlerWrapper( | |
chain.HandlerFunc(nilTestHandler))))))))))), | |
} | |
m := http.NewServeMux() | |
m.Handle("/", h) | |
s := httptest.NewServer(m) | |
b.ResetTimer() | |
for n := 0; n < b.N; n++ { | |
re0, err := http.Get(s.URL + "/") | |
if err != nil { | |
b.Error(err) | |
} | |
_ = re0.Body.Close() | |
} | |
} | |
func BenchmarkKressin10(b *testing.B) { | |
c0 := middleware.New(emptyCtxMiddleware, | |
emptyCtxMiddleware, emptyCtxMiddleware, emptyCtxMiddleware, | |
emptyCtxMiddleware, emptyCtxMiddleware, emptyCtxMiddleware, | |
emptyCtxMiddleware) | |
c0 = c0.Append(emptyCtxMiddleware).Append(emptyCtxMiddleware) | |
m := http.NewServeMux() | |
m.Handle("/", wrapMiddleware(c0.ThenFunc(nilTestHandler))) | |
s := httptest.NewServer(m) | |
b.ResetTimer() | |
for n := 0; n < b.N; n++ { | |
re0, err := http.Get(s.URL + "/") | |
if err != nil { | |
b.Error(err) | |
} | |
_ = re0.Body.Close() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment