49 lines
1.3 KiB
Go
49 lines
1.3 KiB
Go
package transport_test
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
omni "git.yuntuo.hk/k2safe/go-omni"
|
|
netrouter "git.yuntuo.hk/k2safe/go-omni/transport/nethttp"
|
|
)
|
|
|
|
func BenchmarkTransportRouteDispatch(b *testing.B) {
|
|
b.Run("nethttp", func(b *testing.B) {
|
|
router := netrouter.New()
|
|
router.Handle(http.MethodGet, "/users/:id", http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}))
|
|
request := httptest.NewRequest(http.MethodGet, "/users/42", nil)
|
|
b.ReportAllocs()
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
writer := httptest.NewRecorder()
|
|
router.ServeHTTP(writer, request)
|
|
if writer.Code != http.StatusNoContent {
|
|
b.Fatalf("unexpected status %d", writer.Code)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
func BenchmarkTransportApplicationRequest(b *testing.B) {
|
|
b.Run("nethttp", func(b *testing.B) {
|
|
application := omni.NewWithRouter(netrouter.New())
|
|
application.GET("/health", func(c *omni.Context) error {
|
|
return c.OK(map[string]string{"status": "ok"})
|
|
})
|
|
request := httptest.NewRequest(http.MethodGet, "/health", nil)
|
|
b.ReportAllocs()
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
writer := httptest.NewRecorder()
|
|
application.ServeHTTP(writer, request)
|
|
if writer.Code != http.StatusOK {
|
|
b.Fatalf("unexpected status %d", writer.Code)
|
|
}
|
|
}
|
|
})
|
|
}
|