From a1eed695199632d9404479f1b7c7941b5b2f0e45 Mon Sep 17 00:00:00 2001 From: elliotxx <951376975@qq.com> Date: Fri, 28 Jul 2023 15:57:18 +0800 Subject: [PATCH] test: add benchmark for health check --- example_test.go | 69 ++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 66 insertions(+), 3 deletions(-) diff --git a/example_test.go b/example_test.go index 677ff67..eebaf56 100644 --- a/example_test.go +++ b/example_test.go @@ -2,8 +2,10 @@ package healthcheck import ( "fmt" + "io/ioutil" "net/http" "net/http/httptest" + "testing" "github.com/elliotxx/healthcheck/checks" "github.com/gin-gonic/gin" @@ -11,7 +13,7 @@ import ( func Example_register() { // Example code - r := gin.Default() + r := gin.New() _ = Register(&r.RouterGroup) // Simulate request @@ -25,7 +27,7 @@ func Example_register() { func Example_registerFor() { // Example code - r := gin.Default() + r := gin.New() config := NewDefaultConfig() config.Verbose = true @@ -43,7 +45,7 @@ func Example_registerFor() { func Example_customHandler() { // Example code - r := gin.Default() + r := gin.New() r.GET("livez", NewHandler(NewDefaultHandlerConfig())) readyzChecks := []checks.Check{checks.NewPingCheck(), checks.NewEnvCheck("DB_HOST")} @@ -69,10 +71,71 @@ func Example_customHandler() { // } +func BenchmarkDefaultHealthCheck(b *testing.B) { + // Example code + gin.SetMode(gin.ReleaseMode) + gin.DefaultWriter = ioutil.Discard + r := gin.New() + _ = Register(&r.RouterGroup) + + // Simulate request for benchmark + req, _ := http.NewRequest(http.MethodGet, "/healthz", nil) + for i := 0; i < b.N; i++ { + w := httptest.NewRecorder() + r.ServeHTTP(w, req) + } +} + +func BenchmarkDefaultHealthCheckWithVerbose(b *testing.B) { + // Example code + gin.SetMode(gin.ReleaseMode) + gin.DefaultWriter = ioutil.Discard + r := gin.New() + _ = Register(&r.RouterGroup) + + // Simulate request for benchmark + req, _ := http.NewRequest(http.MethodGet, "/healthz?verbose", nil) + for i := 0; i < b.N; i++ { + w := httptest.NewRecorder() + r.ServeHTTP(w, req) + } +} + +func BenchmarkDefaultHealthCheckWithExcludes(b *testing.B) { + // Example code + gin.SetMode(gin.ReleaseMode) + gin.DefaultWriter = ioutil.Discard + r := gin.New() + _ = Register(&r.RouterGroup) + + // Simulate request for benchmark + req, _ := http.NewRequest(http.MethodGet, "/healthz?excludes=Ping", nil) + for i := 0; i < b.N; i++ { + w := httptest.NewRecorder() + r.ServeHTTP(w, req) + } +} + +func BenchmarkDefaultHealthCheckWithVerboseExcludes(b *testing.B) { + // Example code + gin.SetMode(gin.ReleaseMode) + gin.DefaultWriter = ioutil.Discard + r := gin.New() + _ = Register(&r.RouterGroup) + + // Simulate request for benchmark + req, _ := http.NewRequest(http.MethodGet, "/healthz?verbose&excludes=Ping", nil) + for i := 0; i < b.N; i++ { + w := httptest.NewRecorder() + r.ServeHTTP(w, req) + } +} + func dummyRequest(r *gin.Engine, endpoint string) { w := httptest.NewRecorder() req, _ := http.NewRequest(http.MethodGet, endpoint, nil) r.ServeHTTP(w, req) + fmt.Println(w.Code) fmt.Println(w.Body.String()) fmt.Println()