1
1
package surl
2
2
3
3
import (
4
+ "crypto/rand"
4
5
"net/url"
5
6
"path"
6
7
"testing"
@@ -10,8 +11,8 @@ import (
10
11
"github.com/stretchr/testify/require"
11
12
)
12
13
13
- func TestSigner ( t * testing. T ) {
14
- formatters : = []struct {
14
+ var (
15
+ formatters = []struct {
15
16
name string
16
17
formatter Option
17
18
}{
@@ -25,7 +26,7 @@ func TestSigner(t *testing.T) {
25
26
},
26
27
}
27
28
28
- encoders : = []struct {
29
+ encoders = []struct {
29
30
name string
30
31
encoder Option
31
32
}{
@@ -39,7 +40,7 @@ func TestSigner(t *testing.T) {
39
40
},
40
41
}
41
42
42
- opts : = []struct {
43
+ opts = []struct {
43
44
name string
44
45
options []Option
45
46
}{
@@ -59,7 +60,9 @@ func TestSigner(t *testing.T) {
59
60
options : []Option {SkipQuery (), PrefixPath ("/signed" )},
60
61
},
61
62
}
63
+ )
62
64
65
+ func TestSigner (t * testing.T ) {
63
66
inputs := []struct {
64
67
name string
65
68
unsigned string
@@ -90,7 +93,7 @@ func TestSigner(t *testing.T) {
90
93
options := append (opt .options , f .formatter , enc .encoder )
91
94
signer := New ([]byte ("abc123" ), options ... )
92
95
93
- t .Run (path .Join (tt .name , f .name , opt .name ), func (t * testing.T ) {
96
+ t .Run (path .Join (tt .name , f .name , enc . name , opt .name ), func (t * testing.T ) {
94
97
signed , err := signer .Sign (tt .unsigned , time .Second * 10 )
95
98
require .NoError (t , err )
96
99
@@ -210,3 +213,33 @@ func TestSigner_Errors(t *testing.T) {
210
213
assert .Error (t , err )
211
214
})
212
215
}
216
+
217
+ var bu string
218
+
219
+ func BenchmarkSigner (b * testing.B ) {
220
+ secret := make ([]byte , 64 )
221
+ _ , err := rand .Read (secret )
222
+ require .NoError (b , err )
223
+
224
+ // invoke bench for each combination of formatter, encoder, and set of
225
+ // options
226
+ for _ , f := range formatters {
227
+ for _ , enc := range encoders {
228
+ for _ , opt := range opts {
229
+ options := append (opt .options , f .formatter , enc .encoder )
230
+
231
+ b .Run (path .Join (f .name , enc .name , opt .name ), func (b * testing.B ) {
232
+ signer := New (secret , options ... )
233
+
234
+ var u string
235
+ for n := 0 ; n < b .N ; n ++ {
236
+ // store result to prevent compiler eliminating func call
237
+ u , _ = signer .Sign ("https://example.com/a/b/c?x=1&y=2&z=3" , time .Hour )
238
+ }
239
+ // store result in pkg var to to prevent compiler eliminating benchmark
240
+ bu = u
241
+ })
242
+ }
243
+ }
244
+ }
245
+ }
0 commit comments