git.sr.ht/~pingoo/stdx@v0.0.0-20240218134121-094174641f6e/jwt/jwt_benchmark_test.go (about)

     1  package jwt_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"git.sr.ht/~pingoo/stdx/jwt"
     7  )
     8  
     9  func BenchmarkJwt(b *testing.B) {
    10  	jwtProvider, _ := jwt.NewProvider(INSECURE_SECRET, jwt.AlgorithmHS256, nil)
    11  	type payload struct {
    12  		Message string
    13  	}
    14  	claims := payload{Message: "Hello World"}
    15  	var parsedClaims payload
    16  
    17  	b.Run("issue", func(b *testing.B) {
    18  		b.ReportAllocs()
    19  		b.ResetTimer()
    20  		for i := 0; i < b.N; i++ {
    21  			_, _ = jwtProvider.IssueToken(claims, nil)
    22  		}
    23  	})
    24  
    25  	token, _ := jwtProvider.IssueToken(claims, nil)
    26  
    27  	b.Run("verify", func(b *testing.B) {
    28  		b.ReportAllocs()
    29  		b.ResetTimer()
    30  		for i := 0; i < b.N; i++ {
    31  			_ = jwtProvider.VerifyToken(token, &parsedClaims)
    32  		}
    33  	})
    34  }