Skip to content

Commit d410dc2

Browse files
committed
add tests and benchmark for compact encoding
BenchmarkLegacyEncode-8 239144 4504 ns/op 3445 B/op 21 allocs/op BenchmarkLegacyEncode-8 261038 4499 ns/op 3442 B/op 21 allocs/op BenchmarkCompactEncode-8 933446 1304 ns/op 721 B/op 4 allocs/op BenchmarkCompactEncode-8 927328 1331 ns/op 721 B/op 4 allocs/op BenchmarkLegacyDecode-8 298273 3923 ns/op 2312 B/op 17 allocs/op BenchmarkLegacyDecode-8 298401 3944 ns/op 2367 B/op 17 allocs/op BenchmarkCompactDecode-8 806112 1359 ns/op 465 B/op 3 allocs/op BenchmarkCompactDecode-8 905617 1348 ns/op 471 B/op 3 allocs/op
1 parent 406f3ed commit d410dc2

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

securecookie_test.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"crypto/sha256"
1111
"encoding/base64"
1212
"fmt"
13+
"math/rand"
1314
"reflect"
1415
"strings"
1516
"testing"
@@ -35,7 +36,13 @@ func TestSecureCookie(t *testing.T) {
3536
"baz": 128,
3637
}
3738

39+
rng := rand.New(rand.NewSource(1))
40+
3841
for i := 0; i < 50; i++ {
42+
t.Log("i=", i)
43+
s1.Compact(i&1 != 0)
44+
s2.Compact(i&2 != 0)
45+
3946
// Running this multiple times to check if any special character
4047
// breaks encoding/decoding.
4148
encoded, err1 := s1.Encode("sid", value)
@@ -71,6 +78,20 @@ func TestSecureCookie(t *testing.T) {
7178
if err4.IsInternal() {
7279
t.Fatalf("Expected IsInternal() == false, got: %#v", err4)
7380
}
81+
82+
// check compatibility
83+
s1.Compact(!s1.genCompact)
84+
dst3 := make(map[string]interface{})
85+
err5 := s1.Decode("sid", encoded, &dst3)
86+
if err5 != nil {
87+
t.Fatalf("%v: %v", err5, encoded)
88+
}
89+
if !reflect.DeepEqual(dst3, value) {
90+
t.Fatalf("Expected %v, got %v.", value, dst3)
91+
}
92+
93+
value["foo"] = "bar" + string([]rune{rune(rng.Intn(1024) + 20)})
94+
value["bas"] = rng.Intn(1000000)
7495
}
7596
}
7697

@@ -306,3 +327,49 @@ func TestCustomType(t *testing.T) {
306327
t.Fatalf("Expected %#v, got %#v", src, dst)
307328
}
308329
}
330+
331+
const N = 250
332+
333+
func benchmarkEncode(b *testing.B, compact bool) {
334+
hk := make([]byte, 32)
335+
bk := make([]byte, 32)
336+
buf := make([]byte, N)
337+
rand.Read(hk)
338+
rand.Read(bk)
339+
rand.Read(buf)
340+
sec := New(hk, bk)
341+
sec.SetSerializer(NopEncoder{})
342+
sec.Compact(compact)
343+
b.ResetTimer()
344+
for i := 0; i < b.N; i++ {
345+
v := buf[:rand.Intn(N-N/4)+N/4]
346+
_, _ = sec.Encode("session", v)
347+
}
348+
}
349+
350+
func benchmarkDecode(b *testing.B, compact bool) {
351+
hk := make([]byte, 32)
352+
bk := make([]byte, 32)
353+
buf := make([]byte, N)
354+
rand.Read(hk)
355+
rand.Read(bk)
356+
rand.Read(buf)
357+
sec := New(hk, bk)
358+
sec.SetSerializer(NopEncoder{})
359+
sec.Compact(compact)
360+
vals := make([]string, 128)
361+
for i := range vals {
362+
v := buf[:rand.Intn(N-N/4)+N/4]
363+
vals[i], _ = sec.Encode("session", v)
364+
}
365+
b.ResetTimer()
366+
for i := 0; i < b.N; i++ {
367+
var v []byte
368+
_ = sec.Decode("session", vals[i&127], &v)
369+
}
370+
}
371+
372+
func BenchmarkLegacyEncode(b *testing.B) { benchmarkEncode(b, false) }
373+
func BenchmarkCompactEncode(b *testing.B) { benchmarkEncode(b, true) }
374+
func BenchmarkLegacyDecode(b *testing.B) { benchmarkDecode(b, false) }
375+
func BenchmarkCompactDecode(b *testing.B) { benchmarkDecode(b, true) }

0 commit comments

Comments
 (0)