-
Notifications
You must be signed in to change notification settings - Fork 19
/
claims_merge_benchmark_test.go
164 lines (138 loc) · 3.18 KB
/
claims_merge_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
package jwt
import (
"encoding/json"
"fmt"
"reflect"
"testing"
"time"
)
type testStruct struct {
Username string `json:"username"`
Age int `json:"age"`
}
func BenchmarkSignWithMerge(b *testing.B) { // faster.
for i := 0; i < b.N; i++ {
if _, err := Sign(testAlg, testSecret, testStruct{ // init in the test.
Username: "kataras",
Age: 27,
}, MaxAge(15*time.Minute)); err != nil {
b.Fatal(err)
}
}
}
var testStructValue = testStruct{
Username: "kataras",
Age: 27,
}
// Follows benchmarks to see which way is fastest to automatically add "exp" and "iat" standard JWT claims.
// The initial and unique idea of the `Merge` function is the fastest(x3) implementation one.
func BenchmarkSignWithoutMerge(b *testing.B) { // slower
for i := 0; i < b.N; i++ {
claims := Map{"username": "kataras", "age": 27} // init in the test.
MaxAgeMap(15*time.Minute, claims)
if _, err := Sign(testAlg, testSecret, claims); err != nil {
b.Fatal(err)
}
}
}
func BenchmarkMerge(b *testing.B) { // Our way is the fastest and others' advandages are not worth, keep it.
now := Clock()
iat := now.Unix()
exp := now.Add(15 * time.Minute).Unix()
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
claims := Merge(testStructValue, Claims{Expiry: exp, IssuedAt: iat})
payload, err := Marshal(claims)
if err != nil {
b.Fatal(err)
}
if len(payload) == 0 {
b.Fatal("empty payload")
}
}
}
func BenchmarkStructToMapJSON(b *testing.B) {
now := Clock()
iat := now.Unix()
exp := now.Add(15 * time.Minute).Unix()
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
claims, err := structToMapJSON(testStructValue)
if err != nil {
b.Fatal(err)
}
if claims["exp"] == nil {
claims["exp"] = exp
claims["iat"] = iat
}
payload, err := Marshal(claims)
if err != nil {
b.Fatal(err)
}
if len(payload) == 0 {
b.Fatal("empty payload")
}
}
}
func BenchmarkStructToMapReflection(b *testing.B) {
now := Clock()
iat := now.Unix()
exp := now.Add(15 * time.Minute).Unix()
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
claims, err := structToMapReflection(testStructValue)
if err != nil {
b.Fatal(err)
}
if claims["exp"] == nil {
claims["exp"] = exp
claims["iat"] = iat
}
payload, err := Marshal(claims)
if err != nil {
b.Fatal(err)
}
if len(payload) == 0 {
b.Fatal("empty payload")
}
}
}
func structToMapJSON(i interface{}) (Map, error) {
if m, ok := i.(Map); ok {
return m, nil
}
m := make(Map)
raw, err := json.Marshal(i)
if err != nil {
return nil, err
}
if err := json.Unmarshal(raw, &m); err != nil {
return nil, err
}
return m, nil
}
func structToMapReflection(i interface{}) (Map, error) {
if m, ok := i.(Map); ok {
return m, nil
}
v := reflect.Indirect(reflect.ValueOf(i))
if v.Kind() != reflect.Struct {
return nil, fmt.Errorf("structToMapReflection only accepts structs; got %T", v)
}
n := v.NumField()
m := make(map[string]interface{}, n)
typ := v.Type()
for i := 0; i < n; i++ {
fieldTyp := typ.Field(i)
if fieldTyp.PkgPath != "" {
continue
}
if tag := fieldTyp.Tag.Get("json"); tag != "" {
m[tag] = v.Field(i).Interface()
}
}
return m, nil
}