Skip to content

Commit

Permalink
test: add benchmark for health check
Browse files Browse the repository at this point in the history
  • Loading branch information
elliotxx committed Jul 28, 2023
1 parent 97f22ce commit a1eed69
Showing 1 changed file with 66 additions and 3 deletions.
69 changes: 66 additions & 3 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@ package healthcheck

import (
"fmt"
"io/ioutil"

Check failure on line 5 in example_test.go

View workflow job for this annotation

GitHub Actions / Run lint check

SA1019: "io/ioutil" has been deprecated since Go 1.19: As of Go 1.16, the same functionality is now provided by package io or package os, and those implementations should be preferred in new code. See the specific function documentation for details. (staticcheck)
"net/http"
"net/http/httptest"
"testing"

"github.com/elliotxx/healthcheck/checks"
"github.com/gin-gonic/gin"
)

func Example_register() {
// Example code
r := gin.Default()
r := gin.New()
_ = Register(&r.RouterGroup)

// Simulate request
Expand All @@ -25,7 +27,7 @@ func Example_register() {

func Example_registerFor() {
// Example code
r := gin.Default()
r := gin.New()

config := NewDefaultConfig()
config.Verbose = true
Expand All @@ -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")}
Expand All @@ -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()
Expand Down

0 comments on commit a1eed69

Please sign in to comment.