-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathsign_benchmark_test.go
207 lines (182 loc) · 5.52 KB
/
sign_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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package benchmarks
import (
"testing"
"time"
jose "github.com/go-jose/go-jose/v3"
josejwt "github.com/go-jose/go-jose/v3/jwt"
jwtgo "github.com/golang-jwt/jwt"
jwt "github.com/kataras/jwt"
)
var testSecret = []byte("sercrethatmaycontainch@r$32chars")
func BenchmarkSign_Map(b *testing.B) {
type testStruct struct {
Foo string `json:"foo"`
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
// supports custom types and expiration helper.
now := time.Now()
claims := map[string]any{
"foo": "bar",
"exp": now.Add(15 * time.Minute).Unix(),
"iat": now.Unix(),
}
_, err := jwt.Sign(jwt.HS256, testSecret, claims)
if err != nil {
b.Fatal(err)
}
}
}
func BenchmarkSign_Struct(b *testing.B) {
type testStruct struct {
Foo string `json:"foo"`
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
// supports custom types and expiration helper.
claims := testStruct{Foo: "bar"}
_, err := jwt.Sign(jwt.HS256, testSecret, claims, jwt.MaxAge(15*time.Minute))
if err != nil {
b.Fatal(err)
}
}
}
func BenchmarkSign_jwt_go_Map(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
now := time.Now()
claims := jwtgo.MapClaims{
"foo": "bar",
"exp": time.Now().Add(15 * time.Minute).Unix(),
"iat": now.Unix(),
}
token := jwtgo.NewWithClaims(jwtgo.SigningMethodHS256, claims)
_, err := token.SignedString(testSecret)
if err != nil {
b.Fatal(err)
}
}
}
func BenchmarkSign_jwt_go_Struct(b *testing.B) {
// This is the official example as shown at:
// https://github.com/dgrijalva/jwt-go/blob/dc14462fd58732591c7fa58cc8496d6824316a82/example_test.go#L31
type MyCustomClaims struct {
Foo string `json:"foo"`
jwtgo.StandardClaims
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
now := time.Now()
claims := MyCustomClaims{
Foo: "bar",
StandardClaims: jwtgo.StandardClaims{
ExpiresAt: now.Add(15 * time.Minute).Unix(),
IssuedAt: time.Now().Unix(),
},
}
token := jwtgo.NewWithClaims(jwtgo.SigningMethodHS256, claims)
_, err := token.SignedString(testSecret)
if err != nil {
b.Fatal(err)
}
}
}
type testStructJwtGo struct {
Foo string `json:"foo"`
Expiry int64 `json:"exp"`
IssuedAt int64 `json:"iat"`
}
var _ jwtgo.Claims = testStructJwtGo{}
func (c testStructJwtGo) Valid() error { return nil }
func BenchmarkSign_jwt_go_Struct2(b *testing.B) {
// This is our example, which performs better than its official docs
// (in order to have a fair benchmark between the rest).
//
// Does support custom type but:
// 1. Should implement a Valid() error although it's never called on its Signing process(...)
// 2. it should provide the "exp", "iat" by its own and
// 3. it does not provide automatic validation on its Verify status(!)
// It does not support separate helper for max age, so it needs a separate jwt claims struct,
// unlike kataras/jwt which you can specify an already defined struct without a single modification of its fields.
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
now := time.Now()
// Reproduce the behavior of the kataras/jwtgo.MaxAge helper.
claims := testStructJwtGo{
Foo: "bar",
Expiry: now.Add(15 * time.Minute).Unix(),
IssuedAt: now.Unix(),
}
token := jwtgo.NewWithClaims(jwtgo.SigningMethodHS256, claims)
_, err := token.SignedString(testSecret)
if err != nil {
b.Fatal(err)
}
}
}
func BenchmarkSign_go_jose_Map(b *testing.B) {
// To be fair with the rest benchmarks,
// the signer should be part of the benchmark time,
// however, let's move it outside, even with that test-benefit,
// this is the slower implementation by far:/
signer, err := jose.NewSigner(jose.SigningKey{
Algorithm: jose.HS256,
Key: testSecret,
}, (&jose.SignerOptions{}).WithType("JWT"))
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
// Support custom types but not expiration helper, so use map here to set the "exp" too.
// If we define a struct with an "exp" json tag we should be able to reproduce
// the same, however this means that you need separate structs for JWT and other usage,
// unlike kataras/jwt which u can use already defined structs.
// We will benchmark it with structs (see below test).
now := time.Now()
claims := map[string]any{
"foo": "bar",
"exp": now.Add(15 * time.Minute).Unix(),
"iat": now.Unix(),
}
_, err = josejwt.Signed(signer).Claims(claims).CompactSerialize()
if err != nil {
b.Fatal(err)
}
}
}
func BenchmarkSign_go_jose_Struct(b *testing.B) {
// We define a struct with an "exp" json tag we should be able to reproduce
// the same, however this means that you need separate structs for JWT and other usage,
// unlike kataras/jwt which u can use already defined structs.
//
// This is slower than its map test because internally the go-jose library converts
// the structure to a map and then marshals again...
type testStructWithStandardClaim struct {
Foo string `json:"foo"`
Expiry int64 `json:"exp"`
IssuedAt int64 `json:"iat"`
}
signer, err := jose.NewSigner(jose.SigningKey{
Algorithm: jose.HS256,
Key: testSecret,
}, (&jose.SignerOptions{}).WithType("JWT"))
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
now := time.Now()
// Reproduce the behavior of the kataras/jwtgo.MaxAge helper.
claims := testStructWithStandardClaim{
Foo: "bar",
Expiry: now.Add(15 * time.Minute).Unix(),
IssuedAt: now.Unix(),
}
_, err = josejwt.Signed(signer).Claims(claims).CompactSerialize()
if err != nil {
b.Fatal(err)
}
}
}