Skip to content

Commit 59e7d86

Browse files
committed
benchmarks
``` go test -bench . goos: darwin goarch: amd64 pkg: github.com/asoorm/tyk-mashery-auth/hook BenchmarkSha256_Sha256Sum-4 5000000 341 ns/op 48 B/op 2 allocs/op BenchmarkSha256_ValidateSignature-4 1000000 1635 ns/op 823 B/op 13 allocs/op PASS ok github.com/asoorm/tyk-mashery-auth/hook 3.720s ```
1 parent 8d23b25 commit 59e7d86

File tree

2 files changed

+47
-4
lines changed

2 files changed

+47
-4
lines changed

hook/hook.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func (s *Sha256) Init(sharedSecret string, allowedClockSkew int64, headerAuthKey
2727

2828
func (s Sha256) ValidateSignature(obj *coprocess.Object) (*coprocess.Object, error) {
2929

30-
log.Info("ValidateSignature called")
30+
//log.Info("ValidateSignature called")
3131

3232
authHeader, ok := obj.Request.Headers[s.headerAuthKey]
3333
if !ok {
@@ -60,7 +60,7 @@ func (s Sha256) validate(tokenAttempt string, signatureAttempt string) error {
6060
for i := int64(0); i <= s.allowedClockSkew; i++ {
6161
attempts++
6262
if hex.EncodeToString(s.Sha256Sum(tokenAttempt, now+i)) == signatureAttempt {
63-
log.Info("attempts: ", attempts)
63+
//log.Info("attempts: ", attempts)
6464
return nil
6565
}
6666

@@ -70,12 +70,12 @@ func (s Sha256) validate(tokenAttempt string, signatureAttempt string) error {
7070

7171
attempts++
7272
if hex.EncodeToString(s.Sha256Sum(tokenAttempt, now-i)) == signatureAttempt {
73-
log.Info("attempts: ", attempts)
73+
//log.Info("attempts: ", attempts)
7474
return nil
7575
}
7676
}
7777

78-
log.Info("attempts: ", attempts)
78+
//log.Info("attempts: ", attempts)
7979
return errors.New("invalid signature" + signatureAttempt)
8080
}
8181

hook/hook_test.go

+43
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package hook
22

33
import (
4+
"encoding/hex"
45
"testing"
56
"time"
7+
8+
"github.com/TykTechnologies/tyk-protobuf/bindings/go"
69
)
710

811
func BenchmarkSha256_Sha256Sum(b *testing.B) {
@@ -17,3 +20,43 @@ func BenchmarkSha256_Sha256Sum(b *testing.B) {
1720
_ = s.Sha256Sum("foobarbaz", now)
1821
}
1922
}
23+
24+
func BenchmarkSha256_ValidateSignature(b *testing.B) {
25+
26+
b.ReportAllocs()
27+
28+
sharedSecret := "foobarbaz"
29+
authToken := "4321knj8fqgm5ffq64tdzifato6fb5p5rkqze933ehivqelctivti8qs0xnzmpq3"
30+
31+
s := Sha256{}
32+
s.Init(sharedSecret, 600, "Authorization", "X-Signature")
33+
signatureAttempt := s.Sha256Sum(authToken, time.Now().Unix())
34+
35+
coprocessObj := coprocess.Object{
36+
Request: &coprocess.MiniRequestObject{
37+
Headers: map[string]string{
38+
"Authorization": authToken,
39+
"X-Signature": hex.EncodeToString(signatureAttempt),
40+
},
41+
},
42+
Session: &coprocess.SessionState{
43+
Metadata: map[string]string{
44+
"secret": sharedSecret,
45+
},
46+
},
47+
}
48+
49+
//requestJsBytes, _ := json.MarshalIndent(coprocessObj, "", "")
50+
//println(string(requestJsBytes))
51+
52+
for n := 0; n < b.N; n++ {
53+
t := Sha256{}
54+
t.Init(sharedSecret, 600, "Authorization", "X-Signature")
55+
//s.Init(sharedSecret, 600, )
56+
_, err := s.ValidateSignature(&coprocessObj)
57+
if err != nil {
58+
b.Log(err.Error())
59+
b.FailNow()
60+
}
61+
}
62+
}

0 commit comments

Comments
 (0)