added benchmarks

This commit is contained in:
ston1th 2018-09-13 14:18:11 +02:00
commit fb52ae4a30
4 changed files with 113 additions and 34 deletions

View file

@ -9,17 +9,19 @@ import (
"time"
)
var claims = Claims{
"sub": "1234567890",
"name": "John Doe",
"admin": true,
"fizz": "buzz",
}
func TestValidate(t *testing.T) {
jwt, err := New(time.Second, NewMemBlacklist(), nil)
if err != nil {
t.Error(err)
}
token := NewToken(Claims{
"sub": "1234567890",
"name": "John Doe",
"admin": true,
"fizz": "buzz",
}, nil)
token := NewToken(claims.Copy(), nil)
err = jwt.Sign(token)
if err != nil {
t.Error(err)
@ -86,12 +88,7 @@ func TestNoBlacklist(t *testing.T) {
if err != nil {
t.Error(err)
}
token := NewToken(Claims{
"sub": "1234567890",
"name": "John Doe",
"admin": true,
"fizz": "buzz",
}, nil)
token := NewToken(claims.Copy(), nil)
err = jwt.Sign(token)
if err != nil {
t.Error(err)
@ -133,3 +130,85 @@ func TestInvalidSecretReader(t *testing.T) {
t.Error(errors.New("error should be invalid key size"))
}
}
func BenchmarkDecodeToken(b *testing.B) {
jwt, _ := New(0, nil, nil)
t := NewToken(claims.Copy(), nil)
jwt.Sign(t)
token := t.String()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
DecodeToken(token)
}
}
func BenchmarkNew(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
New(0, nil, nil)
}
}
func BenchmarkNewWithBlacklist(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
New(0, NewMemBlacklist(), nil)
}
}
func BenchmarkSignHS256(b *testing.B) {
jwt, _ := New(0, nil, nil)
t := NewToken(claims.Copy(), nil)
b.ReportAllocs()
for i := 0; i < b.N; i++ {
jwt.Sign(t)
}
}
func BenchmarkSignHS384(b *testing.B) {
jwt, _ := New(0, nil, nil)
t := NewToken(claims.Copy(), NewHS384())
b.ReportAllocs()
for i := 0; i < b.N; i++ {
jwt.Sign(t)
}
}
func BenchmarkSignHS512(b *testing.B) {
jwt, _ := New(0, nil, nil)
t := NewToken(claims.Copy(), NewHS512())
b.ReportAllocs()
for i := 0; i < b.N; i++ {
jwt.Sign(t)
}
}
func BenchmarkVerifyHS256(b *testing.B) {
jwt, _ := New(0, nil, nil)
t := NewToken(claims.Copy(), nil)
jwt.Sign(t)
b.ReportAllocs()
for i := 0; i < b.N; i++ {
jwt.Verify(t)
}
}
func BenchmarkVerifyHS384(b *testing.B) {
jwt, _ := New(0, nil, nil)
t := NewToken(claims.Copy(), NewHS384())
jwt.Sign(t)
b.ReportAllocs()
for i := 0; i < b.N; i++ {
jwt.Verify(t)
}
}
func BenchmarkVerifyHS512(b *testing.B) {
jwt, _ := New(0, nil, nil)
t := NewToken(claims.Copy(), NewHS512())
jwt.Sign(t)
b.ReportAllocs()
for i := 0; i < b.N; i++ {
jwt.Verify(t)
}
}