cleaned up benchmarks

This commit is contained in:
Dave Grijalva 2015-04-11 13:53:09 -07:00
parent 901c439dfe
commit c0c67af490
3 changed files with 33 additions and 21 deletions

View File

@ -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)
}
for _, data := range preppedData {
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
data.t.SignedString(hmacTestKey)
}
})
}
func BenchmarkHS256Signing(b *testing.B) {
benchmarkSigning(b, jwt.SigningMethodHS256, hmacTestKey)
}
func BenchmarkHS384Signing(b *testing.B) {
benchmarkSigning(b, jwt.SigningMethodHS384, hmacTestKey)
}
func BenchmarkHS512Signing(b *testing.B) {
benchmarkSigning(b, jwt.SigningMethodHS512, hmacTestKey)
}

View File

@ -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)
}
})
}

View File

@ -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)
}