Skip to content

Commit

Permalink
cleaned up benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
dgrijalva committed Apr 11, 2015
1 parent 901c439 commit c0c67af
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 19 deletions.
27 changes: 8 additions & 19 deletions hmac_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,25 +78,14 @@ func TestHMACSign(t *testing.T) {
}
}

func BenchmarkHMACSigning(b *testing.B) {

var preppedData = make([]struct {
t *jwt.Token
method jwt.SigningMethod
k interface{}
}, len(hmacTestData))

for i, data := range hmacTestData {
preppedData[i].t, _ = jwt.Parse(data.tokenString, func(*jwt.Token) (interface{}, error) { return nil, nil })
preppedData[i].method = jwt.GetSigningMethod(data.alg)
}
func BenchmarkHS256Signing(b *testing.B) {
benchmarkSigning(b, jwt.SigningMethodHS256, hmacTestKey)
}

for _, data := range preppedData {
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
data.t.SignedString(hmacTestKey)
}
})
}
func BenchmarkHS384Signing(b *testing.B) {
benchmarkSigning(b, jwt.SigningMethodHS384, hmacTestKey)
}

func BenchmarkHS512Signing(b *testing.B) {
benchmarkSigning(b, jwt.SigningMethodHS512, hmacTestKey)
}
11 changes: 11 additions & 0 deletions jwt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,14 @@ func TestParseRequest(t *testing.T) {
}
}
}

// Helper method for benchmarking various methods
func benchmarkSigning(b *testing.B, method jwt.SigningMethod, key interface{}) {
t := jwt.New(method)
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
t.SignedString(key)
}
})

}
12 changes: 12 additions & 0 deletions rsa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,15 @@ func TestRSAKeyParsing(t *testing.T) {
}

}

func BenchmarkRS256Signing(b *testing.B) {
benchmarkSigning(b, jwt.SigningMethodRS256, hmacTestKey)
}

func BenchmarkRS384Signing(b *testing.B) {
benchmarkSigning(b, jwt.SigningMethodRS384, hmacTestKey)
}

func BenchmarkRS512Signing(b *testing.B) {
benchmarkSigning(b, jwt.SigningMethodRS512, hmacTestKey)
}

0 comments on commit c0c67af

Please sign in to comment.