forked from mirror/jwt
jwt: Add parser benchmarks (#70)
This commit is contained in:
parent
bd2db2d4a2
commit
3258b3fca0
|
@ -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"])
|
||||
|
|
|
@ -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{}) {
|
|||
}
|
||||
}
|
||||
})
|
||||
|
||||
}
|
||||
|
|
14
rsa_test.go
14
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)
|
||||
|
|
Loading…
Reference in New Issue