Skip to content

Commit 7f38560

Browse files
committed
Benchmarks
1 parent 63d19ec commit 7f38560

File tree

2 files changed

+67
-5
lines changed

2 files changed

+67
-5
lines changed

readme.md

+29
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,32 @@ https://example.com/a/b/c?expiry=3xx1vi&foo=bar&signature=-mwCtMLTBgDkShZTbBcHjR
117117
## Notes
118118

119119
* Any change in the order of the query parameters in a signed URL renders it invalid, unless `SkipQuery` is specified.
120+
121+
## Benchmarks
122+
123+
```bash
124+
> go test -run=XXX -bench=.
125+
goos: linux
126+
goarch: amd64
127+
pkg: github.com/leg100/surl
128+
cpu: AMD Ryzen 7 3800X 8-Core Processor
129+
BenchmarkSigner/by_path/decimal/no_opts-16 514827 2451 ns/op
130+
BenchmarkSigner/by_path/decimal/with_prefix-16 362757 3174 ns/op
131+
BenchmarkSigner/by_path/decimal/skip_query-16 470324 2399 ns/op
132+
BenchmarkSigner/by_path/decimal/skip_query_and_with_prefix-16 365589 3052 ns/op
133+
BenchmarkSigner/by_path/base58/no_opts-16 481573 2339 ns/op
134+
BenchmarkSigner/by_path/base58/with_prefix-16 466501 2809 ns/op
135+
BenchmarkSigner/by_path/base58/skip_query-16 546319 2304 ns/op
136+
BenchmarkSigner/by_path/base58/skip_query_and_with_prefix-16 453450 2799 ns/op
137+
BenchmarkSigner/by_query/decimal/no_opts-16 197506 6483 ns/op
138+
BenchmarkSigner/by_query/decimal/with_prefix-16 165560 6662 ns/op
139+
BenchmarkSigner/by_query/decimal/skip_query-16 157486 7489 ns/op
140+
BenchmarkSigner/by_query/decimal/skip_query_and_with_prefix-16 150454 7837 ns/op
141+
BenchmarkSigner/by_query/base58/no_opts-16 206263 6256 ns/op
142+
BenchmarkSigner/by_query/base58/with_prefix-16 161008 6787 ns/op
143+
BenchmarkSigner/by_query/base58/skip_query-16 159558 7374 ns/op
144+
BenchmarkSigner/by_query/base58/skip_query_and_with_prefix-16 151778 7578 ns/op
145+
PASS
146+
ok github.com/leg100/surl 19.906s
147+
```
148+

signer_test.go

+38-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package surl
22

33
import (
4+
"crypto/rand"
45
"net/url"
56
"path"
67
"testing"
@@ -10,8 +11,8 @@ import (
1011
"github.com/stretchr/testify/require"
1112
)
1213

13-
func TestSigner(t *testing.T) {
14-
formatters := []struct {
14+
var (
15+
formatters = []struct {
1516
name string
1617
formatter Option
1718
}{
@@ -25,7 +26,7 @@ func TestSigner(t *testing.T) {
2526
},
2627
}
2728

28-
encoders := []struct {
29+
encoders = []struct {
2930
name string
3031
encoder Option
3132
}{
@@ -39,7 +40,7 @@ func TestSigner(t *testing.T) {
3940
},
4041
}
4142

42-
opts := []struct {
43+
opts = []struct {
4344
name string
4445
options []Option
4546
}{
@@ -59,7 +60,9 @@ func TestSigner(t *testing.T) {
5960
options: []Option{SkipQuery(), PrefixPath("/signed")},
6061
},
6162
}
63+
)
6264

65+
func TestSigner(t *testing.T) {
6366
inputs := []struct {
6467
name string
6568
unsigned string
@@ -90,7 +93,7 @@ func TestSigner(t *testing.T) {
9093
options := append(opt.options, f.formatter, enc.encoder)
9194
signer := New([]byte("abc123"), options...)
9295

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) {
9497
signed, err := signer.Sign(tt.unsigned, time.Second*10)
9598
require.NoError(t, err)
9699

@@ -210,3 +213,33 @@ func TestSigner_Errors(t *testing.T) {
210213
assert.Error(t, err)
211214
})
212215
}
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

Comments
 (0)