Skip to content

Commit

Permalink
jwt: Add parser benchmarks (golang-jwt#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
lggomez authored Aug 3, 2021
1 parent bd2db2d commit 3258b3f
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 2 deletions.
18 changes: 18 additions & 0 deletions ecdsa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
Expand Down
48 changes: 46 additions & 2 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -299,5 +344,4 @@ func benchmarkSigning(b *testing.B, method jwt.SigningMethod, key interface{}) {
}
}
})

}
14 changes: 14 additions & 0 deletions rsa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 3258b3f

Please sign in to comment.