diff --git a/ecdsa_test.go b/ecdsa_test.go index 5b503eb..b7c78b9 100644 --- a/ecdsa_test.go +++ b/ecdsa_test.go @@ -106,6 +106,24 @@ func TestECDSASign(t *testing.T) { } } +func BenchmarkECDSAParsing(b *testing.B) { + for _, data := range ecdsaTestData { + key, _ := ioutil.ReadFile(data.keys["private"]) + + b.Run(data.name, func(b *testing.B) { + b.ReportAllocs() + b.ResetTimer() + b.RunParallel(func(pb *testing.PB) { + for pb.Next() { + if _, err := jwt.ParseECPrivateKeyFromPEM(key); err != nil { + b.Fatalf("Unable to parse ECDSA private key: %v", err) + } + } + }) + }) + } +} + func BenchmarkECDSASigning(b *testing.B) { for _, data := range ecdsaTestData { key, _ := ioutil.ReadFile(data.keys["private"]) diff --git a/parser_test.go b/parser_test.go index 870fd1c..2de337a 100644 --- a/parser_test.go +++ b/parser_test.go @@ -287,8 +287,53 @@ func TestParser_ParseUnverified(t *testing.T) { } } -// Helper method for benchmarking various methods +func BenchmarkParseUnverified(b *testing.B) { + privateKey := test.LoadRSAPrivateKeyFromDisk("test/sample_key") + + // Iterate over test data set and run tests + for _, data := range jwtTestData { + // If the token string is blank, use helper function to generate string + if data.tokenString == "" { + data.tokenString = test.MakeSampleToken(data.claims, privateKey) + } + + // Parse the token + var parser = data.parser + if parser == nil { + parser = new(jwt.Parser) + } + // Figure out correct claims type + switch data.claims.(type) { + case jwt.MapClaims: + b.Run("map_claims", func(b *testing.B) { + benchmarkParsing(b, parser, data.tokenString, jwt.MapClaims{}) + }) + case *jwt.StandardClaims: + b.Run("standard_claims", func(b *testing.B) { + benchmarkParsing(b, parser, data.tokenString, &jwt.StandardClaims{}) + }) + } + } +} + +// Helper method for benchmarking various parsing methods +func benchmarkParsing(b *testing.B, parser *jwt.Parser, tokenString string, claims jwt.Claims) { + b.Helper() + b.ReportAllocs() + b.ResetTimer() + b.RunParallel(func(pb *testing.PB) { + for pb.Next() { + _, _, err := parser.ParseUnverified(tokenString, jwt.MapClaims{}) + if err != nil { + b.Fatal(err) + } + } + }) +} + +// Helper method for benchmarking various signing methods func benchmarkSigning(b *testing.B, method jwt.SigningMethod, key interface{}) { + b.Helper() t := jwt.New(method) b.ReportAllocs() b.ResetTimer() @@ -299,5 +344,4 @@ func benchmarkSigning(b *testing.B, method jwt.SigningMethod, key interface{}) { } } }) - } diff --git a/rsa_test.go b/rsa_test.go index e0c6663..cbf6e69 100644 --- a/rsa_test.go +++ b/rsa_test.go @@ -155,6 +155,20 @@ func TestRSAKeyParsing(t *testing.T) { } +func BenchmarkRSAParsing(b *testing.B) { + key, _ := ioutil.ReadFile("test/sample_key") + + b.ReportAllocs() + b.ResetTimer() + b.RunParallel(func(pb *testing.PB) { + for pb.Next() { + if _, err := jwt.ParseRSAPrivateKeyFromPEM(key); err != nil { + b.Fatalf("Unable to parse RSA private key: %v", err) + } + } + }) +} + func BenchmarkRS256Signing(b *testing.B) { key, _ := ioutil.ReadFile("test/sample_key") parsedKey, err := jwt.ParseRSAPrivateKeyFromPEM(key)