Skip to content

Commit 46fd8d2

Browse files
committed
test: add unit and integration coverage suites
1 parent d08d545 commit 46fd8d2

65 files changed

Lines changed: 36973 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

client/bench_test.go

Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
package client
2+
3+
import (
4+
"bytes"
5+
"fmt"
6+
"net"
7+
"testing"
8+
9+
"github.com/ncode/twamp/common"
10+
"github.com/ncode/twamp/crypto"
11+
"github.com/ncode/twamp/logging"
12+
)
13+
14+
// BenchmarkSendWithHMAC benchmarks the sendWithHMAC hot path in authenticated mode
15+
func BenchmarkSendWithHMAC(b *testing.B) {
16+
// Setup
17+
serverConn, clientConn := net.Pipe()
18+
defer serverConn.Close()
19+
defer clientConn.Close()
20+
21+
client := &Client{
22+
conn: clientConn,
23+
logger: logging.NewNoop(),
24+
keyDerivation: &crypto.TWAMPKeys{
25+
HMACKey: bytes.Repeat([]byte{0xAA}, 32),
26+
},
27+
}
28+
29+
// Server side consumer
30+
done := make(chan struct{})
31+
go func() {
32+
defer close(done)
33+
buf := make([]byte, 1024)
34+
for i := 0; i < b.N; i++ {
35+
_, err := serverConn.Read(buf)
36+
if err != nil {
37+
return
38+
}
39+
}
40+
}()
41+
42+
data := []byte("test message for benchmarking sendWithHMAC performance")
43+
44+
b.ResetTimer()
45+
for i := 0; i < b.N; i++ {
46+
err := client.sendWithHMAC(data, true)
47+
if err != nil {
48+
b.Fatalf("sendWithHMAC failed: %v", err)
49+
}
50+
}
51+
b.StopTimer()
52+
53+
<-done
54+
}
55+
56+
// BenchmarkSendWithHMACUnauthenticated benchmarks sendWithHMAC in unauthenticated mode
57+
func BenchmarkSendWithHMACUnauthenticated(b *testing.B) {
58+
// Setup
59+
serverConn, clientConn := net.Pipe()
60+
defer serverConn.Close()
61+
defer clientConn.Close()
62+
63+
client := &Client{
64+
conn: clientConn,
65+
logger: logging.NewNoop(),
66+
mode: common.ModeUnauthenticated,
67+
}
68+
69+
// Server side consumer
70+
done := make(chan struct{})
71+
go func() {
72+
defer close(done)
73+
buf := make([]byte, 1024)
74+
for i := 0; i < b.N; i++ {
75+
_, err := serverConn.Read(buf)
76+
if err != nil {
77+
return
78+
}
79+
}
80+
}()
81+
82+
data := []byte("test message for benchmarking sendWithHMAC performance")
83+
84+
b.ResetTimer()
85+
for i := 0; i < b.N; i++ {
86+
err := client.sendWithHMAC(data, false)
87+
if err != nil {
88+
b.Fatalf("sendWithHMAC failed: %v", err)
89+
}
90+
}
91+
b.StopTimer()
92+
93+
<-done
94+
}
95+
96+
// BenchmarkCalculateHMAC benchmarks HMAC calculation separately
97+
func BenchmarkCalculateHMAC(b *testing.B) {
98+
client := &Client{
99+
keyDerivation: &crypto.TWAMPKeys{
100+
HMACKey: bytes.Repeat([]byte{0xAA}, 32),
101+
},
102+
}
103+
104+
data := []byte("test message for benchmarking HMAC calculation performance")
105+
106+
b.ResetTimer()
107+
for i := 0; i < b.N; i++ {
108+
_, err := client.calculateHMAC(data)
109+
if err != nil {
110+
b.Fatalf("calculateHMAC failed: %v", err)
111+
}
112+
}
113+
}
114+
115+
// BenchmarkReceiveAndVerify benchmarks the receiveAndVerify hot path
116+
func BenchmarkReceiveAndVerify(b *testing.B) {
117+
// Setup
118+
serverConn, clientConn := net.Pipe()
119+
defer serverConn.Close()
120+
defer clientConn.Close()
121+
122+
client := &Client{
123+
conn: clientConn,
124+
logger: logging.NewNoop(),
125+
}
126+
127+
// Server side sender
128+
done := make(chan struct{})
129+
testData := bytes.Repeat([]byte{0x42}, 64) // 64 bytes of test data
130+
go func() {
131+
defer close(done)
132+
for i := 0; i < b.N; i++ {
133+
_, err := serverConn.Write(testData)
134+
if err != nil {
135+
return
136+
}
137+
}
138+
}()
139+
140+
b.ResetTimer()
141+
for i := 0; i < b.N; i++ {
142+
_, err := client.receiveAndVerify(64, false)
143+
if err != nil {
144+
b.Fatalf("receiveAndVerify failed: %v", err)
145+
}
146+
}
147+
b.StopTimer()
148+
149+
<-done
150+
}
151+
152+
// BenchmarkStopNSessions benchmarks StopNSessions with varying session counts
153+
func BenchmarkStopNSessions(b *testing.B) {
154+
sessionCounts := []int{1, 5, 10, 50}
155+
156+
for _, count := range sessionCounts {
157+
b.Run(fmt.Sprintf("%d_sessions", count), func(b *testing.B) {
158+
b.ResetTimer()
159+
for i := 0; i < b.N; i++ {
160+
b.StopTimer()
161+
162+
// Setup client with sessions
163+
serverConn, clientConn := net.Pipe()
164+
client := &Client{
165+
conn: clientConn,
166+
mode: common.ModeUnauthenticated,
167+
controlMode: common.ModeUnauthenticated,
168+
testMode: common.ModeUnauthenticated,
169+
currentSessions: make(map[common.SessionID]*TestSession),
170+
logger: logging.NewNoop(),
171+
}
172+
173+
// Create sessions
174+
sessionChans := make([]chan struct{}, count)
175+
for j := 0; j < count; j++ {
176+
sid := common.SessionID{byte(j), byte(j >> 8), byte(j >> 16), byte(j >> 24)}
177+
sessionChans[j] = make(chan struct{})
178+
client.currentSessions[sid] = &TestSession{
179+
sid: sid,
180+
stopChan: sessionChans[j],
181+
logger: logging.NewNoop(),
182+
}
183+
}
184+
185+
// Server side
186+
done := make(chan struct{})
187+
go func() {
188+
defer close(done)
189+
buf := make([]byte, 16384)
190+
serverConn.Read(buf)
191+
}()
192+
193+
b.StartTimer()
194+
client.StopNSessions(uint32(count))
195+
b.StopTimer()
196+
197+
<-done
198+
serverConn.Close()
199+
clientConn.Close()
200+
201+
// Cleanup channels
202+
for _, ch := range sessionChans {
203+
select {
204+
case <-ch:
205+
default:
206+
close(ch)
207+
}
208+
}
209+
}
210+
})
211+
}
212+
}

0 commit comments

Comments
 (0)