jwt: Add parser benchmarks (#70)

This commit is contained in:
Luis Gabriel Gomez 2021-08-03 17:57:36 -03:00 committed by GitHub
parent bd2db2d4a2
commit 3258b3fca0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 78 additions and 2 deletions

View File

@ -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) { func BenchmarkECDSASigning(b *testing.B) {
for _, data := range ecdsaTestData { for _, data := range ecdsaTestData {
key, _ := ioutil.ReadFile(data.keys["private"]) key, _ := ioutil.ReadFile(data.keys["private"])

View File

@ -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{}) { func benchmarkSigning(b *testing.B, method jwt.SigningMethod, key interface{}) {
b.Helper()
t := jwt.New(method) t := jwt.New(method)
b.ReportAllocs() b.ReportAllocs()
b.ResetTimer() b.ResetTimer()
@ -299,5 +344,4 @@ func benchmarkSigning(b *testing.B, method jwt.SigningMethod, key interface{}) {
} }
} }
}) })
} }

View File

@ -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) { func BenchmarkRS256Signing(b *testing.B) {
key, _ := ioutil.ReadFile("test/sample_key") key, _ := ioutil.ReadFile("test/sample_key")
parsedKey, err := jwt.ParseRSAPrivateKeyFromPEM(key) parsedKey, err := jwt.ParseRSAPrivateKeyFromPEM(key)