-
Notifications
You must be signed in to change notification settings - Fork 19
/
verify_benchmark_test.go
83 lines (67 loc) · 1.64 KB
/
verify_benchmark_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package benchmarks
import (
"testing"
"time"
josejwt "github.com/go-jose/go-jose/v3/jwt"
jwtgo "github.com/golang-jwt/jwt"
jwt "github.com/kataras/jwt"
)
func createTestToken() ([]byte, error) {
claims := jwt.Map{"foo": "bar"}
return jwt.Sign(jwt.HS256, testSecret, claims, jwt.MaxAge(15*time.Minute))
}
// Test performance of Verify.
func BenchmarkVerify(b *testing.B) {
token, err := createTestToken()
if err != nil {
b.Fatal(err)
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err = jwt.Verify(jwt.HS256, testSecret, token)
if err != nil {
b.Fatal(err)
}
}
}
func BenchmarkVerify_jwt_go(b *testing.B) {
token, err := createTestToken()
if err != nil {
b.Fatal(err)
}
tokenString := string(token)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := jwtgo.Parse(tokenString, func(token *jwtgo.Token) (interface{}, error) {
return testSecret, nil
})
if err != nil {
b.Fatal(err)
}
}
}
func BenchmarkVerify_go_jose(b *testing.B) {
token, err := createTestToken()
if err != nil {
b.Fatal(err)
}
tokenString := string(token)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
// Note: the go-jose package's `ParseSigned` does not actually verify the token,
// it just decodes it, we have to call its `Claims` method without a destination (2nd arg)
// to make it to verify the signature (the fastest way to do it on that package)
// unlike the rest (kataras/jwt and golang-jwt/jwt).
parsedToken, err := josejwt.ParseSigned(tokenString)
if err != nil {
b.Fatal(err)
}
err = parsedToken.Claims(testSecret)
if err != nil {
b.Fatal(err)
}
}
}