-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbench_test.go
More file actions
111 lines (94 loc) · 2.17 KB
/
bench_test.go
File metadata and controls
111 lines (94 loc) · 2.17 KB
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
package noise_test
import (
"testing"
"github.com/tetsuo/noise"
)
// setupTransportPair completes an NN handshake and returns tx/rx cipher states.
func setupTransportPair(b *testing.B) (tx, rx *noise.CipherState) {
b.Helper()
initiator, err := noise.NewNoiseState(noise.PatternNN, true, &noise.Config{})
if err != nil {
b.Fatal(err)
}
responder, err := noise.NewNoiseState(noise.PatternNN, false, &noise.Config{})
if err != nil {
b.Fatal(err)
}
if err := initiator.Initialize([]byte("bench"), nil); err != nil {
b.Fatal(err)
}
if err := responder.Initialize([]byte("bench"), nil); err != nil {
b.Fatal(err)
}
msg1, err := initiator.Send(nil)
if err != nil {
b.Fatal(err)
}
if _, err := responder.Recv(msg1); err != nil {
b.Fatal(err)
}
msg2, err := responder.Send(nil)
if err != nil {
b.Fatal(err)
}
if _, err := initiator.Recv(msg2); err != nil {
b.Fatal(err)
}
return initiator.Tx(), responder.Rx()
}
func BenchmarkEncrypt64(b *testing.B) {
tx, _ := setupTransportPair(b)
plaintext := make([]byte, 64)
buf := make([]byte, 0, 64+16)
b.SetBytes(64)
b.ReportAllocs()
for b.Loop() {
_, err := tx.Encrypt(buf[:0], plaintext, nil)
if err != nil {
b.Fatal(err)
}
}
}
func BenchmarkDecrypt64(b *testing.B) {
benchDecrypt(b, 64)
}
func benchDecrypt(b *testing.B, size int) {
b.Helper()
tx, rx := setupTransportPair(b)
plaintext := make([]byte, size)
// Pre-encrypt exactly b.N messages to avoid measuring encryption time
ciphertexts := make([][]byte, b.N)
for i := range b.N {
ct, err := tx.Encrypt(nil, plaintext, nil)
if err != nil {
b.Fatal(err)
}
ciphertexts[i] = ct
}
buf := make([]byte, 0, size)
b.SetBytes(int64(size))
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := rx.Decrypt(buf[:0], ciphertexts[i], nil)
if err != nil {
b.Fatal(err)
}
}
}
func BenchmarkEncrypt1024(b *testing.B) {
tx, _ := setupTransportPair(b)
plaintext := make([]byte, 1024)
buf := make([]byte, 0, 1024+16)
b.SetBytes(1024)
b.ReportAllocs()
for b.Loop() {
_, err := tx.Encrypt(buf[:0], plaintext, nil)
if err != nil {
b.Fatal(err)
}
}
}
func BenchmarkDecrypt1024(b *testing.B) {
benchDecrypt(b, 1024)
}